Enhancing Skills

cat: Concatenate and display file contents

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 of file1.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 both file1.txt and file2.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 of file1.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 to newfile.txt. Type the content and press Ctrl+D to save and exit. The cat command then displays the content of newfile.txt.

Sample Command and Output:

$ cat file1.txt | grep "search_term"
This line contains the search_term.

Description:

  • cat file1.txt | grep "search_term": Uses cat to display the contents of file1.txt and pipes it to grep, which searches for lines containing search_term.


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.