cat: Concatenate and display file contents
August 9th, 2024 9:27 AM Mr. Q Categories: Command
Command: cat
Used to concatenate and display the contents of one or more files.
Sample Command and Output:
$ cat file1.txt
This is the content of file1.txt.
It spans multiple lines.
Description:
cat file1.txt
: Displays the contents offile1.txt
on the terminal. It outputs the entire content of the file.
Sample Command and Output:
$ cat file1.txt file2.txt
This is the content of file1.txt.
It spans multiple lines.
This is the content of file2.txt.
Description:
cat file1.txt file2.txt
: Concatenates and displays the contents of bothfile1.txt
andfile2.txt
sequentially.
Sample Command and Output:
$ cat -n file1.txt
1 This is the content of file1.txt.
2 It spans multiple lines.
Description:
cat -n file1.txt
: Displays the contents offile1.txt
with line numbers prefixed. The-n
option numbers the lines in the output.
Sample Command and Output:
$ cat > newfile.txt
This is the content being written to newfile.txt.
Ctrl+D
$ cat newfile.txt
This is the content being written to newfile.txt.
Description:
cat > newfile.txt
: Redirects input from the terminal tonewfile.txt
. Type the content and pressCtrl+D
to save and exit. Thecat
command then displays the content ofnewfile.txt
.
Sample Command and Output:
$ cat file1.txt | grep "search_term"
This line contains the search_term.
Description:
cat file1.txt | grep "search_term"
: Usescat
to display the contents offile1.txt
and pipes it togrep
, which searches for lines containingsearch_term
.