The cat
(concatenate) command is a staple in Unix and Unix-like operating systems for displaying, concatenating, and writing file contents to the standard output.
cat [OPTION]... [FILE]...
[OPTION]
...: Modify the behavior ofcat
.[FILE]
...: Files to be processed. Reads from stdin if no file or-
is specified.
-n
,--number
: Number all output lines.-b
,--number-nonblank
: Number non-empty output lines only.-s
,--squeeze-blank
: Squeeze multiple adjacent blank lines.-E
,--show-ends
: Display$
at the end of each line.-T
,--show-tabs
: Display TAB characters as^I
.
To display the contents of a file:
cat file.txt
To concatenate multiple files into one:
cat file1.txt file2.txt > combined.txt
To create a new file:
cat > newfile.txt
To append text to an existing file:
cat >> existingfile.txt
To view end-of-line or tabs:
cat -E file.txt
cat -T file.txt
You can use cat without specifying a file to read from the standard input. This is useful for quickly creating file content from terminal input:
cat > example.txt
Type your content here...
Press CTRL+D (EOF) to save and exit.
cat can also be used to view the contents of multiple files sequentially on the terminal. This is a quick way to compare or merge files manually:
cat file1.txt file2.txt file3.txt
If you have a file with list items or entries that you want to sort and remove duplicates from, you can use cat in combination with other commands like sort
and uniq
.
cat list.txt | sort | uniq > sorted_list.txt
Using cat list.txt | sort | uniq > sorted_list.txt
streamlines the process of organizing data by automatically sorting the contents of list.txt
, removing any duplicate entries, and saving the cleaned, ordered list to sorted_list.txt
. This command sequence accelerates workflows by performing data cleanup and organization in a single step, eliminating the need for manual sorting and deduplication, thus saving time and reducing potential for error in data handling.
The cat
command is an essential tool for text processing in Unix/Linux environments, offering a wide range of functionalities from file display to concatenation and debugging.
https://www.geeksforgeeks.org/cat-command-in-linux-with-examples/