Linux Cat Command With Examples


Concatenate or simply cat is very useful command in Linux and Unix systems. Cat has the ability to create files, concatenate files or redirect output. In this tutorial, we will look at different useful examples for daily usage for system administrators.

Syntax

Syntax of Linux cat command is standard as most of the Linux tools.

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

Man and Help

Man page can get like below. As we can see cat command provides a lot of features.

$ man cat
Man and Help
Man and Help

Print Content

The most popular way to use cat command is printing contents of a file. cat command will read given file line by line and print to the terminal or shell accordingly. In this example, we will print file named myshell.sh  which is a bash script.

$ cat myshell.sh
Print Content
Print Content

Print Content of Multiple Files

cat command accepts multiple parameters like the file name to print all contents of provided files. In this example, we will print file contents named myshell.sh  and myscan in a row.

$ cat myshell.sh myscan

Create File

An empty file will be created when cat output is directed to the file name like below. After redirect control character should be sent to close the file.

$ cat > new_file

And then

Ctrl+d
Create File
Create File

Use With More and Less Commands

While working more than one text file with more or less commands cat provides a practical solution to pipe all files contents.

$ cat myshell.sh myscan | less

Display Line Numbers

While printing file contents with cat the line numbers can be shown to direct line specification. If we need specific line numbers it will be very helpful especially using with grep  command. We will use -n as an option to enable line number printing.

$ cat -n myshell.sh
Display Line Numbers
Display Line Numbers

Display $ as End Of  Line

If we want to put delimiter at the end of each line we can use -e option. This will put $ to the end of each line.

$ cat -e myshell.sh
Display $ End Of Line
Display $ End Of Line

Display Tab Separated Lines

There is also an option to separate tabbed lines with -T parameter like below. ^|  will be used as a tab sign to show as characters.

$ cat -T myshell.sh
Display Tab Separated Lines
Display Tab Separated Lines

Redirect With Cat

The redirect will be used to provide standard output content to the next command standard input. Cat prints file content and following file will get content of the previous file.

$ cat myshell.sh > yourshell.sh

Keep in mind this operation will overwrite all yourshell.sh content with new content

LEARN MORE  Linux tail Command Tutorial With Examples

Append To File With Cat

While redirecting outputs previous content will be overwritten. to prevent overwrite and add new content to the existing content >> operator can be used.

$ cat myshell.sh >> yourshell.sh
Append To File With Cat
Append To File With Cat

Redirect Standard Input

Another useful usage of the cat command is redirecting standard input. Generally, the content of a file is the standard input.

$ cat < myshell.sh
Redirect Standard Input
Redirect Standard Input

Concatenate Multiple File Into One File

One of the funniest usage of the cat command is adding multiple files into a single file with a single shot.

$ cat myshell.sh yourshell.sh > ourshell.sh
Concatenate Multiple File Into One File
Concatenate Multiple File Into One File

Use With Sort

Using cat with sort command is another useful scenario. All content of files are piped to the sort and sort will sort all content accordingly.

$ cat myshell.sh yourshell.sh | sort
Use With Sort
Use With Sort

Leave a Comment