About cat command in linux

Содержание
  1. Cat command in Linux with examples
  2. 13 Basic Cat Command Examples in Linux
  3. General Syntax of Cat Command
  4. 1. Display Contents of File
  5. 2. View Contents of Multiple Files in terminal
  6. 3. Create a File with Cat Command
  7. 4. Use Cat Command with More & Less Options
  8. 5. Display Line Numbers in File
  9. 6. Display $ at the End of File
  10. 7. Display Tab Separated Lines in File
  11. 8. Display Multiple Files at Once
  12. 9. Use Standard Output with Redirection Operator
  13. 10. Appending Standard Output with Redirection Operator
  14. 11. Redirecting Standard Input with Redirection Operator
  15. If You Appreciate What We Do Here On TecMint, You Should Consider:
  16. cat command in Linux / Unix with Examples
  17. Purpose
  18. cat command syntax
  19. cat command in Linux with examples
  20. Display A File With cat Command
  21. Create A File With cat Command
  22. Viewing A Large File With cat Command And Shell Pipes
  23. How To Combine Two Or More Files Using cat Command
  24. How To Append Data To A Text File
  25. Task: Number All Output Lines
  26. How To View Non-printing Characters
  27. Viewing All Files
  28. Print Files
  29. Joining Binary Files
  30. Fooling Programs
  31. Testing Audio Device
  32. Cat Command in Linux: Essential and Advanced Examples
  33. A. Using cat command with standard input
  34. B. Using cat command with binary files
  35. 1. Joining split files
  36. 2. Working with streamable file formats
  37. 3. Hacking cpio archives
  38. C. Essential cat command options
  39. -n : number lines
  40. -s : suppress repeated empty output lines
  41. -b : number only non-empty lines
  42. -v , -e , -t : display non-printing characters
  43. -v : view invisible characters
  44. -e : display invisible characters, including end-of-line
  45. -t : display invisible characters, including tabs
  46. -et : show all hidden characters
  47. Bonus: The useless use of cat command in Linux

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).

Читайте также:  Street fighter 5 не запускается windows

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 / 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
  • » Syntax
  • » Examples
  • » View A File
  • » Create A File
  • » Cat and Pipes
  • » Combine Files
  • » Append To A File
  • » Number All Lines
  • » Non-printing Characters
  • » View Large Number Of Files
  • » Print Files
  • » Joining Binary Files
  • » Fooling Programs
  • » Testing Audio Device
  • » Gathering Linux System Information
  • » Display Large Blocks of Textual Data In A Script
  • » Print Files In Reverse
  • » Options
  • » Summing up

The cat command is used for:

  1. Display text file on screen
  2. Read text file
  3. Create a new text file
  4. File concatenation
  5. Modifying file
  6. Combining text files
  7. 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:

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: Essential and Advanced Examples

Continuing the tour of those not-that-well known commands started last week with the ls command, let’s examine today the cat command.

The cat name stands for catenate as the primary job of that command is to join several input files by sequentially sending their content on standard output:

If you want to store the result of that concatenation in a file, you have to use a shell redirection:

Even if its primary design goal is to catenate files, the cat utility is also often employed with only one argument to display the content of that file onto the screen, exactly like I did it on the last line of the example above.

A. Using cat command with standard input

When used without any argument, the cat command will read data from its standard input and write them to its standard output— which is mostly useless … unless you are using some option to transform the data. We will talk about a couple of interesting options later.

In addition to file paths, the cat command also understands the — special file name as an alias for the standard input. That way, you can insert the data read from the standard input between the files given on the command line:

B. Using cat command with binary files

1. Joining split files

The cat command does not make any assumption about the file content, so it will happily work with binary data. Something that may be useful to rejoin files broken by the split or csplit command. Or to join partial downloads like we will do it now:

We have now two halves of an image. You can open the first half and see it is “broken” using ImageMagick’s display , or gimp , or any other software able to read image files:

If you study the curl command I used, you see the two parts are perfectly complementary. The first half is from byte 0 to 50000 and the second half, from the byte 50001 to the end of the file. There should be no missing data between them. So we only have to catenate the two parts together (in the right order) to get back the full file:

2. Working with streamable file formats

Not only you can use the cat command to “rejoin” binary files that were split into several parts, but in some cases, you can also create new files that way. That works particularly well with “headerless” or “streamable” file formats like MPEG transport stream video files ( .TS files):

We can now combine all those transport stream video files using the cat command, obtaining a perfectly valid TS file in the output:

Thanks to the TS file format, you can combine those files in the order you want, and you can even use the same file several times in the argument list to create loops or repetition in the output video. Obviously, this would be more fun if we were using animated images, but will I let you do that by yourself: many consumer-grade devices record TS files, and if they don’t, you can still use ffmpeg to convert almost any video file to a transport stream file. Don’t hesitate to share your creations using the comment section!

3. Hacking cpio archives

As the last example, let see how we can use the cat command to combine several cpio archives. But this time, it will not be as straightforward since it will require a little bit of knowledge about the cpio archive file format.

A cpio archive stores file’s metadata and content sequentially which makes it suitable for file-level concatenation with the cat utility. Unfortunately, the cpio archive also contains a trailer used to mark the end of the archive:

The good new is, with cpio binary archives, that trailer has a fixed length of 280 bytes. So, using the head standard command, we have an easy way to remove it:

C. Essential cat command options

After having played with various binary file formats, let’s go back to plain old text files now by studying a couple of option specifically tailored to deal with those files. While not part of the POSIX standard, those options are portable across the BSD and GNU cat implementations. Please note I do not pretend to be exhaustive here, so check the man to see the full list of options supported by cat on your system!

-n : number lines

With the n option, the cat command will prefix each output line by its line number:

The -n option numbers output lines. That means the counter is not reset when switching from one input file to the next, as you will see it if you try the following command by yourself:

-s : suppress repeated empty output lines

With the -s option, the cat command will collapse multiple consecutive empty lines in only one:

In the example above, you can see, in the default output, the lines 10 and 11 were empty. When adding the -s option, the second empty line was discarded.

-b : number only non-empty lines

Somewhat related with the two preceding options, the -b option will number lines, but ignoring empty ones:

The example above uses two instances of the cat command with different options in a pipeline. The inner numbering comes from the -b option used with the first cat command. The outer numbering comes from the -n option used with the second cat .

As you can see, the first and last line were not numbered by the -b option because they are empty. But what about the 6th line? Why does it still be numbered with the -b option? Well, because it is a blank line— but not an empty one, as we will see in the next section.

-v , -e , -t : display non-printing characters

The three options -v , -e `, and `-t are used to display different sets of invisible characters. Even if the sets overlap, there is no “catch-all” option, so you will have to combine them if you want to display all invisible characters.

-v : view invisible characters

The -v option display all non-printing characters with caret and meta notation, except the line feed and tabulation.

With that option, control characters will appear as a caret ( ^ ) followed by the appropriate ASCII character (e.g., the carriage return, byte 13, is displayed as ^M because M in ASCII is 64 + 13), and characters with the high-order bit set will appear in “meta” notation M- followed by the representation corresponding to the 7 lower bits (e.g., the byte 141 will be displayed as M-^M because 141 is 128 + 13).

While seemingly esoteric, that feature may be useful when working with binary files, like, for example, if you want to examine the raw information embedded in a JPEG file:

Another use case for the -v option is finding control characters that might have leaked into a text file. If you remember it, we have that strange issue above with the -b option numbering the 6th input line, whereas it looked like it was empty. So let’s investigate that:

Ah ah! Do you see those ^M marks? They are used to replace the otherwise invisible carriage return character. Where did it come from? Well, the dict protocol, like any other Internet protocol, is using CRLF as the line terminator. So we downloaded them as part of our sample files. You can learn more about line feed and carriage returns in the fold and fmt article. But for now, it explains why cat considered the 6th line as not being empty.

-e : display invisible characters, including end-of-line

The -e option works like the -v option, except it will also add a dollar sign ( $ ) before each line-feed character, thus explicitly showing the end of lines:

-t : display invisible characters, including tabs

The -t option works like the -v option, except it will also display tabulations using the ^I caret notation (tab is stored as a byte holding the value 9, and I in ASCII is 64+9=73):

-et : show all hidden characters

As I already mentioned it briefly, if you want to display all non-printing characters, including both tabulations and end-of-line markers, you will need to use both the -e and -t options:

Bonus: The useless use of cat command in Linux

No article about the cat command would be complete without a mention of the “Useless Use of Cat” anti-pattern.

It occurs when you use cat for the only purpose of sending the content of a file to the standard input of another command. That use of the cat command is said “useless” since a simple redirection or filename parameter would have done the job, and would have done it better. But an example worth a thousand words:

In the example above, I used a pipeline to display the content of the uuoc.txt file with the less pager:

So, the only purpose of the cat command was to feed the standard input of the less command with the content of the uuoc.txt file. I would have obtained the same behavior using a shell redirection:

As a matter of fact, the less command, like many commands, also accept a filename as an argument. So I could have simply written that instead:

As you can see, no need for cat here. If I mention the “Useless Use of Cat” anti-pattern, this is because, if you use it publicly on a forum or elsewhere, undoubtedly someone will point that out at you with the argument you will create an “extra process for nothing.”

I must admit for a long time I was quite dismissive with such comments. After all, on our modern hardware, spawning one extra process for a one-shot operation couldn’t cause that much overhead.

But while writing this article, I did a quick experiment, comparing the time required with and without UUOC by a test awk script to process 500MB of data coming from a slow media.

To my surprise, the difference was far from negligible:

However, the reason is not about an extra process being created. But because of the extra read/write and context switching the UUOC incurs (as you can infer it from the time spend executing system code). So indeed, when you work on large datasets that extra cat command has a nonnegligible cost. As for myself, I will try to be more vigilant with that now! And you? If you have examples of the Useless Use of Cat don’t hesitate to share them with us!

Источник

Читайте также:  Windows sp3 oem key
Оцените статью