• Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate
TecNStuff
Menu
  • Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate

Basic Cat Command Examples in Linux

Written by Admin, Updated On May 7, 2019
cat, linux commands, terminal
Linux Cat Command

The cat command stands for “concatenate” and it is widely used in Linux OS. Using it you can read, write, concatenate files and redirect output to terminal. Commonly, cat is most used to display the contents of single or multiple text files, create new files, combine files, append content from one file to another file.

Basic Syntax of cat Command#

First of all you should know the syntax of cat command before going to use it. Below is the basic syntax of cat command:

cat [OPTIONS] [FILE_NAMES]

Where,

  • OPTIONS – You can specify the options. Get all available options using cat –help.
  • FILE_NAMES – Specify single or multiple file names.

Display Contents of File#

The cat command is commonly used to read contents of files. Below example will show the contents of the /etc/timezone file:

cat /etc/timezone
Output
Etc/UTC

Display Contents of Multiple Files#

You can view content of multiple files using below command:

cat file1.txt file2.txt

Redirect Contents of File#

You can store output to a file instead of displaying to the screen. Below command will copy content from first file to second file:

cat file1.txt > file2.txt

Above command will create a file if second file does not exists. If it is available then it will overwrite the content of file.

Appending Output with Redirection Operator#

You can append contents of one to another file without overwrite using below command:

cat file1.txt >> file2.txt

Redirecting Standard Input with Redirection Operator#

When you want to take input from second file to terminal then you can use standard input '<' (less than) symbol as given below:

cat < file1.txt

Create a File with Cat Command#

It is very easy to create a file using cat command. To create file using cat command you just have to use redirection operator and the name of the file. Hit the Enter key and now you can type whatever you wants to write. Save the file using Ctrl + D.

cat > file.txt

In above example we are creating a file name with file.txt. If file with that name exists then it will be overwritten otherwise it will create a new file.

Display Line Numbers#

If you want to display file content with line number then you can use -n option with cat command as below:

cat -n file1.txt
Output
1 This is Line Number One
2 This is Second Line Number

Display Tab separated Lines in File#

You can use -T argument to differentiate tabs and spaces:

cat -T /etc/hosts
Output
127.0.0.1^Ilocalhost
127.0.1.1^Ilocal.tecnstuff

The TAB characters will be shown as ^I.

Display $ at the End of File#

You can display line ending with '$' also can display if any gap between paragraphs. This options is useful to squeeze multiple lines in a single line.

cat -e /etc/lsb-release
Output
DISTRIB_ID=Ubuntu$
DISTRIB_RELEASE=18.04$
DISTRIB_CODENAME=bionic$
DISTRIB_DESCRIPTION="Ubuntu 18.04.2 LTS"$

Concatenating Multiple Files in a Single File#

When you pass more then one file name as arguments with cat command then contents of files will be concatenated. It will read file as given sequence and will append to display same.

In below command it will read the contents of file1.txt and file2.txt and display the result in the terminal:

cat file1.txt file2.txt

You can concatenate the contents of file file1.txt and file2.txt and store them to another file using redirection operator as below:

cat file1.txt file2.txt > file3.txt

If the file is not present then it will be created otherwise it will be overwritten.

Conclusion#

Finally, you have learned how to use the Linux cat command.

If you have any question or feedback, feel free to leave a comment.

If our content helps you, please consider buying us a coffee

Thank you for your support.

Share On
Share on Facebook
Share on Twitter
Share on Reddit
Share on Tumblr
 Previous Article How to Install phpMyAdmin with Nginx on Ubuntu 18.04
Next Article   Most Used Nginx Commands in Linux System

Related Posts

  • How to Install SSH Keys on Ubuntu 22.04

    How to Set up SSH Keys on Ubuntu 22.04

    January 7, 2023
  • How to Install Fail2ban on Ubuntu 22.04

    How to Install and Configure Fail2ban on Ubuntu 22.04

    December 5, 2022
  • How to Enable SSH on Ubuntu 22.04

    How to Enable SSH on Ubuntu 22.04

    December 1, 2022

Leave a Reply Cancel reply

DigitalOcean Referral Badge

Popular Posts

  • How to Install Microsoft Edge Browser on Ubuntu 22.04
    How to Install Microsoft Edge Browser on Ubuntu 22.04 March 14, 2023
  • How to Install Ruby on Ubuntu 22.04 LTS
    How to Install Ruby on Ubuntu 22.04 LTS February 27, 2023
  • How to Install LEMP Stack on Ubuntu 22.04
    How to Install LEMP Stack on Ubuntu 22.04 March 18, 2023
  • How to Install Set Up Apache Virtual Hosts on Ubuntu 22.04
    How to Set Up Apache Virtual Hosts on Ubuntu 22.04 March 2, 2023
  • How to Install MariaDB on Debian 11 Bullseye
    How to Install MariaDB on Debian 11 Bullseye March 8, 2023
© 2020 TecNStuff All rights reserved. This website is using and storing cookies on your browser. By using this website you agree our Privacy Policy.  Follow us -  Twitter | Facebook