- Linux cat command
- Description
- Displaying text files
- Copy a text file
- Append a text file’s contents to another text file
- Incorporating standard input into cat output
- Syntax
- Options
- Examples
- Related commands
- 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
- Cat command in Linux with examples
- 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:
Linux cat command
On Unix-like operating systems, the cat command reads data from files, and outputs their contents. It is the simplest way to display the contents of a file at the command line.
This page covers the GNU/Linux version of cat.
Description
cat stands for «catenate.» It is one of the most commonly-used commands in Unix-like operating systems. It can be used to:
- Display text files
- Copy text files into a new document
- Append the contents of a text file to the end of another text file, combining them
Displaying text files
The simplest way to use cat is to give it the name of a text file. It displays the contents of the text file on the screen. For instance:
. will read the contents of mytext.txt and send them to standard output (your terminal screen). If mytext.txt is very long, they will zoom past and you only see the last screen’s worth of your document.
If you want to view the document page-by-page or scroll back and forth through the document, you can use a pager or viewer such as pg, more, or less.
If you specify more than one file name, cat displays those files one after the other, catenating their contents to standard output. So this command:
Will print the contents of those two text files as if they were a single file.
Copy a text file
Normally you would copy a file with the cp command. You can use cat to make copies of text files in much the same way.
cat sends its output to stdout (standard output), which is usually the terminal screen. However, you can redirect this output to a file using the shell redirection symbol «>«.
For instance, this command:
. will read the contents of mytext.txt and send them to standard output; instead of displaying the text, however, the shell will redirect the output to the file newfile.txt. If newfile.txt does not exist, it is created. If newfile.txt already exists, it will be overwritten and its previous contents are lost, so be careful.
Similarly, you can catenate several files into your destination file. For instance:
. will read the contents of mytext.txt and mytext2.txt and write the combined text to the file newfile.txt. Again, if newfile.txt does not already exist, it is created; if it already exists, it is overwritten.
Append a text file’s contents to another text file
Instead of overwriting another file, you can also append a source text file to another using the redirection operator «>>«.
. will read the contents of mytext.txt, and write them at the end of another-text-file.txt. If another-text-file.txt does not already exist, it is created and the contents of mytext.txt will be written to the new file.
The cat command also works for multiple text files as well:
. will write the combined contents of mytext.txt and mytext2.txt to the end of another-text-file.txt.
Incorporating standard input into cat output
cat reads from standard input if you specify a hyphen («—«) as a file name. For example, if you have a file, list.txt, which contains the following text:
. you could use the echo command to output text, pipe that output to cat, and instruct cat to catenate it with the file’s contents, like this:
. which would output the following text:
In short, cat is a simple but very useful tool for working with the data in text files, system logs, configuration files, and any other human-readable data stored in a file.
Syntax
Options
These options are available on GNU cat, which is standard on most Linux distributions. If you are using a different Unix-like operating system (BSD, for example), some of these options may not be available; check your specific documentation for details.
-A, —show-all | Equivalent to -vET. |
-b, —number-nonblank | Number non-empty output lines. This option overrides -n. |
-e | Equivalent to -vE. |
-E, —show-ends | Display «$» at end of each line. |
-n, —number | Number all output lines. |
-s, —squeeze-blank | Suppress repeated empty output lines. |
-t | Equivalent to -vT. |
-T, —show-tabs | Display TAB characters as ^I. |
-v, —show-nonprinting | Use ^ and M- notation, except for LFD and TAB. |
—help | Display a help message, and exit. |
—version | Output version information, and exit. |
Examples
Read the contents of file.txt and display them on the screen.
Reads the contents of file1.txt and file2.txt, and displays them in order on the terminal screen.
Read the contents of file.txt and write them to newfile.txt, overwriting anything newfile.txt previously contained. If newfile.txt does not exist, it is created.
Read the contents of file.txt and append them to the end of another-file.txt. If another-file.txt does not exist, it is created.
Display the contents of file.txt, omitting any repeated blank lines.
Related commands
cp — Copy files and directories.
ed — A simple text editor.
less — Scrolling text viewer.
more — Display text one screen at a time.
pico — A simple text editor.
pg — Browse page by page through text files.
tac — Output the contents of files in reverse order.
tee — Route a file’s contents to multiple outputs.
touch — Update the timestamp of a file or directory.
Источник
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:
Источник
Cat command in Linux with examples
Cat(concatenate) command is very frequently used in Linux. It reads data from the file and gives their content as output. It helps us to create, view, concatenate files. So let us see some frequently used cat commands.
1) To view a single file
Command:
2) To view multiple files
Command:
3) To view contents of a file preceding with line numbers.
Command:
4) Create a file
Command:
5) Copy the contents of one file to another file.
Command:
6) Cat command can suppress repeated empty lines in output
Command:
7) Cat command can append the contents of one file to the end of another file.
Command:
8) Cat command can display content in reverse order using tac command.
Command:
9) Cat command can highlight the end of line.
Command:
10) If you want to use the -v, -E and -T option together, then instead of writing -vET in the command, you can just use the -A command line option.
Command
11) Cat command to open dashed files.
Command:
12) Cat command if the file has a lot of content and can’t fit in the terminal.
Command:
12) Cat command to merge the contents of multiple files.
Command:
13) Cat command to display the content of all text files in the folder.
Command:
14) Cat command to write in an already existing file.
Command :
This article is contributed by Pranav. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Источник
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.
Источник