Linux cat command

Linux cat command

Linux cat Command – Concatenate and Display File Content

The cat (short for "concatenate") command is one of the most commonly used commands in Linux for viewing, creating, and combining text files. It is a simple yet powerful tool to display the content of files and is especially useful for quick file viewing, combining multiple files, or redirecting output.

Syntax of cat

cat [OPTIONS] FILE(s)
  • OPTIONS → Flags that modify the behavior of cat.
  • FILE(s) → The name(s) of the file(s) you want to view or manipulate.

Common Usage of cat

1. Display the Content of a File

To display the contents of a file on the terminal:

cat file_name

Example:

cat document.txt

This will display the content of document.txt in the terminal window.

2. Display Multiple Files

To display the contents of multiple files one after another:

cat file1.txt file2.txt

Example:

cat file1.txt file2.txt

This will show the content of file1.txt followed by file2.txt.

3. Create a New File

You can create a new file and add content to it using cat:

cat > new_file.txt

This will allow you to type text. Once you're done, press Ctrl + D to save the file and exit.

Example:

cat > notes.txt This is a note. Press Ctrl+D to save and exit.

This creates notes.txt with the text "This is a note."

4. Append Content to an Existing File

To append content to an existing file, use the >> operator:

cat >> existing_file.txt

Example:

cat >> notes.txt This is additional content. Press Ctrl+D to save and exit.

This appends "This is additional content." to notes.txt.

5. Concatenate and Display Files Together

To combine multiple files and display their content in a single output:

cat file1.txt file2.txt > combined.txt

This will concatenate the contents of file1.txt and file2.txt into a new file named combined.txt.

6. Number Lines of a File

To display the contents of a file with line numbers, use the -n option:

cat -n file_name

Example:

cat -n document.txt

This will display document.txt with line numbers before each line.

7. Show Line Numbers Only for Non-Empty Lines (-b)

If you want to display line numbers only for non-empty lines, use the -b option:

cat -b file_name

Example:

cat -b document.txt

This will number only the non-blank lines of document.txt.

8. Show Non-Printable Characters (-v)

If you want to display non-printable characters (like tabs and newlines) in the output, use the -v option:

cat -v file_name

Example:

cat -v document.txt

This will display document.txt and any special or non-printable characters will be visible.

9. Suppress Repeated Empty Lines (-s)

To suppress consecutive empty lines and display only a single empty line between sections, use the -s option:

cat -s file_name

Example:

cat -s document.txt

This will remove any repeated blank lines in document.txt, leaving only one blank line between paragraphs.

Examples

1. Display a Single File

cat myfile.txt

This will display the content of myfile.txt in the terminal.

2. Concatenate Multiple Files

cat file1.txt file2.txt

This will display the contents of file1.txt followed by the contents of file2.txt.

3. Create and Write to a File

cat > newfile.txt Hello, this is a new file. Press Ctrl+D to save and exit.

This will create newfile.txt and write the text "Hello, this is a new file." inside.

4. Append Content to a File

cat >> existingfile.txt This is the appended content. Press Ctrl+D to save and exit.

This appends the text "This is the appended content." to existingfile.txt.

5. Show Line Numbers

cat -n myfile.txt

This will display myfile.txt with line numbers before each line.

6. Concatenate Files into One

cat file1.txt file2.txt > combined.txt

This will combine the content of file1.txt and file2.txt into a new file called combined.txt.

7. View Non-Printable Characters

cat -v myfile.txt

This will display myfile.txt, showing any special characters like tabs or newlines.

Error Handling with cat

  • File Not Found: If the specified file does not exist, you will get an error like:

    cat: file_name: No such file or directory
  • Permission Denied: If you don't have permission to read the file, you'll see an error:

    cat: file_name: Permission denied

Conclusion

The cat command is one of the most versatile and commonly used commands in Linux for working with text files. It allows you to display, create, append, and concatenate files with ease. Whether you need to view file contents, combine files, or add content, cat is an essential tool for managing text files in Linux.

Would you like additional details or SEO optimization? 🚀

Souy Soeng

Souy Soeng

Our website teaches and reads PHP, Framework Laravel, and how to download Admin template sample source code free. Thank you for being so supportive!

Github

Post a Comment

CAN FEEDBACK
close