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.

Linux cat command syntax

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

Showing file contents

cat /etc/hosts

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

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/

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

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

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


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

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 1 comment so far... add one ↓

CategoryList of Unix and Linux commands
Disk space analyzersdf β€’ ncdu β€’ pydf
File Managementcat β€’ cp β€’ mkdir β€’ tree
FirewallAlpine Awall β€’ CentOS 8 β€’ OpenSUSE β€’ RHEL 8 β€’ Ubuntu 16.04 β€’ Ubuntu 18.04 β€’ Ubuntu 20.04
Network UtilitiesNetHogs β€’ dig β€’ 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

Comments on this entry are closed.

Comments are closed. Still have questions? Post it on our forum