The cat command in Linux is a versatile and widely used utility that stands for “concatenate.” Its primary function is to read and display the content of one or more files and, if desired, concatenate them together. The cat command is often used to quickly view the contents of small text files, concatenate files, or redirect their output to other commands or files.
Here is a brief introduction to the cat command and its common usage:
Usage:
cat [OPTION]... [FILE]...
Get Your Free Linux training!
Join our free Linux training and discover the power of open-source technology. Enhance your skills and boost your career! Learn Linux for Free!Key Features:
- Displaying File Content: The primary purpose of cat is to display the contents of one or more files to the terminal.
- Concatenating Files: By specifying multiple files as arguments, cat can concatenate their content and display the merged output. For example, cat file1.txt file2.txt will show the contents of file1.txt followed by the contents of file2.txt.
- Redirecting Output: The output of cat can be redirected to create or update files. For example, cat file1.txt > newfile.txt will create a new file named newfile.txt with the contents of file1.txt.
- Numbering Lines: Using the -n option, cat can number each line of the output, which can be helpful for reference or documentation.
- Viewing Special Characters: By default, cat displays control characters and non-printable characters. This behavior can be modified using the -v option.
- Displaying Tabs: The -T option can be used to display tabs as ^I, making it easier to visualize tab-separated data.
Example Usage:
# Display the content of a file
cat myfile.txt
# Concatenate two files and display the merged output
cat file1.txt file2.txt
# Create a new file with the content of existing file
cat file1.txt > newfile.txt
# Number the lines of a file and display the output
cat -n myfile.txt
# Display tabs as ^I for better visualization
cat -T data.tsv
While cat is useful for simple tasks, it may not be the best choice for handling large files or complex text processing. For more advanced tasks, other commands like less, grep, or text editors like vim may be more appropriate.
Table of Contents
Display a File Using Linux cat command
The cat command is one of the most frequently used commands in Linux. It allows you to view the contents of a file. The syntax for using the cat command is:
cat filename
where filename is the name of the file you want to view.
To see an example of how the cat command works, let’s create a file called myfile.txt and add some text to it:
echo "This is my file" > myfile.txt
Now, if we use the cat command to view the contents of myfile.txt, we should see the following output:
This is my file
As you can see, the cat command simply displays the contents of the file.
Copy the contents of one file to another with Linux cat command
To copy the contents of one file to another using the Linux cat command, you can simply redirect the output of cat to the destination file. Here’s the command:
cat source_file > destination_file
In this command:
- source_file: The file whose contents you want to copy.
- destination_file: The file to which you want to copy the contents.
For example, if you want to copy the contents of file1.txt to file2.txt, you can use the following command:
cat file1.txt > file2.txt
This will overwrite the contents of file2.txt with the contents of file1.txt. If file2.txt does not exist, it will be created, and if it already exists, its contents will be replaced with the contents of file1.txt.
Appending the contents of one file to another with Linux cat command
cat filename >> filename-new
Let’s say you have two files, file1.txt and file2.txt, with the following contents:
file1.txt:
This is the content of file1.
file2.txt:
This is the initial content of file2.
Now, you want to append the contents of file1.txt to file2.txt using the cat command. You can do it like this:
cat file1.txt >> file2.txt
After running this command, the contents of file2.txt will be updated, and it will look like this:
file2.txt:
This is the initial content of file2.
This is the content of file1.
The contents of file1.txt have been appended to file2.txt without overwriting the original content in file2.txt.
View the contents of many files with Linux Cat command
If you want to view the contents of multiple files, you can specify them all on the command line:
cat file1.txt file2.txt file3.txt
Combine multiple files into a single file with Linux cat command
The cat command can also be used to concatenate (combine) files. For example, if we have two files, file1.txt and file2.txt, and we want to create a new file that contains the contents of both files, we can use the cat command like this:
cat file1.txt file2.txt > newfile.txt
This will create a new file called newfile.txt that contains the contents of both file1.txt and file2.txt.
Sorting Contents of Multiple Files in a Single File with Linux cat command
cat filename1 filename2 filename3|sort > filename-new
Linux Cat Command with More & Less Options
cat filename|more
cat filename|less
Show Line Numbers using Linux Cat command
If there are lines with no characters at all they won’t be numbered. For all the non-empty lines in a file use the following command:
cat -b filename
To show numbers for all the lines regardless as to whether they are blank, type the following command:
cat -n filename
Display non-printing characters using Linux Cat command
cat -e filename
Reducing Blank Lines with Linux Cat command
When we show the contents of a file using the cat command we probably don’t want to see when there are loads of consecutive blank lines. Use the -s switch to condense all blank lines into a single blank line:
cat -s filename
Linux cat command Options
- -A, –show-all equivalent to -vET
- -b, –number-nonblank number nonempty output lines, overrides -n
- -e equivalent to -vE
- -E, –show-ends display $ at end of each line
- -n, –number number all output lines
- -s, –squeeze-blank suppress repeated empty output lines
- -t equivalent to -vT
- -T, –show-tabs display TAB characters as ^I
- -u (ignored)
- -v, –show-nonprinting use ^ and M- notation, except for LFD and TAB
Related: 10 Examples to use Linux Cat Command