Home » Linux Basics » 03 - Basic Commands
3

Redirection

Simplifying commands through redirection

Output from any program may be sent to a file rather than the screen using the > redirection operator.

    [ LinuxUser ] ~$ man ls > lshelp.txt

The above command sends the output of the 'man ls' command (the help page for the ls command) to the file lshelp.txt. If the file does not exist, a new file with the specified name is created and the output of the command is written into the new file. If a file with the name 'lshelp.txt' already exists, its contents are overwritten. The >> operator may be used to append to an existing file rather than overwriting the file. In the following example, the man page of the 'ls' command is appended to the contents of the help.txt file

    [ LinuxUser ] ~$ man ls >> help.txt

The tilde '~' symbol stands for the user's home directory. A user may use the following commands to write a detailed or long listing of the device drivers and other files in the /dev directory into a file name 'device-info.txt' in the home directory. The '-l' option of the 'ls' command returns a detailed listing with information about file permissions, size and so on.

    [ LinuxUser ] ~$ cd /dev
[ LinuxUser ] /dev$ ls -l > ~/device-info.txt

While the '>' operator by itself redirects the standard output of a command to a file, '2>' may be used to redirect the standard error to a file. That is, if running the command results in an error, the error message is written to the specified file:

    [ LinuxUser ] ~$ ls -l sdfhesur3 2> errors.txt
[ LinuxUser ] ~$ more errors.txt
ls: cannot access sdfhesur3: No such file or directory

A command's input may also be received from a file instead of the standard input through the '<' operator. The following command gets a list of files from the files.txt file as an argument for 'cat'; the program will display the contents of all the files whose files names are in files.txt:

    [ LinuxUser ] ~$ cat < files.txt

Piping output to a program

Piping is a powerful Linux utility that allows users to send the output of one command to the input of another command. Conceptually, the end of a command and the beginning of another are connected by the pipe; the pipe carries output from one to the input of another. Many commands may be piped together to form one long command.

The following command loads the output of the long listing of the current directory into the vi text editor. The '|' symbol is used to denote a pipe. Note that the vi command processes the input from the pipe as though it is input from the standard input - standard input is normal input the command receives from user input on the shell. The hyphen '-' is used to denote that the input to the vi command comes from the standard input.

    [ LinuxUser ] ~$ ls -l | vi -

The following example shows many commands stringed together using pipes. The first command simply displays the contents of the /etc/passwd file (this contains a list of all users and each user's details); the second command sorts this output line-by-line (by user name). Finally, the less command allows the user to navigate around this output, examine, and modify the entries in the file

    [ LinuxUser ] ~$ cat /etc/passwd | sort | less

A pipe is often used to send the output of a command or commands directly to the printer:

    [ LinuxUser ] ~$ man grep cat | lpr