04. Viewing and creating small files cat

Let's look at the cat command (short for concatenation), which is used for manipulating text files.

There are a number of things cat can do including:

  1. Display a (small) text file.
  2. Creating a new text file.
  3. Concatente text files.
  4. Append to a text file.

1) Displaying text files

To display the contents of a file, simply place a filename or the path to a file as an argument.

$ cat fileName
file contents will output here.

2) Creating a text file

cat is also used to create a text file. The following command will allow you to type directly into the terminal to the helloWorld.txt file.

$ cat > helloWorld.txt
type in text here.

To quit, press ctrl+d.

3) Concatenating text files

To place two or more text files together, simply use the following command:

$ cat file1.txt file2.txt > newFile.txt

4) Appending text to a file

To append instead of overwriting, use >>. Appending will simply add to the end of a file instead of starting a new one.

$ cat >> helloWorld.txt
# now type some text here and it will get appended to existing text.

Again, press ctrl+d to when done.

Options

The cat command has a few options which you may find useful.

-E or --show-ends
A $ is shown at each end of line.
-n or --number
Adds line numbers to the beginning of every line.
-s or --squeeze-blank
Compresses groups of blank lines down to a single blank line.
-T or --show-tabs
Shows tab characters at ^I.

Aching back from coding all day?

Self-Massage Tool

Aching back from coding all day? Try Back Problems

Relieve spasms, tight muscles, trigger points and pressure points with the Body Back Buddy! This trigger point massage is designed to help you self-message any area of your body - especially those that are hard to reach. Keeping your muscles relaxes and out of contraction is importan in helping to reduce pain and prevent muscle injury.

$ Check price
29.9529.95Amazon 4.5 logo(3,443+ reviews)

More Back Problems resources

Take your Linux skills to the next level!

The Linux Command Line

Take your Linux skills to the next level! Try Linux & UNIX

The Linux Command Line takes you from your very first terminal keystrokes to writing full programs in Bash, the most popular Linux shell. Along the way you'll learn the timeless skills handed down by generations of gray-bearded, mouse-shunning gurus: file navigation, environment configuration, command chaining, pattern matching with regular expressions, and more.

$ Check price
39.9539.95Amazon 4.5 logo(274+ reviews)

More Linux & UNIX resources

Ad