the command line: It is a powerful and flexible operating environment on Linux, and — if you practice — can actually be quite fun, too!
In this chapter, you learn how to master the command line so that you are able to perform common tasks through it, and also link commands together to create new command groups. We also look at the two most popular Linux text editors: Vim and Emacs. The command line is also known as the shell, the console, the command prompt, and the
Why Use the Shell?
Moving from the GUI to the command line is a conscious choice for most people, although it is increasingly rare that it is an either/or choice. While in X, you can press Ctrl+Alt+F1 at any time to switch to a terminal, but most people tend to run an X terminal application that enables them to have the point-and-click applications and the command-line applications side by side.
Reasons for running the shell include
> You want to chain two or more commands together.
> You want to use a command or parameter available only on the shell.
> You are working on a text-only system.
> You have used it for a long time and feel comfortable there.
Chaining two or more commands together is what gives the shell its real power. Hundreds of commands are available and, by combining them in different ways, you get hundreds of new commands. Some of the shell commands are available through the GUI, but these commands usually have only a small subset of their parameters available, which limits what you are able to do with them.
Working from a text-only system encapsulates both working locally with a broken GUI and also connecting to a remote, text-only system. If your Linux server is experiencing problems, the last thing you want to do is load it down with a GUI connection — working in text mode is faster and more efficient.
The last use is the most common: People use the shell just because it is familiar to them, with many people even using the shell to start GUI applications just because it saves them taking their hands off the keyboard for a moment! This is not a bad thing; it provides fluency and ease with the system and is a perfectly valid way of working.
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 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