“cat” command in Linux/Unix

The “cat” command is a commonly used Linux/Unix command that allows users to view the contents of a file, concatenate multiple files, and create new files. In this article, we will take a closer look at the cat command and some of its most useful options and examples.

Basic usage

The most basic use of the “cat” command is to display the contents of a file on the terminal. To do this, simply type cat followed by the name of the file you want to view:

cat file.txt

This will print the contents of 1file.txt to the terminal. If the file is large, you can use the less command to view it one page at a time:

cat 1file.txt | less

Concatenating files

One of the main uses of the cat command is to concatenate (combine) multiple files into a single file. To do this, simply specify the names of the files you want to concatenate, separated by a space:

cat file_1.txt file_2.txt file_3.txt > combined.txt

This will create a new file called combined.txt that contains the contents of file_1.txt, file_2.txt, and file_3.txt in that order.

Creating new files

You can also use the cat command to create new files. To do this, use the > operator to redirect the output of the cat command to a new file:

cat > new_file.txt

This will create an empty file called new_file.txt. You can then type the contents of the file, and press CTRL+D when you are finished to save the file.

Advertisements

Other options

There are several other options available with the cat command that can be useful in different situations. Some of the most useful options include:

-n: This option causes cat to display the line numbers of the file.

cat -n file.txt

-E: This option causes cat to display a $ character at the end of each line. This can be useful for identifying the end of each line in a file.

cat -E file.txt

-T: This option causes cat to display a ^I character at the beginning of each tab character in the file. This can be useful for identifying the presence of tab characters in a file.

cat -T file.txt

Examples

Here are some examples of how you might use the cat command in different situations:

View the contents of a file:

cat file.txt

View the contents of a file one page at a time:

cat file.txt | less

Concatenate multiple files into a single file:

cat file_1.txt file_2.txt file_3.txt > combined.txt

Create a new file and add contents to it:

cat > newfile.txt
This is the first line of the file.
This is the second line of the file.
^D

Concatenate multiple files into a single file:

cat file_1.txt file_2.txt file_3.txt > combined.txt

Display the line numbers of a file:

cat -n file.txt

Display a $ character at the end of each line in a file:

cat -E file.txt

Display a ^I character at the beginning of each tab character in a file:

cat -T file.txt

Conclusion

As you can see, the cat command is a powerful and useful tool for working with files in Linux/Unix systems. Whether you need to view the contents of a file, concatenate multiple files, or create a new file, the cat command has you covered. With the options and examples provided in this article, you should be able to use the cat command effectively in your own projects.

Advertisements

LEAVE A REPLY

Please enter your comment!
Please enter your name here