Linux cat Command Examples

Filed Under: UNIX/Linux

One of the most commonly used commands in Linux is the cat command. Short for concatenate, Linux cat command allows users to perform the following operations.

  • View contents of a file
  • Redirect output of files
  • Create files and input content

In this tutorial, we take a look at cat command with a few examples of how it can be used.

Syntax:

$ cat [option] [file]

1. Display content of a file

To view a file’s content, use the syntax.

$ cat file-name

For example to view the file /etc/passwd run:

$ cat /etc/passwd

Sample output

Cat Command view a file's content

2. Viewing Multiple Files

In addition to viewing a single file, you can view multiple files in one command using the syntax as shown below.

$ cat file1 file2

For example, we have 2 files with the following content.

test files

To view the files simultaneously, run:

$ cat file1.txt file2.txt

Sample output

view two files using cat command

3. Number the lines of content in a file

If you want to number the lines of content in a file, use the -n as shown below.

$ cat -n filename

For example,

$ cat -n /etc/passwd

Sample Output

cat -n option

4. Use cat command with less or more options

The more or less parameters enable a user to easily scroll down long content that scrolls down very fast, making it difficult to follow . The synatx is quite simple.

$ cat filename | less

less parameter in cat

OR

$ cat filename | more

more parameter in cat

5. Create files using cat command

To create a file, use the > sign as shown in the syntax below.

$ cat > filename

Next, type your desired text input and at the very end, press CTRL + D to exit.

The text will be written onto the text file. You can verify this using the cat command as seen in the first example.

create a file with cat

6. View multiple files at once

Assume you have several files that you’d want to view at a go, use the colon mark ; to separate the files being displayed as shown.

cat file1.txt; cat file2.txt; cat file3.txt; cat file4.txt ...

7. Appending standard output to another file

You can append content onto a file using the double greater sign redirection operator >> as shown.

 cat file1 >> file2

This appends the contents of file 1 onto file 2.

Sample output

Appending Contents Of File1 To File2

8. Redirect output of a file onto another file using the redirection operator

You can redirect standard output of a file into a new file with ‘>‘ (greater than) symbol.

cat file1 > file2

Redirect Output From One File To Another

As seen in the output above, extra caution should be taken when doing this because this operation overwrites the contents of the destination file.

To redirect multiple outputs into a single file use the syntax

$ cat file1 file2 file3 file4 > file5

Conclusion

Linux cat command is a simple utility to view file contents, create a text file with some content, and append data to a text file.

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages