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:
Copy001$ cat file1.txt
002This is the content of file1.txt.
003It 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:
Copy001$ cat file1.txt file2.txt
002This is the content of file1.txt.
003It spans multiple lines.
004This 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:
Copy001$ cat -n file1.txt
002 1 This is the content of file1.txt.
003 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:
Copy001$ cat > newfile.txt
002This is the content being written to newfile.txt.
003Ctrl+D
004$ cat newfile.txt
005This 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:
Copy001$ cat file1.txt | grep "search_term"
002This 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
.