cmd line tutorials - gencmd

cmd line tutorials - gencmd

The cat Command

Unix-Linux Mac

The cat command is a versatile command line utility that can be used to read, write, and create files.

Syntax

The basic syntax for the cat command is as follows:

cat [options] file

cat [options] file file2

The file argument is the name of the file that you want to print. You can read and print multiple files. You can also use the - symbol to represent standard input, which is the input that is typed into the terminal window.

Examples

Here are some examples of how to use the cat command:

Read the contents of the file “myfile.txt” and print it to standard out:

cat myfile.txt

Create a copy of file:

cat myfile.txt > newfile.txt

Read the contents of a file and then display it in the terminal window, with line numbers:

cat -n myfile.txt

Read the contents of a file and then pipe it to another command:

cat myfile.txt | grep "search_term"

Write to a new file with contents that you enter in stdin. This is a good alternative to the touch command where you also want to enter some text right away. Use Ctrl+D or Ctrl+C to stop entering text:

cat - > abc.txt

Options

The cat command has a number of options that can be used to modify its behavior. Some of the most common options are:

-n: Number the lines of the output.

-b: Number the non-blank lines of the output.

-s: Squeeze multiple adjacent empty lines, causing the output to be single spaced.

-u: Disable output buffering.

-v: Displays non-printing characters so they are visible.

For more information on the cat command and its options, please see the man page for cat: man cat

With gencmd

gencmd print file contents

  • cat [filename]

gencmd add two files contents to a third file

  • cat file1 file2 >> file3
  • cat file1 file2 > file3