The cat
command is a vital tool for handling files and their contents in the context of Linux and Unix-like operating systems. Cat is a shortcut for "concatenate" and is used for a number of file generation, viewing, and modification tasks.
The basic purpose of the cat
command is to join files together and show their contents on the terminal. Its usefulness goes far beyond simple concatenation, though. With its many functions, it's a useful addition to any Linux user's command-line toolkit.
Using SSH, log into the server and check the basic syntax.
Basic syntax of cat
command is:
cat
: command itself.
[OPTION]
: optional arguments or flags that alter the cat command's behavior. A hyphen (-
) usually comes before them.
[FILE
]: the name of the file or files for processing.
Here are some of the most common examples for using the cat
command in Linux:
Use the cat
command with the input redirection (>
) to create a new file in Linux.
cat > filename
After executing the command, type the content to be added to the file.
Press Ctrl + D to save the content and exit.
Example: to create a file named example.txt with some sample text, follow the command:
cat > example.txt
When you press Ctrl + D the file example.txt
will be created with the content that was specified.
To view a file's contents with the cat
command, provide the filename as an argument. Use syntax cat filename
.
Replace filename
with the file that needs to be viewed.
Example: To view a file named example.txt, use the command cat example.txt
to display the content of example.txt in the terminal.
To view multiple files in order, specify the files as additional arguments:
cat file1 file2 file3
This will show the content of file1, followed by file2 and then file3.
Concatenate files using cat
command by listing the files that need to be concatenated as an argument:
cat file1 file2 > merged_file
Example: To concatenate the contents of file1and file2 while redirecting the output to a new file named merge.file, follow the command:
cat file1 file2 >> merge_file
Multiple files can be concatenated by listing them after cat
. To concatenate multiple files while maintaining the original files intact, use the append (>>
) redirection operator instead of (>
)
To copy the content of one file to another, use the cat
command along with redirection:
cat file1 > destination_file
Put the names of the files need to be copied to and from in lieu of file1
and destination_file
, respectively.
Example: To copy the content of file1 to file2, use the command:
cat file1 > file2
Remember that if the destination file already exists, this procedure will overwrite its contents.
To append the content without overwriting the existing content, you can use the cat command with the add (
>>
) redirection operator: more on it in the next chapter.
To append the content of one file to the end of another, use the append (>>
) redirection operator along with the cat
command:
cat source_file >> destination_file
Replace source_file
with the name of the file which content needs to be appended, and destination_file
with the name of the file to which the content needs to be appended to.
Example: To append the content of file1 to the end of file2 without overwriting its existing content, use:
cat file1 >> file2
If file2 does not exist, it will be created.
The -s
can be used to suppress repeated empty lines in the output using the cat
command. This option compresses the multiple empty lines into a single empty line:
cat -s filename
Replace filename
with the name of the file to show, and hide the lines that include repeated empty lines.
Example: example.txt file has multiple empty lines.
Use cat -s
on example.txt to suppress the repeated empty lines.
Use the cat
command along with -n
to display line numbers in the output:
cat -n filename
Example: example.txt file with the content This is line 1, This is line 2, This is line 3, This is line 4 will be displayed with line numbers by using the command:
cat -n example.txt
Use the cat
command along with grep to number the non-empty lines in the output.
cat -n filename | grep -v '^*$'
cat -n filename
: Numbers all the lines in the file
grep -v '^*$'
: removes lines that contain spaces only or that are empty.
Example: example.txt file has multiple empty lines.
Use cat -n example.txt | grep -v '^ *$'
to display the non-empty lines with numbers.
To display non-printable characters using the cat
command, use the -v
or --show-nonprinting
option.
cat -v filename
Or:
cat --show-nonprinting filename
Replace filename
with the name of the file to display, including non-printable characters.
There is no direct option to highlight the end of lines using the cat
command. This can be accomplished by appending a special character to each line using the sed
command, and using the cat to show the file.
cat -s/$/→/’ filename | cat -e
sed 's/$/→/' filename
: This command uses sed to substitute (s
) the end of each line ($
) with →
, or any other preferred character.
cat -e
: Shows the content of the file, with special character ($
by default) at the end of each line.
Example: example.txt with content This is line 1, This is line 2 and This is line 3.
Using the command sed 's/$/→/' example.txt | cat -e
will show the content with the end of each line highlighted.
The output indicates the end of each line as →$
, where $
is the end of the line marker from cat -e
and →
is the character inserted by sed
. The character can be altered per the user’s preference.
Being proficient with the cat
command helps users to be much more productive and efficient when working in the command line environment. The cat
command offers crucial capabilities for manipulating text files in Linux. For operations involving text processing, such as reading file contents or concatenating files, cat
is still a crucial tool in the Linux toolbox.