Menu Close

cat Command in Linux

Posted in Linux Tutorial

The cat command in Linux is a versatile and frequently used utility for working with text files. It derives its name from “concatenate” and is primarily used to read and concatenate files.

The cat command is an essential tool for managing files and their contents in Linux and Unix-like operating systems. Short for “concatenate,” cat is used for various tasks including file creation, viewing, and modification.

The primary function of the cat command is to concatenate files and display their contents on the terminal. However, its capabilities extend well beyond basic concatenation. With a range of options and functionalities, cat is a valuable addition to any Linux user’s command-line toolkit.

Basic Syntax

Basic syntax of cat command is: 

cat [OPTION] [FILENAME]
  • cat: command itself.

  • [OPTION]: optional arguments or flags that alter the cat command’s behavior. A hyphen (-) usually comes before them.

  • [FILE]: the name of the file or files for processing.

Here is a detailed explanation of the cat command and its various options and use cases:

Basic Usage

1. Display the Contents of a File

To display the contents of a file, you can simply use cat followed by the filename.

cat filename.txt

This will print the content of filename.txt to the standard output (the terminal).

2. Concatenate Multiple Files

You can concatenate (join together) multiple files and display their combined contents. let us create three text file textfile1.txt textfile2.txt textfile3.txt, and use echo command to give them content text file 1, text file 2, text file 3 separately.

echo text file 1 > textfile1.txt
echo text file 2 > textfile2.txt
echo text file 3 > textfile3.txt

If you pass more than one filename to cat it will output each of them, one after the other, as a single block of text:

cat testfile1.txt testfile2.txt testfile3.txt

results will be:

text file 1
text file 2
text file 3

3. Redirect Output to a File

You can use cat to combine files and redirect the output to a new file.

cat file1.txt file2.txt > combined.txt

This command concatenates file1.txt and file2.txt and writes the result to combined.txt.

4. Using cat command to create new file

Use the cat command with the input redirection (>) to create a new file in Linux.

cat > filename.txt

Press Ctrl + D to save the content and exit. 

To copy the content of one file to another, use the cat command along with redirection:

cat filename.txt > des-filename.txt

Put the names of the files need to be copied to and from in lieu of filename.txt and des-filename.txt, respectively.

Remember that if the destination file already exists, this procedure will overwrite its contents. To append the content without overwriting the existing content, you can use the cat command with the add (>>) redirection operator

6. Using cat command to append content of one file to the end of another file

cat filename.txt >> des-filename.txt

To append the content of one file to the end of another, use the append (>>) redirection operator along with the cat command. Above command will append filename.txt  to the end of des-filename.txt. 

Useful Shortcuts

Where you want to pass multiple file names to a single command, there are some useful shortcuts that can save you a lot of typing if the files have similar names. A question mark (“?”) can be used to indicate “any single character” within the file name. An asterisk (“*”) can be used to indicate “zero or more characters”. These are sometimes referred to as “wildcard” characters. A couple of examples might help, the following commands all do the same thing:

cat textfile?.txt

results will be :

text file 1 
text file 2 
text file 3

An asterisk (“*”) can be used to indicate “zero or more characters”

cat tex*

results will be:

text file 1 
text file 2 
text file 3

If you look at the output of ls you’ll notice that the only files or folders that start with “t” are the three test files we’ve just created, so you could even simplify that last command even further to cat t*, meaning “concatenate all the files whose names start with a t and are followed by zero or more other characters”. 

We can use cat command join all our files together into a single new file, then view it:

cat t* > combinedfile.txt

cat com*

results will be

text file 1 
text file 2 
text file 3

 avoid typing the commands again you can use the Up Arrow and Down Arrow keys to move back and forth through the history of commands you’ve used. Press the Up Arrow a couple of times to get to the first cat and press Enter to run it, then do the same again to get to the second.

As you can see, the file looks the same. That’s not because it’s been left untouched, but because the shell clears out all the content of the file before it writes the output of your cat command into it. Because of this, you should be extra careful when using redirection to make sure that you don’t accidentally overwrite a file you need.

You can use >> to append the content to the file.

cat t* >> combinedfile.txt

The content in combinedfile.txt will be following:

text file 1 
text file 2 
text file 3
text file 1 
text file 2 
text file 3

Useful Options

1. Show Line Numbers

To display line numbers along with the content, use the -n option.

cat -n filename.txt

This will print the file content with line numbers prefixed.

 

2. Squeeze Blank Lines

To compress multiple consecutive blank lines into a single blank line, use the -s option.

cat -s filename.txt

 

This removes extra blank lines from the output.

3. Show Non-printing Characters

To display non-printing characters (such as tab and newline) in a visible format, use the -v option.

cat -v filename.txt

Non-printing characters will be displayed in a caret notation.

4. Show End of Line Characters

To display a $ at the end of each line, use the -E option.

cat -E filename.txt

This can be useful for identifying line breaks in files.

5. Show Tabs

To display tab characters as ^I, use the -T option.

cat -T filename.txt

This helps in visualizing tab spaces.

Advanced Usage

1. Reading from Standard Input

cat can also read from standard input, making it useful in pipelines.

echo "Hello, World!" | cat -n

This will output:

1  Hello, World!

2. Using cat with Here Documents

You can use cat with here documents to create files with multiple lines of content.

cat <<EOF > myfile.txt
This is line one.
This is line two.
EOF

This creates myfile.txt with the specified lines.

Summary

The cat command is a simple yet powerful tool for displaying, creating, and concatenating files. Its various options allow you to tailor the output to your needs, making it an essential command for text file manipulation in Unix-like operating systems. Whether you are displaying file contents, combining files, or working with pipelines, cat is a fundamental command that enhances your ability to manage text files efficiently.

1 Comment

  1. Goodman

    The cat command reads files sequentially, displaying their content to the terminal. It concatenates multiple files if specified, allowing for viewing, combining, or redirecting output to another file.

Leave a Reply