Выбрать главу

Basic Commands

It is impossible to know how many commands the average shell citizen uses, but if we had to guess, we would place it at about 25:

► cat — Prints the contents of a file

► cd — Changes directories

► chmod — Changes file access permissions

► cp — Copies files

► du — Prints disk use

► emacs — Text editor

► find — Finds files by searching

► gcc — The C/C++/Fortran compiler

► grep — Searches for a string in input

► less — The filter for paging through output

► ln — Creates links between files

► locate — Finds files from an index

► ls — Lists files in the current directory

► make — Compiles and installs programs

► man — The manual page reader

► mkdir — Makes directories

► mv — Moves files

► ps — Lists processes

► rm — Deletes files and directories

► ssh — Connects to other machines

► tail — Prints the last lines of a file

► top — Prints resource use

► vim — A text editor

► which — Prints the location of a command

► xargs — Executes commands from its input

Of course, many other commands are available to you and are used fairly often — diff, nmap, ping, su, uptime, who, and so on — but if you are able to understand the 25 listed here, you will have sufficient skill to concoct your own command combinations.

Note that we say understand the commands — not know all their possible parameters and uses. This is because several of the commands, although commonly used, are used only by people with specific needs. gcc and make are good examples of this: Unless you plan to become a programmer, you need not worry about these beyond just typing make and make install now and then. If you want to learn more about these two commands, refer to Chapter 28, "C/C++ Programming Tools for Fedora."

Similarly, both Emacs and Vim are text editors that have a text-based interface all their own, and SSH has already been covered in detail in Chapter 15, "Remote Access with SSH."

What remains are 20 commands, each of which has many parameters to customize what it actually does. Again, you can eliminate many of these because many of the parameters are esoteric and rarely used, and, the few times in your Linux life that you need them, you can just read the manual page!

We go over these commands one by one, explaining the most common ways to use them. There is one exception to this: The xargs command requires you to understand how to join commands together, so it is bundled in the section "Combining Commands."

Most of these commands have been touched on elsewhere in the book, primarily in Chapter 4, "Command-Line Quick Start." The goal of that chapter was to give you "the least you need to know" to get by, whereas in this chapter each command is treated individually with an aim to giving you the confidence needed to be able to mix them together to create your own commands.

Printing the Contents of a File with cat

Many of Fedora's shell commands manipulate text strings, so if you want to be able to feed them the contents of files, you need to be able to output those files as text. Enter the cat command, which prints the contents of any files you pass to it. Its most basic use is like this:

$ cat myfile.txt

That prints the contents of myfile.txt. For this use, there are two extra parameters that are often used: -n numbers the lines in the output, and -s ("squeeze") prints a maximum of one blank line at a time. That is, if your file has 1 line of text, 10 blank lines, 1 line of text, 10 blank lines, and so on, -s shows the first line of text, a single blank line, the next line of text, a single blank line, and so forth. When you combine -s and -n, cat numbers only the lines that are printed — the 10 blank lines shown as one will count as 1 line for numbering.

This command prints information about your CPU, stripping out multiple blank lines and numbering the output:

$ cat -sn /proc/cpuinfo

You can also use cat to print the contents of several files at once, like this:

$ cat -s myfile.txt myotherfile.txt

In that command, cat merges myfile.txt and myotherfile.txt on the output, stripping out multiple blank lines. The important thing is that cat does not distinguish between the files in the output — no filenames are printed, and no extra breaks between the two files. This allows you to treat the two as one or, by adding more files to the command line, to treat 20 files as 1.

Changing Directories with cd

Changing directories is surely something that has no options, right? Not so. cd is actually more flexible than most people realize. Unlike most of the other commands here, cd is not a command in itself — it is built in to bash (or whichever shell interpreter you are using), but it is still used like a command.

The most basic use of cd is this:

$ cd somedir

That command looks in the current directory for the somedir subdirectory, and then moves you into it. You can also specify an exact location for a directory, like this:

$ cd /home/paul/stuff/somedir

The first part of cd's magic lies in the characters (- and ~, a dash and a tilde). The first means "switch to my previous directory," and the second means "switch to my home directory." This conversation with cd shows this in action:

[paul@caitlin ~]$ cd /usr/local

[paul@caitlin local]$ cd bin

[paul@caitlin bin]$ cd -

/usr/local

[paul@caitlin local]$ cd ~

[paul@caitlin ~]$

In the first line, we change to /usr/local and get no output from the command. In the second line, we change to bin, which is a subdirectory of /usr/local. Next, cd - is used to change back to the previous directory. This time, bash prints the name of the previous directory so we know where we are. Finally, cd ~ is used to change back to the home directory, although if you want to save an extra few keystrokes, just typing cd by itself is equivalent to cd ~.