How to use the command cat (with examples)
- Linux
- December 25, 2023
The cat
command is a utility that is used to print and concatenate files. It is a standard Unix command used to display the contents of a file or to combine multiple files into one.
Use case 1: Print the contents of a file to stdout
Code:
cat path/to/file
Motivation: This use case allows you to quickly view the contents of a file without having to open it in an editor. It is useful when you want to quickly check the contents of a file.
Explanation:
cat
: the command itself.path/to/file
: the path to the file you want to display the contents of.
Example output:
Suppose we have a file called example.txt
with the following contents:
Hello, world!
This is an example file.
Running the command cat example.txt
would display the following output:
Hello, world!
This is an example file.
Use case 2: Concatenate several files into an output file
Code:
cat path/to/file1 path/to/file2 ... > path/to/output_file
Motivation: This use case allows you to combine the contents of multiple files into one file. It is useful when you have separate files that you want to merge into a single file.
Explanation:
cat
: the command itself.path/to/file1
,path/to/file2
, …: the paths to the files you want to concatenate.>
: redirection operator that redirects the output to a file instead of the stdout.path/to/output_file
: the path to the output file where the concatenated contents will be saved.
Example output:
Suppose we have two files called file1.txt
and file2.txt
with the following contents:
file1.txt
:
This is file 1.
file2.txt
:
This is file 2.
Running the command cat file1.txt file2.txt > output.txt
would create a file called output.txt
with the following contents:
This is file 1.
This is file 2.
Use case 3: Append several files to an output file
Code:
cat path/to/file1 path/to/file2 ... >> path/to/output_file
Motivation: This use case is similar to the previous one, but instead of overwriting the output file, it appends the concatenated contents to the end of the output file. It is useful when you want to add more data to an existing file without erasing its contents.
Explanation:
cat
: the command itself.path/to/file1
,path/to/file2
, …: the paths to the files you want to concatenate.>>
: redirection operator that appends the output to a file instead of overwriting it.path/to/output_file
: the path to the output file where the concatenated contents will be saved.
Example output:
Suppose we have an existing file called output.txt
with the following contents:
This is the original content.
Running the command cat file1.txt file2.txt >> output.txt
would append the contents of file1.txt
and file2.txt
to output.txt
, resulting in the following contents:
This is the original content.
This is file 1.
This is file 2.
Use case 4: Copy the contents of a file into an output file in unbuffered mode
Code:
cat -u /dev/tty12 > /dev/tty13
Motivation: This use case allows you to copy the contents of a file to another location in unbuffered mode. Unbuffered mode ensures that the data is immediately written to the output file without delay, which can be helpful in certain situations.
Explanation:
cat
: the command itself.-u
: option that enables unbuffered output./dev/tty12
: the input file from which the contents will be read.>
: redirection operator that redirects the output to another file./dev/tty13
: the output file where the contents will be saved.
Example output:
The output of this command will depend on the contents of /dev/tty12
and the destination specified by /dev/tty13
. If /dev/tty12
contains text, it will be copied to /dev/tty13
in unbuffered mode.
Use case 5: Write stdin to a file
Code:
cat - > path/to/file
Motivation: This use case allows you to save the input from stdin (usually entered through the keyboard) into a file. It is useful when you want to quickly create a file with some content without opening an editor.
Explanation:
cat
: the command itself.-
: hyphen symbol used to represent stdin as the input source.>
: redirection operator that redirects the output to a file.path/to/file
: the path to the file where stdin content will be saved.
Example output:
If you run the command cat - > newfile.txt
and then type “Hello, world!” followed by pressing Enter, the text “Hello, world!” will be saved in the newfile.txt
.
Use case 6: Number all output lines
Code:
cat -n path/to/file
Motivation: This use case allows you to display the contents of a file with line numbers. It is useful when you want to reference specific lines in a file.
Explanation:
cat
: the command itself.-n
: option that displays line numbers before each line.path/to/file
: the path to the file you want to display.
Example output:
Suppose we have a file called numbers.txt
with the following contents:
First line
Second line
Third line
Running the command cat -n numbers.txt
would display the following output:
1 First line
2 Second line
3 Third line
Use case 7: Display non-printable and whitespace characters
Code:
cat -v -t -e path/to/file
Motivation: This use case allows you to see non-printable and whitespace characters in a file. It is useful when you want to inspect the file for any invisible characters.
Explanation:
cat
: the command itself.-v
: option that displays non-printable characters.-t
: option that replaces tabs with^I
.-e
: option that appends$
to each line end.path/to/file
: the path to the file you want to display.
Example output:
Suppose we have a file called special.txt
with the following contents:
This is a test.
Running the command cat -v -t -e special.txt
would display the following output:
This is a^Itest.$
Conclusion:
The cat
command is a versatile utility that can be used to print and concatenate files. It provides various options for manipulating and displaying file contents. Whether you want to quickly view the contents of a file, merge multiple files, or inspect non-printable characters, cat
is a handy tool to have in your command line toolkit.