0% found this document useful (0 votes)
45 views3 pages

File Editing, Redirection

The document describes various Linux command line redirection operators that allow redirecting standard output, standard error, or both to files or devices. It provides examples of using redirection operators like >, >>, 2>, 2>&1 to redirect command output and errors to files for logging or discarding errors. The tee command is also described, which copies standard input to both standard output and files, allowing output to be viewed and saved simultaneously.

Uploaded by

pmmanick
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views3 pages

File Editing, Redirection

The document describes various Linux command line redirection operators that allow redirecting standard output, standard error, or both to files or devices. It provides examples of using redirection operators like >, >>, 2>, 2>&1 to redirect command output and errors to files for logging or discarding errors. The tee command is also described, which copies standard input to both standard output and files, allowing output to be viewed and saved simultaneously.

Uploaded by

pmmanick
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Output Redirection operators: -

> file redirect stdout to overwrite a file

>> file redirect stdout to append to a file

2> file redirect stderr to overwrite a file

2> /dev/null discard stderr error messages by redirecting to /dev/null

> file  2>&1 redirect stdout and stderr to overwrite the same file

The following sequence redirects standard output to file and then redirects
standard error to the same place as standard output (file).

&> file

>> file  2>&1 redirect stdout and stderr to append to the same file

&>> file

2>&1 > file

This redirects standard error to the default place for standard output (the
terminal window, so no change) and then redirects only standard output to
file.

 Save a time stamp for later reference.

[user@host ~]$ date > /tmp/saved-timestamp

 Copy the last 100 lines from a log file to another file.

[user@host ~]$ tail -n 100 /var/log/dmesg > /tmp/last-100-boot-messages

 Concatenate four files into one.

[user@host ~]$ cat file1 file2 file3 file4 > /tmp/all-four-in-one

 List the home directory's hidden and regular file names into a file.

[user@host ~]$ ls -a > /tmp/my-file-names

[user@host ~]$ diff previous-file current-file >> /tmp/tracking-changes-made


 The next few commands generate error messages because some system directories are
inaccessible to normal users. Observe as the error messages are redirected. Redirect
errors to a file while viewing normal command output on the terminal.

[user@host ~]$ find /etc -name passwd 2> /tmp/errors

 Save process output and error messages to separate files.

[user@host ~]$ find /etc -name passwd > /tmp/output 2> /tmp/errors

 Ignore and discard error messages.

[user@host ~]$ find /etc -name passwd > /tmp/output 2> /dev/null

 Store output and generated errors together.

[user@host ~]$ find /etc -name passwd &> /tmp/save-both

 Append output and generated errors to an existing file.

[user@host ~]$ find /etc -name passwd >> /tmp/save-both 2>&1

This example takes the output of the ls command and uses less to display it on the terminal
one screen at a time.

[user@host ~]$ ls -l /usr/bin | less

Pipelines, Redirection, and the tee Command


When redirection is combined with a pipeline, the shell sets up the entire pipeline first, then it
redirects input/output. If output redirection is used in the middle of a pipeline, the output will
go to the file and not to the next command in the pipeline.
In this example, the output of the ls command goes to the file, and less displays nothing on
the terminal.

[user@host ~]$ ls > /tmp/saved-output | less

The tee command overcomes this limitation. In a pipeline, tee copies its standard input to its
standard output and also redirects its standard output to the files named as arguments to the
command.
This example redirects the output of the ls command to the file and passes it to less to be
displayed on the terminal one screen at a time.

[user@host ~]$ ls -l | tee /tmp/saved-output | less


If tee is used at the end of a pipeline, then the final output of a command can be saved and
output to the terminal at the same time.

[user@host ~]$ ls -t | head -n 10 | tee /tmp/ten-last-changed-files

You might also like