# Linux Cat Command

The ‘cat’ command is considered to be the most widely used and universal tool. They are used frequently. This command can be used to display the content of a file, or copy content from one file to another, or display the line number, or concatenate the contents of multiple files, or display $at the end of the line, etc. ## Linux cat command: to display file content: We can use the ‘cat’ command to display the content of a file on the screen. ### Syntax: cat <fileName> ### Example: cat jtp.txt [email protected]:~$ cd Desktop/ [email protected]:~/Desktop$[email protected]:~/Desktop$ cat jtp.txt this is w3schools thankyou thankyou thankyou a b c d e f g h i j k l mmmmm nnnnn

As we can see in the above example, we used “cat jtp.txt” command to display the file ‘jtp.txt’.

Note: If you want to display the content of multiple files all at once then you have to type file names in a single line such as “cat file1 file2 file3… fileN.

 Option Function cat > [fileName] It is used to create a file. cat [oldfile] > [newfile] This command copies the content from an old file to a new file. cat [file1 file2 and so on] > [new file name] This command is used to concatenate the contents of multiple files into one. cat -n/cat -b [fileName] To show line numbers. cat -e [fileName] It displays the $character at the end of each line. cat [fileName] < ## Linux cat command (to create a file): We can also use this ‘cat’ command to create a new file by using greater than sign (>). ### Syntax: cat > <file name> ### Example: cat > w3schools [email protected]:~/Desktop$ cat > w3schools welcome to w3schools let's learn linux have a great day ahead. [email protected]:~/Desktop$[email protected]:~/Desktop$ cat w3schools welcome to w3schools let's learn linux have a great day ahead. codes@w3schools:~/Desktop$ Here, in the above example we created a new file called “w3schools”. Now let us understand how we created it. First, we type the “cat > w3schools” command and then press ‘enter’. You will be then automatically directed to the next line. You can press ‘enter’ after every line as you will be directed to the next line. To save your file, just go to the next line and press ‘ctrl+d’. ## To Append the Content of A File: When you use the ‘cat’ command with double greater than sign (>>) it will append something in your already existing file that means it will add something in the last of a file. ### Syntax: cat >> (file name) ### Example: cat >> w3schools [email protected]:~/Desktop$ cat >> w3schools a new line will be added to the end of the file. codes@w3schools:~/Desktop$[email protected]:~/Desktop$ cat w3schools welcome to w3schools let's learn linux have a great day ahead. a new line will be added to the end of the file. [email protected]:~/Desktop$ In the above example, a new line is added at the end of the file w3schools ‘. As we pass “cat >> w3schools “ command, type the lines as many numbers as you want to add in the file. Then save the file by pressing ‘ctrl + d’. ## Linux cat command (to copy file): We can also use the ‘cat’ command to copy the content of one file into another file. ### Syntax: cat (older file name) > (newer file name) ### Example: cat combo > combo2 [email protected]:~/Desktop$ cat combo hello everyone at w3schools [email protected]:~/Desktop$[email protected]:~/Desktop$ cat combo>combo2 [email protected]:~/Desktop$[email protected]:~/Desktop$ cat combo2 hello everyone at w3schools [email protected]:~/Desktop$ We copied the content of file ‘combo’ into file ‘combo2’ by using the command “cat combo > combo2” in the above example. ## Linux cat command (to concatenate files): We can easily concatenate the contents of multiple files in a single new file by using the ‘cat’ command. ### Syntax: cat <filename1> <filename2>.... > <newFilename> ### Example: cat file1 file2 file3 > combo [email protected]:~/Desktop$ cat file1 Hello [email protected]:~/Desktop$[email protected]:~/Desktop$ cat file2 everyone [email protected]:~/Desktop$[email protected]:~/Desktop$ cat file3 at w3schools [email protected]:~/Desktop$[email protected]:~/Desktop$ cat fil1 file2 file3 >combo [email protected]:~/Desktop$[email protected]:~/Desktop$ cat combo hello everyone at w3schools

Here, we combined the content of three files “file1, file2, and file3” into a new single file “combo” by using the command “cat file1 file2 file3 >combo”.

Carefully see the content of three separate files and then match them with the content of a new concatenated file “combo”.

## To Insert A New Line:

When we use a hyphen (-) with the cat command for concatenating multiple files, then a new line can be inserted.

### Syntax:

cat - <filename1> <filename2>. . . . > <new filename>

### Example: cat – file1 file2 file3 >combo

[email protected]:~/Desktop$cat -file1 file2 file3>combo This is a combo of all three files. codes@w3schools:~/Desktop$ [email protected]:~/Desktop$cat combo This is a combo of all three files. hello everyone at w3schools [email protected]:~/Desktop$

In the above example, we are inserting a new line at the beginning of the new concatenated file while concatenating file1, file2 and file3 using the command “cat – file1 file2 file3 >combo”.

Note: The new line can be inserted at the beginning only.

## Linux cat -n command (to display line numbers):

The ‘cat -n’ option will help you number your lines as it displays line numbers before each line in a file.

### Syntax:

cat -n <fileName>

### Example: cat -n jtp.txt

[email protected]:~/Desktop$cat -n jtp.txt 1 This is w3schools. 2 thanku 3 thanku 4 a 5 b 6 c 7 d 8 e 9 f 10 g 11 h 12 i 13 j 14 k 15 l 16 [email protected]:~/Desktop$

As seen in the above example, a number is displayed before every line of the file ‘jtp.txt’ after we pass the command “cat -n jtp.txt”.

## cat -b (file name):

It removes the empty lines from the file.

### Syntax:

cat -b (file name)

### Example: cat -b jtp.txt

[email protected]:~/Desktop$cat -b jtp.txt 1 This is w3schools. 2 thanku 3 thanku 4 a 5 b 6 c 7 d 8 e 9 f 10 g 11 h 12 i 13 j 14 k 15 l [email protected]:~/Desktop$

In the previous example, there was an empty line numbered 16 which is removed in the above example after we pass the command “cat -b jtp.txt”.

## Linux cat -e command (to display $): A ‘$’ sign is displayed at the end of every line by using the ‘cat-e’ option.

### Syntax:

cat -e <fileName>

### Example: cat -e program

[email protected]:~/Desktop$cat -e program This is linux$ you are learning linux $at jtp$ thank you $abc$ def $[email protected]:~/Desktop$

Look at the above example, some lines have spaces only and other also included spaces but a user won’t be able to differentiate the white space present at the end of each line. So we can use this “cat -e program” command which can put the $sign at the end of every line and those including spaces as well. ## Linux cat command (as an end marker): This ‘cat << EOF ‘ option puts an end marker at the end of a file. This command is known as here directive and the file content will be saved where the end marker is given. The file can also be saved by pressing ‘ctrl + d ‘. It works just like the end marker. Note: We can use any other word than ‘EOF’ for the end marker. ### Syntax: cat << EOF ### Example: cat > exm.txt << EOF [email protected]:~$ cat > exm.txt <<EOF >hello >this is w3schools >welcome all > EOF [email protected]:~$cat > exm.txt hello this is w3schools welcome all [email protected]:~$

In the above example, we are creating a ‘exm.txt’ file by using ‘EOF’ as the end marker after passing the command “cat > exm.txt << EOF”.

## Linux Cat Filters

It just moves stdin to stout when the cat command is used inside pipes.

### Syntax:

cat <fileName> | cat or tac | cat or tac |. . .

### Example: cat weeks.txt | tac | cat | cat | tac

[email protected]:~$cat weeks.txt sunday monday tuesday wednesday thursday friday Saturday [email protected]:~$ cat weeks.txt | tac | cat | cat | tac sunday monday tuesday wednesday thursday friday Saturday [email protected]:~\$

In the above example, we are passing the output of one ‘cat’ or ‘tac’ command to another as input.