10 must know usage of cat command in Linux/Unix

Advertisement

In this tutorial we will explore cat command used in Linux and Unix nodes. Now the seasoned Linux Administrator would already be familiar with this command but I hope to add some more command example usage which may be new to even an expert one. But mostly this article is for new comers and beginners to Linux and Unix platform.

 

Overview on cat command in Linux/Unix

Now most of us would be familiar with cat command as it's usage to view the contents of the file. But I normally don't prefer cat command for that purpose as it is actually meant to be used to concatenate files and print on the standard output.

The general syntax to use cat command in Linux and Unix is:

cat [OPTION]... [FILE]...

We will explore the different usage of this command with different examples to help you understand.

 

1. Print content of file on STDOUT

This is the most basic usage of cat command wherein you can print the content of any file on STDOUT console without using any extra arguments:

10 must know usage of cat command in Linux/Unix

 

2. Creating a new file

I am giving this example as we are learning the various possible usage of cat command. But to be honest I never used it to create files but yes you can use it to "create files".

10 must know usage of cat command in Linux/Unix

The syntax would be:

Advertisement
cat >FILENAME

where you can replace FILENAME with the name of the file which you want to create. This will open the console editor where you can write your data into the file and once you are done, to save press ctrl+d

 

3. Concatenate multiple file's content

You can provide multiple file names with their path to print the content of all the respective files at once. Here I am creating two new files with some content and then using cat command to print the output from both the files.

10 must know usage of cat command in Linux/Unix

Now let us use cat command to concatenate the content from two files into another third file:

10 must know usage of cat command in Linux/Unix

We could have also used to append the content into any existing file. So I will re-write the content from file1 and file2 into file3 but this time I will append the data using >>:

~]# cat file1 file2 >> file3

~]# cat file3
This is from file1
This is from file2
This is from file1
This is from file2

Similarly you can concatenate multiple files.

 

4. Redirect output from a file as standard input for another file

The - tells cat command to read from the standard input so if we have a requirement to pass a content from one file as an input to another file then we can use following syntax:

cat FILE1 - FILE2

Here we mean output the FILE1's content then standard input and then FILE2's content

Let's take some example:

Advertisement

Here I have an input_file with following content:

~]# cat input_file
1
2
3
4
5

And I have another shell script which expects input from this input_file:

~]# cat script.sh
#!/bin/bash

input="$1"
for i in "$input"; do
  echo $i
done

So we can use them in this format:

 ~]# cat input_file - script.sh
1
2
3
4
5
^C<-- The screen waits at this stage

The downside of this method is that the console will wait at the end of input_file waiting for user interaction.

We could also use different approach here using loops with cat, such as:

 ~]# cat input_file | while read line; do echo $line; done
1
2
3
4
5

Using pipe also we can transfer the output from one command to another which further we utilised using while loop. Additionally we could have also used xargs:

~]# cat input_file | xargs -L1 echo
1
2
3
4
5

So it depends on your requirement and you can choose the best preferred method

 

5. Using cat command in loops

Now this is something which we mostly use in the production environments. Assuming we want to work on the contents of a file to make some modification then we can use for loop or while loop to iterate through each line of a file and then perform the action.

Here I have a script which iterates through each line of /etc/passwd and collects the username and UID:

 ~]# cat script.sh
#!/bin/bash

# using cat we print the output of /etc/passwd
# and using for loop we read line by line of the STDOUT
for i in $(cat /etc/passwd); do
  USERNAME=$(echo $i | cut -d: -f1)
  myUID=$(echo $i | cut -d: -f3)
  echo "username: $USERNAME"
  echo "UID: $myUID"
done

Output from this script:

10 must know usage of cat command in Linux/Unix

 

6. Detect trailing whitespace at the end of line in a file

We can also use cat command to detect or find the lines of a file which has trailing whitespace character. I had a similar requirement one time where the script will fail to execute if it's input file had extra whitespace at the end of the line. Now this input file had 1000s if line, so it is not possible for me to check each line one by one right? So cat command to the rescue.

We can use -E or --show-ends arguments with cat command to add $ (dollar sign) at the end of the line which can show us trailing whitespace (if any). Here I have a file with following content:

10 must know usage of cat command in Linux/Unix

Now I have intentionally added some extra space at the end of line 3rd and 4th

10 must know usage of cat command in Linux/Unix

Let's use cat command to find these culprit lines:

~]# cat --show-ends file1 | grep '[[:blank:]]\$'
here I added extra space at the end of line $
here I added multiple extra whitespaces at the end of line  $

So we have those lines with trailing whitespace character.

 

7. Detect TAB character in a file

Similar to our last example, we can also use cat command to detect if there are any TAB character used in a file using --show-tabs or -T argument.

~]# cat file1
first line
second        line
here I have used        TAB
here I added multiple TAB
fifth line

To grep for the lines with TAB we can grep for ^I pattern:

~]# cat --show-tabs file1 | grep '\^I'
here I have used^ITAB
here I added multiple TAB^I^I

 

8. Display line numbers

There are multiple ways to show line numbers for the file's content. But we will only explore cat command here, we can use --number or -n argument to print the line numbers of the file:

10 must know usage of cat command in Linux/Unix

 

9. Display line numbers for non-blank lines only

The previous example using --number or -n would display line number even if the respective line is blank with no text. If your requirement is to count the number of non-blank lines in a file then you can use cat with --number-nonblank or -b argument.

For example, here I have intentionally added a blank line in my /etc/hosts file. If I try to get the number of lines using -n or --number then it shows there are total 7 lines:

~]# cat --number /etc/hosts
     1  127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
     2  ::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
     3  192.168.43.48   controller     controller.example.com
     4  192.168.43.49   worker-1       worker-1.example.com
     5
     6  192.168.43.50   worker-2       worker-2.example.com
     7  192.168.43.50   sample.example.com

But to get the number of non-blank lines we use -b or --number-nonblank:

10 must know usage of cat command in Linux/Unix

 

10. Replace multiple empty lines with single empty line

If your file has multiple empty/blank lines and you wish to search and replace all empty lines with single empty line then you can use -s or --squeeze-blank with cat command to suppress repeated empty output lines.

For example here I have added multiple empty lines in my /etc/hosts file:

~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.43.48   controller     controller.example.com
192.168.43.49   worker-1       worker-1.example.com
<-- Empty Line 1-->
<-- Empty Line 2-->
<-- Empty Line 3-->
192.168.43.50   worker-2       worker-2.example.com
192.168.43.50   sample.example.com

Let's replace repeated empty lines with single empty line:

10 must know usage of cat command in Linux/Unix

 

Summary

In this article we explored different usage of cat command in Linux and Unix used by professionals with different examples. This is a very basic command but at times it can be confusing when you are working with redirection. But it can again come in very handy to get line numbers, detect TAB or trailing whitespaces etc when working with automation. But the original idea behind cat command was to concatenate which we used in Example 3 of this article.

 

References

I have used below external references for this tutorial guide

cat: Concatenate and write files

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Leave a Comment