×



Need to use cat command in Linux to save file ? Need to create a new file using the cat command?

This guide is for you.

The 'cat' [short for “concatenate“] command is one of the most frequently used commands in Linux and other operating systems. The cat command allows us to create single or multiple files, view contain of file, concatenate files and redirect output in terminal or files.
Here at LinuxAPT, as part of our Server Management Services, we regularly help our Customers to resolve Linux related issues.
In this context, we shall look into how to use the cat command to save the file.

More about the cat command in Linux ?

As earlier stated, Cat, which is short for concatenate, is one of the most commonly used commands in Linux and other Unix-like operating systems. The cat command allows us to create single or multiple files, view file inclusions, concatenate files and redirect output in a terminal or file.
The cat syntax goes like this:

cat filename
cat file1 file2 file3
cat [options] filename
cat > filename
cat >> filename

How to display the contents of a file in Linux?

To display the contents of a file in Linux, execute the command for instance (hosts);

cat /etc/file_name

Where file_name represents the name of the file.
For example, to see the contents of the hosts file, execute;

cat /etc/hosts

How to redirect the contents of file to another ?

To copy the contents of a file (input.txt) to another file (output.txt), you can use the > operator as shown below:

cat input.txt > output.txt 

Also, you can send output of any command to file as well with the command:

ls > dirs.txt
cat dirs.

Additionally, can use the >> operator to append the contents of files (foo.txt, bar.txt, ) or command output to files as well:

cat foo.txt >> bar.txt
date >> bar.txt
cat bar.txt


How to save file using cat command in Linux?

Here, we will make or create a file using cat command. It is recommended that you use a cat command when you need to create tiny files as it easier using a text editor such as nano or vim.
Follow the steps below;

1. Create a new file named todays.txt using cat
We shall create a new file (file1.txt), using the cat command as follows:

cat > file1.txt

Then, Press the ENTER key and also enter the data you want as shown below:
This is a test file
Today's date is Jan/03/2021


2. Press the CRTL+D to save the file
To do this, simply hit the CRTL+D button to save the file created by the cat command. Then to display, execute:

cat file1.txt

3. How to Create, Save and append data to the file ?
Please note that if a file named file1.txt is already existed, it will be overwritten by the cat. Use the >> shell operator to append the data/text to an existing file named file1.txt as follows:

cat >> file1.txt

[Need to perform Ubuntu Linux related Installations and tasks? We are available to help you today. ]


Conclusion


More Linux Tutorials

We create Linux HowTos and Tutorials for Sys Admins. Visit us on IbmiMedia.com

Also for Tech related tips, Visit forum.outsourcepath.com or General Technical tips on www.outsourcepath.com