Linux+: Linux Shell 16 – Cat Command

J

Jarret W. Buse

Guest
Linux+: Linux Shell 16 – Cat Command

The cat command allows for the copying of files (standard input) to the display (standard output). The command is useful to display the contents of files to the screen, if you would rather not load the file in an editor. Because you write the files to the display, there is no chance of accidentally editing the file.

The ability of the 'cat' command allows for multiple files to be sent by way of standard output. Options are also available and the syntax is as follows:

cat OPTIONS FILE1 FILE2 … FILE-X

The options for 'cat' are:

-A (--show-all) – is equivalent to using the options '-v -E -T' (-vET)
-b (--number-nonblank) – numbers the lines which contain data
-e – same as using the options '-v -E' (-vE)
-E (--show-ends) – places a '$' at the end of each line
-n (--number) – numbers all lines, including blank ones
-s (--squeeze-blank) – disregards multiple blank lines and only prints one to standard output
-t – same as '-V -t' (-Vt)
-T (--show-tabs) – prints a TAB as '^I'
-v (--show-nonprinting) – shows all non-printing characters in the file, but not the line-feed or TAB. The character ASCII value is set to octal and 200 is added to it to become a printable character (A 'M-' in front of it)
--help – displays the help information for the cat command
--version – shows version of the cat command

It is possible to output two files and change the standard output from display to a file. If I have two files which I want to combine into one file, I can do the following:

cat file1 file2 > file3

The command takes file1 and places its contents into file3, then places file2 into file3 after file1 is finished.

What if you want a special header placed between file1 and file2? Most people would create a small file to combine in-between file1 and file2. It can be done this way as follows:

cat file1 header file2 > file3

Here, the file named header contains the header which is to be placed between the two files when placed into file3.

If the header is not too long, it can be done this way:

cat file1 - file2 > file3

The dash (-) places the cat command into a mode to allow standard input from the keyboard. Press 'Enter' to place a blank line in the output file and type any text to place it in the file. When done entering data from the standard input, type Control+D to end the standard input. Once Control+D is pressed, file2 is sent to the output file and the command ends.

NOTE: Multiple files can be used for 'cat' as well as multiple standard input commands of the dash (-). The dash can be placed between every file and even two placed next to each other. Remember that if two are next to each, it requires you to enter Control+D twice to end the standard input mode.

When you use the 'cat' command, you will notice that large files will cause the screen to scroll until the end of the file is reached. To be able to look over a page at a time, the output can be piped (|) to other commands to control the scrolling. For example, you can pipe the output to the 'more' or 'less' command.

You can also create a file by using one of the following commands:

cat - > file-out
cat > file-out


NOTE: Since no input is specified, the default standard input is used.

You can also append data to an existing file. For example, to add file1 to the contents of file2, do the following:

cat file1 >> file2

You can also type information into the existing file at the end as needed:

cat - >> file2

You may also have seen when a compressed file (RAR) is split into smaller files. A large RAR file is split into four smaller files (file1.rar.001, file1.rar.002, file1.rar.003 and file1.rar.004) and you want to make it into one large file. You can open each file and extract them, then recompress them as one file. You can save time by doing the following:

cat file1.rar.001 file1.rar.002 file1.rar.003 file1.rar.004 > mainfile.rar

The 'mainfile.rar' can then be uncompressed or viewed using a compression utility.

Even though the 'cat' command seems simplistic, it can accomplish complex tasks.
 

Attachments



J

Jim Laughlan

Guest
Can I combine multiple PDF files into one PDF file using the cat command?
 
J

Jarret W. Buse

Guest
Can I combine multiple PDF files into one PDF file using the cat command?
Unfortunately, no you cannot. If you combine two PDF files, they will be combined into one file with the combined file size. When viewed, only the second PDF file will be shown since each file has a separate header information.
 
J

Jim Laughlan

Guest
Unfortunately, no you cannot. If you combine two PDF files, they will be combined into one file with the combined file size. When viewed, only the second PDF file will be shown since each file has a separate header information.
Thanks Jarret! Just was curious if it was possible.
 
A

Aarav

Guest
can i edit a file using cat command whether it is root file or normal file...?????
 


Members online


Top