How to save file in Linux using cat command

last updated in Categories , ,

How do I use cat command in Linux to save file? How can I create a new file using the cat command?

The cat command is one of the many commands in Linux. The cat is an acronym for concatenate files. In other words, it can read files, print files on screen, concatenate files, and save or append data to files. However, a most common use of cat is to display the contents of files on your screen. In this quick guide, we will learn how to use the cat command to save the file.


Linux cat command syntax

The syntax is:
cat filename
cat file1 file2 file3
cat [options] filename
cat > filename
cat >> filename

Showing file contents

Run:
cat /etc/hosts

cat show file contents
Displaying /etc/hosts file contents in your terminal

Want to redirect contents of file to another?

Execute the following command to copy the contents of input.txt to output.txt using the > operator :
cat input.txt > output.txt
Of course, you can send output of any command to file too:
ls > dirs.txt
cat dirs.

Finally, one can use the >> operator to append the contents of files or command output to files too:
cat foo.txt >> bar.txt
date >> bar.txt
cat bar.txt

How to save file using cat command in Linux

Let us see how to 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.

Step 1 – Create a new file named todays.txt using cat

We are going to create a new file, use the cat command as follows:
cat > todays.txt
Press the ENTER key. Type data you want. For example:
This is a test
Today's date is Feb/13/2003

Step 2 – Press the CRTL+D to save the file.

Simply hit the CRTL+D to save the file created by the cat command. To display, enter:
cat todays.txt

Linux save file using cat command
Gif 01: Creating and saving small files with cat command

Step 3 – Creating, saving and appending data to files

Please note that if a file named todays.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 todays.txt as follows:
cat >> todays.txt

Other cat command examples

Let us see how to print line numbers:
cat -n ~/bin/gif
Sample outputs:

     1	#!/bin/bash
     2	/Applications/LICEcap.app/Contents/MacOS/licecap

To see non printing characters such as $ (end of line), tabs and others, run:
cat ~/bin/is_mounted.sh
Sample outputs:

#!/bin/bash$
# Purpose: Mount glusterfs at boot time$
# Author: Vivek Gite$
# --------------------------------------$
p='gfs01:/gvol01'$
$
mount | grep -wq "^${p}"$
$
if [ $? -ne 0 ]$
then$
^I/bin/mount -t glusterfs "$p" /sharedwww/$
fi$

See cat command man page online here or type the following man command:
man cat

Conclusion

We learned how to use the cat command to display or create a new file on Linux.

ADVERTISEMENTS

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

Start the discussion at www.nixcraft.com

Historical Comment Archive

1 comment

    Still, have a question? Get help on our forum!