2024-07-02
The most straightforward use of cat
is displaying the contents of a file to the terminal. Simply provide the filename as an argument:
cat myfile.txt
This command will output the content of myfile.txt
to your standard output (usually your terminal). Let’s say myfile.txt
contains:
This is the first line.
This is the second line.
Running the command above will display this text in your terminal.
cat
shines when it comes to joining multiple files. You can concatenate many files into a single output, either to the terminal or to a new file:
cat file1.txt file2.txt file3.txt > combined.txt
This command concatenates file1.txt
, file2.txt
, and file3.txt
and redirects the output to a new file named combined.txt
. If combined.txt
already exists, it will be overwritten. To append to an existing file, use >>
instead of >
:
cat file1.txt file2.txt file3.txt >> combined.txt
This appends the content of file1.txt
, file2.txt
, and file3.txt
to the end of combined.txt
.
cat
with WildcardsCombining cat
with shell wildcards allows you to process multiple files matching a specific pattern. For instance:
cat *.txt > all_text_files.txt
This command concatenates all files ending with .txt
in the current directory into all_text_files.txt
.
Sometimes, it’s helpful to see line numbers alongside the file content. The -n
option provides this functionality:
cat -n myfile.txt
This will display myfile.txt
with line numbers added at the beginning of each line.
-s
(Suppressing Messages)When concatenating files, and one of the files doesn’t exist, cat
will typically print an error message. The -s
(silent) option suppresses these messages:
cat -s file1.txt file2.txt non_existent_file.txt > output.txt
This command will still concatenate file1.txt
and file2.txt
into output.txt
, without displaying an error message for non_existent_file.txt
.
cat
You can use cat
to create new files and populate them with content. Use the redirection operator >
and echo to achieve this:
cat > newfile.txt << EOF
This is the first line of the new file.
This is the second line.
EOF
This creates newfile.txt
containing the text within the EOF
markers. The EOF
indicates the end of the input. You can replace EOF
with any other unique string.
cat
with Other CommandsThe power of cat
truly emerges when used in conjunction with other Linux commands within pipes. For example, to count the lines in a file:
cat myfile.txt | wc -l
This pipes the output of cat myfile.txt
to the wc -l
command, which counts the number of lines.
This demonstrates just a fraction of the possibilities offered by the seemingly simple cat
command. Experimenting with different options and combinations will reveal its full potential.