Skip to main content

Command Palette

Search for a command to run...

Cheatsheet & Examples: cat

Updated
2 min read
H

I am a developer from Malaysia. I work with PHP most of the time, recently I fell in love with Go. When I am not working, I will be ballroom dancing :-)

cat command is used to read, concatenate, and display the content of files in the terminal. It can also be used to create, view, or merge files.

Display file contents

Example Usage: cat filename

What it does: Prints the content of the specified file to the terminal.

Command-line Arguments Explained:

  • filename: The path to the file being read and displayed.

Concatenate multiple files

Example Usage: cat file1 file2 > combined

What it does: Combines the contents of file1 and file2 into a new file called combined.

Command-line Arguments Explained:

  • file1, file2: The files to be concatenated.
  • >: Shell redirection operator, not a cat argument, used to direct output to a file.

Create or overwrite a file

Example Usage: cat > newfile

What it does: Creates a new file named newfile or overwrites an existing one, allowing input to be entered and saved.

Command-line Arguments Explained:

  • >: Shell redirection operator, not a cat argument, used to redirect input to a file.

Display file with line numbers

Example Usage: cat -n filename

What it does: Displays the content of filename with line numbers added to each line.

Command-line Arguments Explained:

  • -n: Enables line numbering for the output.

Append to a file

Example Usage: cat >> existingfile

What it does: Adds input from standard input to the end of existingfile without overwriting it.

Command-line Arguments Explained:

  • >>: Shell redirection operator, not a cat argument, used to append output to a file.

Display non-printing characters

Example Usage: cat -v filename

What it does: Displays non-printing characters in filename as visible symbols (e.g., tabs as ^I, newlines as $.).

Command-line Arguments Explained:

  • -v: Enables visualization of non-printing characters.

Show help information

Example Usage: cat --help

What it does: Displays a summary of cat command options and usage.

Command-line Arguments Explained:

  • --help: Shows the help message for the cat command.

Pipe file content to another command

Example Usage: cat file | grep "pattern"

What it does: Passes the content of file to the grep command for filtering.

Command-line Arguments Explained:

  • |: Shell pipe operator, not a cat argument, used to connect commands.

Squeeze multiple blank lines

Example Usage: cat -s file1 file2

What it does: Removes extra blank lines between files when outputting their content.

Command-line Arguments Explained:

  • -s: Squeezes multiple consecutive blank lines into one.

Number non-empty lines

Example Usage: cat -b filename

What it does: Displays the content of filename with line numbers, excluding blank lines.

Command-line Arguments Explained:

  • -b: Enables line numbering, skipping blank lines.

More from this blog

Hong's Tech Blog

110 posts

The blog is older than you know. I prefer counting from the emergence of one integral anomaly to the emergence of the next, in which case this is the forth version.