cat
The cat
command in Linux and Unix-like operating systems is a short form for "concatenate." It's one of the most frequently used commands and serves multiple purposes, primarily to read, create, and concatenate files. The basic functionality of cat
is to display the content of files to the standard output (the terminal screen), but it can do more when combined with other commands or used with its options.
Basic Usage
Displaying the contents of a file: To read and display the contents of a file on the terminal, you can use
cat
followed by the filename. For example,cat myfile.txt
will display the contents ofmyfile.txt
.
Creating a File
Creating a new file: You can also use
cat
to create a new file by redirecting its output to a file. For example,cat > newfile.txt
will create a new file namednewfile.txt
. You can then type the content of the file, and once you're done, pressCTRL+D
to save and exit.
Concatenating Files
Concatenating files: The
cat
command can concatenate the contents of multiple files and display the result. For example,cat file1.txt file2.txt
will display the contents offile1.txt
followed by the contents offile2.txt
. You can also redirect the output to create a new file containing the concatenated contents of the files:cat file1.txt file2.txt > combined.txt
.
Appending Content
Appending content to an existing file: You can append the content of one or more files to the end of another file using the
>>
redirection operator. For example,cat file3.txt >> combined.txt
will append the content offile3.txt
to the end ofcombined.txt
.
Other Useful Options
-n
: Numbers all output lines, which is useful for viewing or debugging files with content that needs to be referenced by line number.-s
: Suppresses repeated empty output lines, condensing the output to make it easier to read.-E
: Displays a$
at the end of each line, making it easier to see line breaks, especially in files with mixed content or trailing spaces.
Example Commands
Display file content:
cat filename.txt
Create a file:
cat > filename.txt
Concatenate multiple files:
cat file1.txt file2.txt > mergedfile.txt
Append file content:
cat additional.txt >> existingfile.txt
Number lines:
cat -n file.txt
cat
is a powerful tool for handling text data and is often used in conjunction with other commands and pipes (|
) to perform more complex tasks like searching within files, sorting file contents, and more.
Last updated