Concatenate or simply cat is very useful command in Linux and Unix systems. Cat have the ability to create files, concatenate files or redirect output. In this tutorial we will look different useful examples for daily usage for system administrators.
Syntax of Linux cat command is standard like most of the Linux tools.
cat [OPTION]... [FILE]...
Man and Help
Man page can be get like below.
$ man cat
The most popular way to use cat command is printing contents of a file.
$ cat myshell.sh
Print Content of Multiple Files
cat command accepts multiple parameters as file name to print all contents of provided files.
$ cat myshell.sh myscan
An empty file will be created when cat output is directed to the file name like below. After redirect control character should be send to close file.
$ cat > new_file
Use With More and Less Commands
While working more than one text file with more or less commands cat provides 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.
$ cat -n myshell.sh
Display $ End Of Line
Cat provides $ as end of line separator.
$ cat -e myshell.sh
Display Tab Separated Lines
There is also an option to separatetabbed lines with -T parameter like below.
$ cat -T myshell.sh
Here ^I is used to show tabs
Redirect With Cat
Redirect will used to provide standard output content to the next command standard input. Cat prints files 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
Append To File With Cat
While redirecting outputs previous content will be overwrited. to prevent overwrite and add new content to the existing content >> operator can be used.
$ cat myshell.sh >> yourshell.sh
Redirect Standard Input
Another useful usage of cat command is redirecting standartd input. Generally the content of a file is the standard input.
$ cat < myshell.sh
Concatenate Multiple File Into One File
One of the most funny usage of cat command is adding multiple files into single file with a single shot.
$ cat myshell.sh yourshell.sh > ourshell.sh
Use With Sort
Using cat with sort command is an other useful scenario. All content of files are piped to the sort and sort will sort all content accordingly.
$ cat myshell.sh yourshell.sh | sort