- 13 Basic Cat Command Examples in Linux
- General Syntax of Cat Command
- 1. Display Contents of File
- 2. View Contents of Multiple Files in terminal
- 3. Create a File with Cat Command
- 4. Use Cat Command with More & Less Options
- 5. Display Line Numbers in File
- 6. Display $ at the End of File
- 7. Display Tab Separated Lines in File
- 8. Display Multiple Files at Once
- 9. Use Standard Output with Redirection Operator
- 10. Appending Standard Output with Redirection Operator
- 11. Redirecting Standard Input with Redirection Operator
- If You Appreciate What We Do Here On TecMint, You Should Consider:
- Cat Command in Linux
- Basic Syntax
- Display Contents of File
- Display content of All Files
- Display Multiple Files at Once
- Copy the output of one file to another file
- Append the output of a file to another file
- Copy multiple files to another text file/ Concatenating the files
- Display Line Numbers in File
- Create a File
- Sorting output
- Remove consecutive empty lines
- Display tab characters
- Printing output of a file
- About the author
- Karim Buzdar
- cat command in Linux / Unix with Examples
- Purpose
- cat command syntax
- cat command in Linux with examples
- Display A File With cat Command
- Create A File With cat Command
- Viewing A Large File With cat Command And Shell Pipes
- How To Combine Two Or More Files Using cat Command
- How To Append Data To A Text File
- Task: Number All Output Lines
- How To View Non-printing Characters
- Viewing All Files
- Print Files
- Joining Binary Files
- Fooling Programs
- Testing Audio Device
13 Basic Cat Command Examples in Linux
The cat (short for “concatenate“) command is one of the most frequently used commands in Linux/Unix-like operating systems. cat command allows us to create single or multiple files, view content of a file, concatenate files and redirect output in terminal or files.
In this article, we are going to find out the handy use of cat commands with their examples in Linux.
General Syntax of Cat Command
1. Display Contents of File
The below example will show the contents of /etc/passwd file.
2. View Contents of Multiple Files in terminal
In below example, it will display the contents of the test and test1 file in the terminal.
3. Create a File with Cat Command
We will create a file called test2 file with the below command.
Awaits input from the user, type desired text, and press CTRL+D (hold down Ctrl key and type ‘d‘) to exit. The text will be written in the test2 file. You can see the content of the file with the following cat command.
4. Use Cat Command with More & Less Options
If a file having a large number of content that won’t fit in the output terminal and the screen scrolls up very fast, we can use parameters more and less with the cat command as shown below.
5. Display Line Numbers in File
With the -n option you could see the line numbers of a file song.txt in the output terminal.
6. Display $ at the End of File
In the below, you can see with the -e option that ‘$‘ is shows at the end of the line and also in space showing ‘$‘ if there is any gap between paragraphs. This option is useful to squeeze multiple lines into a single line.
7. Display Tab Separated Lines in File
In the below output, we could see TAB space is filled up with the ‘^I‘ characters.
8. Display Multiple Files at Once
In the below example we have three files test, test1, and test2, and able to view the contents of those files as shown above. We need to separate each file with ; (semicolon).
9. Use Standard Output with Redirection Operator
We can redirect the standard output of a file into a new file else existing file with a ‘>‘ (greater than) symbol. Careful, existing contents of the test1 will be overwritten by the contents of the test file.
10. Appending Standard Output with Redirection Operator
Appends in existing file with ‘>>‘ (double greater than) symbol. Here, the contents of the test file will be appended at the end of the test1 file.
11. Redirecting Standard Input with Redirection Operator
When you use the redirect with standard input ‘ Tags cat command Examples
If You Appreciate What We Do Here On TecMint, You Should Consider:
TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
We are thankful for your never ending support.
Источник
Cat Command in Linux
Following are some basic functions of cat command:
- Displaying contents of the file
- Creating new files
- Combining files
- Copying file contents
Note that we have used Debian 10 OS for explaining the commands and procedures mentioned in this article.
Basic Syntax
The basic syntax for using the Cat command is:
Use the above command if you are in the same directory that contains your file. Otherwise, if you are in some other directory, mention the path to that file as follows:
Display Contents of File
The most common and basic usage of cat command is displaying the file contents. In order to display the file contents to a Terminal, simply type cat and the filename as follows:
An example of this would be to display the contents of /etc/hosts file. In that case, the command would be:
Display content of All Files
To display all the files in a current directory, use the wildcard character with the cat command as follows:
To display only the contents of text files in a directory, enter the following command:
Display Multiple Files at Once
You can also combine and display the contents of multiple files together in the Terminal using the cat command. To display multiple files simultaneously, use the following syntax:
An example of this would be to view the output of all the three files /etc/hostname, /etc/resolv.conf and /etc/hosts in the Terminal as shown in the following screenshot:
Copy the output of one file to another file
It can also be utilized to copy the output of one file to another file. If the destination file does not exist, it will first create it, otherwise overwrites the targeted file.
To copy the output of a source file to another file, use the following syntax:
An example of this would be to copy the output of a testfile1 to another file named testfile_backup as follows:
This command will first create the file testfile_backup and then copy the contents of testfile1 to it.
Append the output of a file to another file
Instead of overwriting the output of a targeted file in the above example, you can also make the cat command to append the output. The following syntax can be used for this purpose:
It will create the destination file if it does not already exist, otherwise will append the output.
Copy multiple files to another text file/ Concatenating the files
Another major use of cat command is that you can combine multiple files into a single file. Following syntax can be used to concatenate the file1, file2, and file3 and save them to another file named file4.txt.
For instance, we want to concatenate the output of /etc/hostname, /etc/resolv.conf and the /etc/hosts file to another file named network.txt. The following command can be used for this purpose:
Display Line Numbers in File
To display line numbers to the output of a file, simply use –n flag s follows:
For instance, if you are viewing a file containing the list of items, then you can use the –n flag to display those items with a number. Remember that empty lines are also numbered as shown in the following screenshot:
If you do not want to number the empty lines, use –b flag as follows:
Create a File
You can also create a file using the cat command. The following syntax can be used for thi spurpose:
After entering the above command, enter the text you want to store in the file. Once done, use Ctrl+D to save and exit. After that, you can view the contents of your newly created file by executing the following command in Terminal:
Sorting output
You can also combine the sort with the cat command to sort the output alphabetically as follows:
Similarly, in case of multiple files, you can concatenate the output into one file in an alphabetical order:
Remove consecutive empty lines
Sometimes the file contains consecutive empty lines that you do not want to print. Cat command allows merging those consecutive empty lines and shows them as one empty line.
Use the following command syntax to remove the repeated empty lines:
For instance, we have the following file with consecutive empty lines.
By using the –s flag, you will see the difference.
Display tab characters
Sometimes, you have to remove tabs from your files. Cat command can help you to find the tabs on your file by using the –t flag as follows:
Tabs will be shown in the output as ^I characters.
Printing output of a file
Another popular use of cat command is in the printing contents of a document. For instance, to print the output of a file to a printing device named /dev/lp, the following syntax will be used:
In this article, we have explained through various examples how you can use the cat command to manipulate the files in Linux. Cat command is popular among all users because of its simple syntax and the lot of options that it provides. Creating and viewing a file, merging, copying, and appending the file contents, printing, and a lot more can be handled with this single cat command.
About the author
Karim Buzdar
Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. He blogs at LinuxWays.
Источник
cat command in Linux / Unix with Examples
I am a new Linux and Unix system user. How do I use cat command on Linux or Unix-like operating systems? Can you provide basic examples and syntax usage for cat command?
The cat (short for concatenate) command is one of the most frequently used flexible commands on Linux, Apple Mac OS X, Unix, *BSD (FreeBSD / OpenBSD / NetBSD) operating systems.
cat command details | |
---|---|
Description | Concatenation files |
Category | File Management |
Difficulty | Easy |
Root privileges | No |
Est. reading time | 6 mintues |
Table of contents
|
The cat command is used for:
- Display text file on screen
- Read text file
- Create a new text file
- File concatenation
- Modifying file
- Combining text files
- Combining binary files
Purpose
Basic file operation on a text file such as displaying or creating new files.
cat command syntax
The basic syntax is as follows:
cat [options] filename
cat file1
cat > file2
cat file3 | command
cat file4 | grep something
cat command in Linux with examples
Display A File With cat Command
To view a file, enter:
Create A File With cat Command
To create a file called “foo.txt”, enter:
cat >foo.txt
Type the following text:
You need to press [CTRL] + [D] i.e. hold the control key down, then tap d. The > symbol tells the Unix / Linux system that what is typed is to be stored into the file called foo.txt (see stdout for more information). To view a file you use cat command as follows:
cat foo.txt
Viewing A Large File With cat Command And Shell Pipes
If the file is too large to fit on the computer scree, the text will scroll down at high speed. You will be not able to read. To solve this problem pass the cat command output to the more or less command as follows:
The more and less command acts as shell filters. However, you can skip the cat command and directly use the Linux / Unix more & less command like this:
How To Combine Two Or More Files Using cat Command
You can combine two files and creates a new file called report.txt, enter:
How To Append Data To A Text File
To append (add data to existing) data to a file called foo.txt, enter:
Task: Number All Output Lines
Type the following command:
Fig.01: Number all output lines with cat command
How To View Non-printing Characters
To display TAB characters as ^I, enter:
cat -T filename
To display $ at end of each line, enter:
Use ^ and M- notation, except for LFD and TAB and show all nonprinting:
To show all, enter:
cat -A fileName
OR
cat -vET fileName
Sample outputs:
Fig.02: Unix / Linux cat command: View Non-printing Characters
Viewing All Files
You can simply use the shell wildcard as follows:
cat *
To view only (c files) *.c files, enter:
cat *.c
Another option is bash for loop, or ksh for loop:
Print Files
You can directly send a file to to the printing device such as /dev/lp
cat resume.txt > /dev/lp
On modern systems /dev/lp may not exists and you need to print a file using tool such as lpr:
cat resume.txt | lpr
OR
lpr resume.txt
Joining Binary Files
You can concatenate binary files. In good old days, most FTP / HTTP downloads were limited to 2GB. Sometime to save bandwidth files size were limited to 100MB. Let us use wget command to grab some files (say large.tar.gz was split to 3 file on remote url):
wget url/file1.bin
wget url/file2.bin
wget url/file3.bin
Now combine such files (downloaded *.bin) with the cat command easily:
Another example with the rar command under Unix and Linux:
Fooling Programs
You can use the cat command to fool many programs. In this example bc thinks that it is not running on terminals and it will not displays its copyright message. The default output:
bc -l
Samples session:
Now try with the cat command:
bc -l | cat
Samples session:
Testing Audio Device
You can send files to sound devices such as /dev/dsp or /dev/audio to make sure sound output and input is working:
You can simply use the following command for recording voice sample and play back with it cat command:
Источник