Never Ending Security

It starts all here

Tag Archives: Cat

Cat Commands Cheatsheet

HowTo: Cat Command Examples

Cat command is one of the basic commands that you learned when you started in the Unix / Linux world. What more could this command do?

Display the contents of a file

When you pass the filename as an argument to cat, it displays the contents of the file.

$ cat file.txt

You can also display contents of more than one file.

$ cat file.txt file2.txt

Create a New File

Using cat command, the lines received from stdin can be redirected to a new file using redirection symbols. When you type simply cat command without any arguments, it just receives the stdin content and displays it in the stdout.

$ cat

You can also redirect the stdout to a new file as shown below.

$ cat > file.txt
Text for file.txt.

Sometimes you may want to append the content to a file, use >> redirection symbol as shown below.

$ cat >> file.txt

Copy File Content

Redirection symbols in unix plays an important role in processing the standard file descriptor contents. Using it, you can copy the contents of one file into another as shown below.

$ cat file.txt > file2.txt

Concatenate Contents of Multiple Files

Through cat command, you will be able to concatenate contents of more than one file into a new file.

For example, the codes from file.txt and file2.txt gets combined into a new file

$ cat file.txt file2.txt > file3.txt

Display Line numbers

To display the contents of a file with the line number in front of each line, use option -n.

$ cat -n file.txt

Note that the lines which contains whitespaces are not considered as empty lines

Concatenate File Contents along with Input from Stdin

There is a possibility to read lines from stdin along with concatenation of other files. Hence the user can type his own content whenever its required.

In the following example, you can insert a few lines (from stdin) in the beginning while combining files together.

$ cat - file.txt file2.txt > file3.txt
Contents from file : file.txt, file2.txt

Don’t Display Repeated Empty Output Lines

Sometimes the file would contain repeated empty lines which you don’t want to display in the stdout while listing it out. cat command provides an option -s which will suppress consecutive empty output lines into one.

# cat -s file.txt

Display End of Line and TAB characters

You can make the cat to display the $ character at end of every line. Normally by listing file contents, users cant identify whitespaces at the end of each lines, by using the cat -e option.

$ cat -e file.txt

Use option -T to display the tab characters. It displays ^I for TAB character.

$ cat -eT file.txt

Read Content until a Specific Pattern

when you are reading from stdin, you can read until a line that contains a specific pattern.

$ cat << EOF
> line one
> line two
line one
line two