How To Assemble Files With Cat Command In Linux/Unix?


Linux or Unix provides cat command that can be used to create, view, and concatenate files. It reads the content of a file and prints it on the terminal. As I already discussed to split a large file into multiple smaller files in an article. We can use the cat command to assemble or concatenate these files. In this article, we will discuss creating, viewing, and concatenating files with the cat command.

Syntax of cat command

Following is the syntax of cat command –

cat [options] filename

Or use –

cat > filename

Or use-

cat file1 file2 >file3

You see the details of cat command with the options that can be used with it by using the following command-

man cat

How to create a new file using the cat command?

To create a text file in Linux, use the following command in your terminal –

cat >filename.txt

Now once the above command gets executed, you can start entering your text in the terminal.

And when you are done with writing text, you need to press ctrl+d to save the content of the file.

How to display the content of a text file?

Cat command is frequently used in Linux to display the content of a text file in the terminal. Now use the following command to show the content of a file –

cat filename.txt

 

How to join two text files in Linux using cat command?

You can use the cat command for joining or concatenating files in Linux. Here we will join two files filename.txt and filename1.txt. You can see the content of these two files by using-

cat filename.txt

cat filename1.txt

To join these files, you can use the following command –

cat filename.txt filename1.txt > filename2.txt

Now if you display the content of filename2.txt then you will see that the text of both the files is merged into one file –

cat filename2.txt

How to join binary files using cat command?

You can also concatenate binary files using cat command. Here we will join the pieces of a large backup.tar.gz file. The pieces of this file are created using the split command. You can see the parts of the backup.tar.gz file in the following image-

To combine these parts into original_backup.tar.gz we will use the following command in the terminal –

cat backup* > original_backup.tar.gz

It can take a few minutes to combine these files. Once it gets done, run the following command to see the original_backup.tar.gz file –

ls -lh

Conclusion

In this article, you have learned to create, view, and concatenate files in Linux or Unix. Now if you have any queries related to this topic then please write it in the comments below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.