How to Use the Cat Command in Linux

Short for concentrate, the cat command is one of the most commonly-used commands in Linux. It is used to view the content of a file, create a file or multiple files, concentrate files as well as redirect output in Terminal or files.

The general syntax of the cat command is as follow:

$ cat [OPTION] [FILE]...

In this article, I am going to show you various ways to use the cat commands that will add value and productivity to your work.

Let’s take a look.

Use cat to create a new file and add content
cat > filename

Example: Create a new file call vegetables.txt.

cat > vegetables.txt

After hitting Enter, a new file named vegetables.txt will be created, and the terminal will now await the user’s input for the file’s content.

Go on and input the content; to save and exit, hit the Control + D shortcut key.

cat command
Display file’s content with cat
cat filename

Example: Display the content of vegetables.txt.

cat vegetables.txt
cat command
Display content of multiple files with cat
cat filename_1 filename_2

Example: View both the content of fruits.txt and vegetables.txt.

cat fruits.txt vegetables.txt
cat command
Display content with line numbering with cat
cat -n filename

Example: View the content of fruits.txt, accompanied by line numbers.

cat -n fruits.txt
cat command
Copy, replace, or replicate a file’s content using cat
cat filename new_filename

Example: Duplicate fruits.txt into a new file named new_fruits.txt. If new_fruits.txt already exists, its existing content will be replaced with fruits.txt‘s content.

cat fruits.txt > new_fruits.txt
cat command

Note: The command cat foo > bar suggests that foo’s content will be copied over to bar. If you want to copy bar’s content to foo instead, use this command cat foo < bar.

Merge multiple files’ content into one with cat
cat filename_1 filename_2 > filename_3

Example: Merge/combine content of both fruits.txt and vegetable.txt into a new file named grocery.txt.

cat fruits.txt vegetable.txt > grocery.txt
cat command