Class Notes on Cat Command

Class Notes on Cat Command

Class Notes on "Cat" Command:

  • Viewing File Contents: - Part I / Chapter 2

When administering your server, you will very often find that you are modifying configuration files, which are all ASCII Text files. The ability to browse the content of these files is very important. Different Methods exists to do this:

cat : This command stands for “catenate” and displays the content of a file by dumping it to the screen.

  • Useful if contents of the file don’t fit on the screen. You will see some text scrolling by, and as the final result, you will only see the last lines of files being displayed on the screen.

cat is one of the most commonly-used commands in Linux. It can be used to:

  • Display text files :: example: cat mytext.txt or cat /etc/passwd

--- If you specify more than one file name, cat will display those files one after the other, catenating their contents to

standard output. Example: cat mytext.txt mytext2.txt

Will print the contents of those two text files as if they were a single file

  • Copy text files into a new document :: example: cat mytext.txt > newfile.txt

cat sends its output to stdout (standard output), which is usually the terminal screen. You can redirect this output to a file using the shellredirection symbol "".

--- After I run “example” above, “this command”will read the contents of mytext.txt and send them to standard output;

--- instead of displaying the text, however, the shell will redirect the output to the file newfile.txt. If newfile.txt does not exist, it will be created. (If newfile.txt already exists, it will be overwritten and its previous contents will be lost, so be careful.)

  • Append the contents of a text file to the end of another text file, combining them
  • Instead of overwriting another file, you can also append a source text file to another using the redirection operator "".
  • Example: cat mytext.txt > another-text-file.txt

--- the command above: will read the contents of mytext.txt, and write them at the end of another-text-file.txt.

If another-text-file.txt does not already exist, it will be created and the contents of mytext.txt will be written

to the newfile.