IT 1100 : Introduction to Operating Systems

Chapter 6

Concatenate Files

As you read the section on the cat command, please note that our primary usage of this command in this class is to display the content of a file. It does a whole lot more, but we won't worry about that right now.

The cat command has similar results to the less command. It allows us to view the contents of a file. Try the following commands to see the difference.

less /etc/passwd

cat /etc/passwd

Redirection

  • Linux programs can receive input known as stdin or standard input (primarily this comes from the keyboard)
  • Programs have 2 types of output (standard output or stdout and standard error or stderr). stdout are normal output messages of a program (ie. the output generated by ls -l) stderr are the status messages or error messages of a program (we will see these later)
  • By default the 2 types of output are sent to the screen.

Redirection

Sometimes you want to redirect the standard output from the screen to a file or some other location (a printer?). You can do this by using either > or >> after the command.

  • ls -l > file_list.txt
  • echo "Who was in the movie Black sheep?" >> chris_farley.txt

Practice the above commands a few times and then use the cat command to see if you can figure out the difference between the > and >>

Redirect StdErr

Issue a command that generates an error like:

  • ls -l /var/i/dont/exist

Now , redirect that error to a file:

  • ls -l /var/i/dont/exist 2> bad_error

Redirect both

To redirect both stdout and stderr to same file do:

  • ls -l /var &> both.txt

Redirect output to the black hole

What if I don't want to see output on the screen AND I don't want to send it to a file? There is a location in linux to do that.

  • ls -l /var/ &> /dev/null

Piping

You can take the output of one command and send it to another command by using the pipe | operator. Here are some examples:

  • ls -l | wc -l

The above command took the output of the ls command and piped it to the wc command. You could use man wc to figure out what either command does if you don't remember.

What does this command do?

  • echo "My name is fred" | wc
  • cat /etc/passwd | less

Textbook Time

Read and Complete the Exercises in the Book