How to save file in Linux using cat command

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.

ADVERTISEMENTS

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.

🐧 If you liked this page, please support my work on Patreon or with a donation.
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS

Comments on this entry are closed.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.