mkdir: cannot create directory `audio/sound`: No such file or directory
At first glance, this seems wrong because mkdir cannot create the directory because it does not exist. What it actually means is that it cannot create the directory sound because the directory audio does not exist. This is where the -p parameter comes in: If you try to make a directory within another directory that does not exist, as in the previous example, -p creates the parent directories, too. So:
$ mkdir -p audio/sound
That first creates the audio directory and then creates the sound directory inside it.
Moving Files with mv
This command is one of the easiest around. There are two helpful parameters to mv: -f, which overwrites files without asking; and -u, which moves the source file only if it is newer than the destination file. That's it!
Listing Processes with ps
This is the third and last "command that should be simple, but isn't" that is discussed here. The ps command lists processes and gives you an extraordinary amount of control over its operation.
The first thing to know is that ps is typically used with what are known as BSD-style parameters. Back in the section "Finding Files by Searching with find," we discussed UNIX-style, GNU-style, and X-style parameters (-c, --dosomething, and -dosomething, respectively), but BSD-style parameters are different because they use single letters without a dash.
The default use of ps therefore lists all processes you are running that are attached to the terminal. However, you can ask it to list all your processes attached to any terminal (or indeed no terminal) by adding the x parameter: ps x. You can ask it to list all processes for all users with the a parameter or combine that with x to list all processes for all users, attached to a terminal or otherwise: ps ax.
However, both of these are timid compared with the almighty u option, which enables user-oriented output. In practice, that makes a huge difference because you get important fields such as the username of the owner, how much CPU time and RAM are being used, when the process was started, and more. This outputs a lot of information, so you might want to try adding the f parameter, which creates a process forest by using ASCII art to connect parent commands with their children. You can combine all the options so far with this command: ps faux (yes, with a little imagination, you spell words with the parameters!).
You can control the order in which the data is returned by using the --sort parameter. This takes either a + or a - (although the + is default) followed by the field by which you want to sort: command, %cpu, pid, and user are all popular options. If you use the minus sign, the results are reversed. This next command lists all processes, in descending order by CPU use:
$ ps aux --sort=-%cpu
There are many other parameters for ps, including a huge number of options for compatibility with other UNIXes. If you have the time to read the man page, you should give it a try!
Deleting Files and Directories with rm
The rm command has only one parameter of interest: --preserve-root. By now, you should know that issuing rm -rf / as root will destroy your Linux installation because -r means recursive and -f means force (do not prompt for confirmation before deleting). It is possible for a clumsy person to issue this command by accident — not by typing the command on purpose, but by putting a space in the wrong place. For example:
$ rm -rf /home/paul
That command deletes the home directory of the user paul. This is not an uncommon command; after you have removed a user and backed up her data, you will probably want to issue something similar. However, if you add an accidental space between the / and the h in home, you get this:
$ rm -rf / home/paul
This time the command means "delete everything recursively from / and then delete home/paul" — quite a different result! You can stop this from happening by using the --preserve-root parameter, which stops you from catastrophe with this message:
rm: it is dangerous to operate recursively on `/'
rm: use --no-preserve-root to override this failsafe.
Of course, no one wants to keep typing --preserve-root each time he runs rm, so you should add this line to the .bashrc file in your home directory:
alias rm='rm --preserve-root'
That alias automatically adds --preserve-root to all calls to rm in future bash sessions.
Printing the Last Lines of a File with tail
If you want to watch a log file as it is written to, or want to monitor a user's actions as they are occurring, you need to be able to track log files as they change. In these situations, you need the tail command, which prints the last few lines of a file and updates as new lines are added. This command tells tail to print the last few lines of /var/log/httpd/access_log, the Apache hit log:
$ tail /var/log/httpd/access_log
To get tail to remain running and update as the file changes, add the -f parameter (which means "follow"):
$ tail -f /var/log/httpd/access_log
You can tie the lifespan of a tail call to follow the existence of a process by specifying the --pid parameter. When you do this, tail continues to follow the file you asked for until it sees that the process (identified by PID) is no longer running, at which point tail stops tailing.
If you specify multiple files on the command line, tail follows both, printing file headers whenever the input source changes. Press Ctrl+C to terminate tail when in follow mode.
Printing Resource Usage with top
The top command is unusual in this list because the few parameters it takes are rarely, if ever, used. Instead, it has a number of commands you can use while it is running to customize the information it shows you. To get the most from these instructions, open two terminal windows. In the first, run the program yes and leave it running; in the second, switch to root, and run top.
The default sort order in top shows the most CPU-intensive tasks first. The first command there should be the yes process you just launched from the other terminal, but there should be many others, too. First, you want to filter out all the other users and focus on the user running yes. To do this, press u and enter the username you used when you ran yes. When you press Enter, top filters out processes not being run by that user.