Sruthy, with her 10+ years of experience, is a dynamic professional who seamlessly blends her creative soul with technical prowess. With a Technical Degree in Graphics Design and Communications and a Bachelor’s Degree in Electronics and Communication, she brings a unique combination of artistic flair…
The cat command is perhaps the most commonly used Unix command. It is derived from catenate – which describes the process of connecting things.
The cat command is a filter that can be used for multiple purposes:
Display the contents of text files.
Copy text files into a new file.
Append a text file to the end of another text file.
Unix Cat command with Examples
Cat Command Syntax:
cat [options] [files]
The cat command becomes a very powerful tool when combined with the Unix shell’s input and output redirection symbols:
cmd > file.txt: the “>” symbol redirects the stdout stream from the command to replace the contents of the specified file. The file will be created if it doesn’t exist, or its contents will be replaced if it does.
cmd >> file.txt: the “>>” symbol redirects the stdout stream from the command to append to the contents of the specified file. The file will be created if it doesn’t exist, or the new contents will be appended to the same file if it does.
Examples:
List contents of file1 on stdout
$ cat file1
List the contents of file1 and file2 together on stdout
$ cat file1 file2
Copy contents of file1 and file2 to file3
$ cat file1 file2 > file3
Append contents of file1 and file2 to file4
$ cat file1 file2 >> file4
If no filename is provided to the command, or if the file name is “-“, it reads the input text from stdin. This can be used to populate the contents of the file from the command prompt.
Example:
$ cat > file1
Hello
World
^D
In this example, the command reads its input from the terminal, and writes the output to the file “file1”. The user can enter their text line by line and terminate the input with the “^D” character that indicates end-of-file.
The cat command also supports the following options:
cat -n: number the output lines.
cat -s: suppress repeated output lines that are empty.
Learn Unix Sort Command with Examples: The Unix sort command is a simple command that can be used to rearrange the contents of text files line by line. The command is a filter command that sorts the input text and prints the result to stdout. By default, sorting is done…
learn Cut Command in Unix with Simple and Practical Examples: Unix provides a number of filter commands that can be used for processing flat file databases. These filter commands can be chained together to perform a series of operations with a single command. A flat file database is a file…
Learn Tar Command in Unix with practical Examples: The primary function of the Unix tar command is to create backups. It is used to create a ‘tape archive’ of a directory tree, that could be backed up and restored from a tape-based storage device. The term ‘tar’ also refers to the…
Learn ls Command in Unix with examples: The Ls command is used to get a list of files and directories. Options can be used to get additional information about the files. Know ls command syntax and options with practical examples and output. ls Command in Unix with Examples ls Syntax: ls…
Learn Grep Command in Unix with Practical Examples: Grep command in Unix/Linux is the short form of ‘global search for the regular expression’. The grep command is a filter that is used to search for lines matching a specified pattern and print the matching lines to standard output. Grep Command…
Introduction to Find Command in Unix: Search files and directories with Unix Find File Command The Unix find command is a powerful utility to search for files or directories. The search can be based on different criteria, and the matching files can be run through defined actions. This command recursively…
Overview of Unix Filters Text Processing Utilities: In this tutorial, we will learn about filters and work with various filter commands. Filters are commands that read input from stdin and write output to stdout. By default, when using a shell terminal, the stdin is from the keyboard, and the stdout is…
In this tutorial, you will learn different basic and advanced Unix Commands. Unix commands are inbuilt programs that can be invoked in multiple ways. Here, we will work with these commands interactively from a Unix terminal. A Unix terminal is a graphical program that provides a command-line interface using a…