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.txton 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.txtandfile2.txtsequentially.
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.txtwith line numbers prefixed. The-noption 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+Dto save and exit. Thecatcommand 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": Usescatto display the contents offile1.txtand pipes it togrep, which searches for lines containingsearch_term.