The basic syntax of cat command in Linux guide 2021

The-basic-syntax-of-cat-command-in-Linux-image

cat command would be on your fingers without any restriction or hesitation if you are going to be a Linux user, In other words, you can say “cat command is most used command in Linux”. You must know the first basic syntax of cat command in Linux.

Generally, cat utility in Linux is used to display the content of the file. I can’t stop me to use cat command at the time to peep into the file.

It is not just used only for a single purpose to see the content of the file, but you can use the cat command to create a file in Linux. I will share the basic syntax of cat tool in Linux.

The basic syntax of cat command

Content Article:

The basic syntax of cat command in Linux

We all must know about the basics fo cat command, you can use the man for displaying manual so you can know more about it.

Use the following syntax to know more:

$man cat

When you follow the above step, you will able to find out the basic syntax of cat command.

I am going to share with you, it is as follows:

cat [OPTION] [FILE]…

Use the cat command to create a file in Linux

Let’s start our first task, create a file in Linux by using cat command, It is a very simple option to perform this action:

Syntax as follows if you want to create a file inside the current working directory otherwise give the directory location where you want to create a file.

$cat > filename
$cat > /Path/to/directory/filename

I am going to create a file in the current directory in the first example and in the second example I will create a file inside /home/vijay/Documents

[[email protected] ~]$ ls 
Desktop  Documents  Downloads  Music  Pictures  Public  Templates  Videos
[[email protected] ~]$ cat file1
cat: file1: No such file or directory
[[email protected] ~]$ cat >file1
This is the file 1
I want to add something in this file later
[[email protected] ~]$ 

I the above example I have used “ls” command to see the content inside current directory.

Later I used “cat file1” to see the available content inside file1, but I got error result as you can see above.

Next I used command “cat >file1” Then I wrote 2 lines, You can write as long as you want. When you complete your writing Press “Ctrl +d” to save and exit.

Again you can use ls command

Use the cat command to add content in the file

You can use >> instead of > followed by the filename.

[[email protected] ~]$ cat >>file1
This is the second line in file1
this is the third line in file1
[[email protected] ~]$

I have added two lines again by using “cat>>file1” command. Pressed “Ctrl+d” to save and exit from file.

Use the cat command to display contents of File

As you saw “I have created a file and write some content”. But how will I check the content exist inside file1.

Yes, It is time to peep into file1. Here is the command:

$cat filename

[[email protected] ~]$ cat file1 
This is the file 1
I want to add something in this file later
This is the second line in file1
this is the third line in file1
[[email protected] ~]$

Display Line Numbers in File

I created a file with only 4 lines, but you can add multiple. Even you can imagine, a programing file has lots of lines. If you want to go on particular line, you must display the number of lines.

$ cat -n filename

[[email protected] ~]$ cat -n file1 
     1	This is the file 1
     2	I want to add something in this file later
     3	This is the second line in file1
     4	this is the third line in file1
[[email protected] ~]$ 

I am going to show you another example:

[[email protected] ~]$ cat -n /etc/passwd
     1	root:x:0:0:root:/root:/bin/bash
     2	bin:x:1:1:bin:/bin:/sbin/nologin
     3	daemon:x:2:2:daemon:/sbin:/sbin/nologin
     4	adm:x:3:4:adm:/var/adm:/sbin/nologin
     5	lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
     6	sync:x:5:0:sync:/sbin:/bin/sync
     7	shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
     8	halt:x:7:0:halt:/sbin:/sbin/halt
     9	mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
    10	operator:x:11:0:operator:/root:/sbin/nologin
    11	games:x:12:100:games:/usr/games:/sbin/nologin
    12	ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
    13	nobody:x:65534:65534:Kernel Overflow User:/:/sbin/nologin
    14	dbus:x:81:81:System message bus:/:/sbin/nologin
    15	systemd-coredump:x:999:997:systemd Core Dumper:/:/sbin/nologin
    16	systemd-resolve:x:193:193:systemd Resolver:/:/sbin/nologin
    17	tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
    18	polkitd:x:998:996:User for polkitd:/:/sbin/nologin
    19	geoclue:x:997:995:User for geoclue:/var/lib/geoclue:/sbin/nologin
    20	rtkit:x:172:172:RealtimeKit:/proc:/sbin/nologin
    21	pulse:x:171:171:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin
    22	qemu:x:107:107:qemu user:/:/sbin/nologin
    23	usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin
    24	unbound:x:996:991:Unbound DNS resolver:/etc/unbound:/sbin/nologin
    25	rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
    26	gluster:x:995:990:GlusterFS daemons:/run/gluster:/sbin/nologin
    27	chrony:x:994:989::/var/lib/chrony:/sbin/nologin
    28	libstoragemgmt:x:993:987:daemon account for libstoragemgmt:/var/run/lsm:/sbin/nologin
    29	pipewire:x:992:986:PipeWire System Daemon:/var/run/pipewire:/sbin/nologin
    30	saslauth:x:991:76:Saslauthd user:/run/saslauthd:/sbin/nologin
    31	setroubleshoot:x:990:985::/var/lib/setroubleshoot:/sbin/nologin
    32	dnsmasq:x:984:984:Dnsmasq DHCP and DNS server:/var/lib/dnsmasq:/sbin/nologin
    33	radvd:x:75:75:radvd user:/:/sbin/nologin
    34	clevis:x:983:982:Clevis Decryption Framework unprivileged user:/var/cache/clevis:/sbin/nologin
    35	cockpit-ws:x:982:980:User for cockpit-ws:/nonexisting:/sbin/nologin
    36	sssd:x:981:979:User for sssd:/:/sbin/nologin
    37	colord:x:980:978:User for colord:/var/lib/colord:/sbin/nologin
    38	gdm:x:42:42::/var/lib/gdm:/sbin/nologin
    39	rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
    40	gnome-initial-setup:x:979:977::/run/gnome-initial-setup/:/sbin/nologin
    41	sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
    42	avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
    43	tcpdump:x:72:72::/:/sbin/nologin
    44	vijay:x:1000:1000:Vijay Kumar:/home/vijay:/bin/bash
    45	vboxadd:x:978:1::/var/run/vboxadd:/bin/false
[[email protected] ~]$ 

Display $ at the End of File

Sometime, You are writing a long paragraph without hitting enter button.

$ sign will be displayed at the end of line as well as gap between two paragraphs

[[email protected] ~]$ cat >file2
Hi friends, how are you?

Hey, I am fine.
I hope you are doing well.

How's your training and study going on?

[[email protected] ~]$ cat -e file2 
Hi friends, how are you?$
$
Hey, I am fine.$
I hope you are doing well.$
$
How's your training and study going on?$
$
[[email protected] ~]$

First, I have created a new file with name file2 by using cat command later I have displayed same file with $ end of line.

Display Tab-separated Lines in File

You can use cat command followed by “T” options to display tab seperated lines.

Syntax of cat command for displaying tab separated lines are below:

$cat -T filename

View Contents of Multiple Files by this command

Do you want to view multiple files at once? then this section is very useful.

You can use following command to view multiple files:

cat firstfilename; cat secondfilename; cat thirdfilename

[[email protected] ~]$ cat file1; cat file2
This is the file 1
I want to add something in this file later
This is the second line in file1
this is the third line in file1
Hi friends, how are you?

Hey, I am fine.
I hope you are doing well.

How's your training and study going on?

[[email protected] ~]$ 

Copy content from one file to another

Cat command in linux is used to display content on the screen, it happend due standard output.

We can redirect standard output into file by using “>” character.

So you can copy one file to another file by using this command.

See the example below:

cat file1 > newfile1

[[email protected] ~]$ cat file1 >newfile1
[[email protected] ~]$ cat newfile1 
This is the file 1
I want to add something in this file later
This is the second line in file1
this is the third line in file1
[[email protected] ~]$ 

If you want to add content inside existing file then use “>>” operator followed by newfilename

$cat file1 >>newfilename

Copy content of multiple files in a single file

It is similar display multiple files first as output and redirect to another newfile2

$cat file1 file2; >newfile2

[[email protected] ~]$ cat file1 file2 >newfile2
[[email protected] ~]$ cat newfile2 
This is the file 1
I want to add something in this file later
This is the second line in file1
this is the third line in file1
Hi friends, how are you?

Hey, I am fine.
I hope you are doing well.

How's your training and study going on?

[[email protected] ~]$ 

Sorting Contents of Multiple Files in a Single File

You can use sort pipe operator to sort output of multiple file’s content.

Example:

$cat file1 file2 | sort >newfile3

[[email protected] ~]$ cat file1 file2 |sort >newfile3
[[email protected] ~]$ cat newfile3 



Hey, I am fine.
Hi friends, how are you?
How's your training and study going on?
I hope you are doing well.
I want to add something in this file later
This is the file 1
This is the second line in file1
this is the third line in file1
[[email protected] ~]$ 

Conclusion

This article shows the basic syntax of cat command in Linux that may help you to explore cat command. You have learnt using basic syntax of cat tool in Linux to create file and edit files.

As I have told you already You may refer man page of cat command if you want to know more options.

Please share it if you find this article useful through our comment box below

If you like our content, please consider buying us a coffee.
Thank you for your support!