Cat Command in Linux

Cat Command in Linux

Basic Syntax

cat [options] [file...]

Common Use Cases

1. View a File’s Content

cat filename.txt

Displays the contents of filename.txt on the terminal.

2. View Multiple Files

cat file1.txt file2.txt

Shows the contents of both files, one after the other.

3. Create a New File

cat > newfile.txt

Start typing, then press Ctrl + D to save and exit.

4. Append Text to a File

cat >> existingfile.txt

Adds content to the end of the file. Press Ctrl + D to stop.

5. Concatenate Files and Save Output

cat file1.txt file2.txt > combined.txt

Combines the two files into a new file called combined.txt.

6. Show Line Numbers

cat -n filename.txt

Displays the file with line numbers.

Some Useful Options

OptionDescription
-nNumber all output lines
-bNumber non-blank lines only
-sSqueeze multiple blank lines into one
-TShow TAB characters as ^I
-EShow $ at the end of each line

Example

cat -n hello.txt

Output:

1 Hello world! 2 Welcome to Linux.

Soeng Souy

Soeng Souy

Website that learns and reads, PHP, Framework Laravel, How to and download Admin template sample source code free.

Post a Comment

CAN FEEDBACK
close