Softpanorama

May the source be with you, but remember the KISS principle ;-)
Home Switchboard Unix Administration Red Hat TCP/IP Networks Neoliberalism Toxic Managers
(slightly skeptical) Educational society promoting "Back to basics" movement against IT overcomplexity and  bastardization of classic Unix

cat command

News Unix Filters Recommended Links Syntax printf Command Examples Reference
split command Input and Output Redirection Process Substitution in Shell Perl power tools      
tac command head tail Perl re-implemenations Tee Netcat String Operations in Shell
AWK Pipes in Loops bash Tips and Tricks Sysadmin Horror Stories Tips Humor Etc

Introduction

The cat  command concatenates and display files. Netcat can be used instead of cat in case networking capabilities are important.  There is a version of cat  called dog  which adds to cat some networking capabilities like ability to access URLs not only plain files. Source package of dog can be retrieved from Cygwin and compiled on any platform. It is also available on Debian

Concatenation can be non trival. The example below output file1 contents, then standard input, then file2  contents.

echo "======" | cat file1 - file2

There are also split and csplit  commands, The former split file(s) into equal chunks, the latter splits file(s) based on context.  Head and tail command can be used to select certain area of the file. There is also a version of cat  called zcat  which performs operation similar to  uncompress -c  or gunzip -c  on each input file

Even classic cat  is a more versatile tool then it looks from the first site. For example it can be used as a primitive editor for creation one liners or adding a line to the config file.

Cat accepts multiple file as an arguments and reads each file in the order listed and writes the content of merged stream  to the standard output. For example

       cat file  prints file on your terminal (Note:    cat -n file  will do the same with line numbering)

cat file1 file2 > file3 

concatenates file1 and file2, and writes the results in file3.

Using cat as poor man editor

If no input file is given, cat  reads from the standard input file. That capability is often used to create small files with cat  instead of editor like vi  or ed.  For example

cat > /etc/resolv.conf
> line
> line
> line

Ctrl-D
You can also add a line to the file if you use  ">>"  instead of  ">":
cat >> /etc/hosts <<
10.10.10.10 fileserver
Ctrl-D

If you're appending only a single line, it's better to use echo instead of cat

Cat also can be use here document  or here string creating small documents or addling lines to system files:

cat > message <<EOF
Hello Jim, 
I will come to work later today as I have doctor appointment
EOF
You can also add lines to system files such as /etc/fstab (see fstab - Wikipedia)
cat >> /etc/fstab <<EOF
# Removable media
/dev/cdrom      /mnt/cdrom      udf,iso9660  noauto,owner,ro                                     0 0
 
# NTFS Windows 7 partition
/dev/sda1       /mnt/Windows    ntfs-3g      quiet,defaults,locale=en_US.utf8,umask=0,noexec     0 0
 
# Partition shared by Windows and Linux
/dev/sda7       /mnt/shared     vfat         umask=000                                           0 0
 
# mounting tmpfs
tmpfs           /mnt/tmpfschk   tmpfs        size=100m                                           0 0
 
# mounting cifs
//pingu/ashare  /store/pingu    cifs         credentials=/root/smbpass.txt                       0 0
 
# mounting NFS
pingu:/store    /store          nfs          rw
EOF   

 

Numbering lines in output

Few Unix users know that cat  can number lines so in a way it can serve as a preprocessor to more to view files with line numbering. For example:

cat -n var/log/messages | more

With option -n cat  can number lines

cat can show nonprinting characters, such as tabs and control characters. For instance, to see if any line has trailing blanks:

$ cat -A input.txt

This line has trailing blanks.    $
This line does not.$
$

Syntax and options

General syntax is

cat [ options ] [file...]

GNU Cut Options:

Solaris Cat Options:

Solaris version is less feature rich then GNU version. Also in Solaris version option -s has different semantic then in GNU version.

When used with the -v  option, the following options may be used:

The -e  and -t  options are ignored if the -v  option is not specified.

Examples

Example 1 Concatenating a file

Example 2: Concatenating two files into one

Example 3: Concatenating several files with text entered on keyboard

The command:

cat start - middle - end > file

when standard input is a terminal, gets two arbitrary pieces of input from the terminal with a single invocation of cat. Note, however, that if standard input is a regular file, this would be equivalent to the command:

cat start - middle /dev/null end > 	file

because the entire contents of the file would be consumed by cat  the first time `-' was used as a file operand and an end-of-file condition would be detected immediately when `-' was referenced the second time.

Example 4:  Output first file contents, then sepator then the second file content (pretty elegant usage, of you ask me)

echo "======" | cat file1 - file2

Example 5: To concatenate several files:

cat section1.1 section1.2 section1.3 >section1

This command creates a file named section1  that is a copy of section1.1  followed by section1.2  and section1.3.

To suppress error messages about files that do not exist, enter:

cat  -q section2.1 section2.2 section2.3 >section2

If section2.1  does not exist, this command concatenates section2.2  and section2.3. The result is the same if you do not use the -q flag, except that the cat command displays the error message:

cat: cannot open section2.1

You may want to suppress this message with the -q flag when you use the cat command in shell procedures.

Example 6: To append one file to the end of another:

cat section1.4 >> section1

The >> (two carets) appends a copy of section1.4  to the end of section1. If you want to replace the file, use the > (caret).

Example 7: To add text to the end of a file:

cat >> notes
Get milk on the way home
Ctrl-D

This command adds Get milk on the way home  to the end of the file called notes. The cat command does not prompt; it waits for you to enter text. Press the Ctrl-D key sequence to indicate you are finished.

<<[-] EndMarker (Here documents)

Here documents are often used with cat command. In this case shell reads each line input until it locates a line containing only the value of the parameter (which serves as end marker ) or an end-of-file character.  This part of the script is called here document. For example

cat << EOF 
jan feb mar
apr may jun
jul aug sep
oct nov dec
EOF

The string EOF was used as the delimiting identifier. It specified the start and end of the here document. The redirect and the delimiting identifier do not need to be separated by a space: <<EOF and  << EOF both work.

The delimited by the end marker part of the script is converted into a file that becomes the standard input. If all or part of the end marker parameter is quoted, no interpretation is used in the body here document. No expansion of variables, no arithmetic expressions, no backticks output substitution, nothing.

In other words, the here document is treated as a file that begins after the next newline character and continues until there is a line containing only the end_marker, with no trailing blank characters. Then the next here document, if any, starts (it can be several). In other words, the format of here document is as follows: 

[n]<<end_marker
   here document
end_marker

There are two types of end marker:

If a hyphen (-) is appended to <<, the shell strips all leading tabs from the Word parameter and the document.

<<< -- Here string

A here string (available in Bash, ksh, or zsh) consisting of <<<, and effects input redirection from a word  or a string literal:

In this case you do not need the end marker  -- end of the sting  signifies end of the here string.  here is an explanation of the concept from Wikipedia:

A single word need not be quoted:

tr a-z A-Z <<< one

yields:

ONE

In case of a string with spaces, it must be quoted:

tr a-z A-Z <<< 'one two three'

yields:

ONE TWO THREE

This could also be written as:

 FOO='one two three'
 tr a-z A-Z <<< $FOO

Multiline strings are acceptable, yielding:

 tr a-z A-Z <<< 'one
 two three'

yields:

ONE
TWO THREE

Note that leading and trailing newlines, if present, are included:

 tr a-z A-Z <<< '
 one
 two three'

yields:

ONE
TWO THREE

The key difference from here documents is that in here documents, the delimiters are on separate lines (the leading and trailing newlines are stripped), and the terminating delimiter can be specified.

Environmental variables and exit status

See environ(5) for descriptions of the following environment variables that affect the execution of cat: LC_CTYPE, LC_MESSAGES, and NLSPATH.

The following exit values are returned:


Top Visited
Switchboard
Latest
Past week
Past month

NEWS CONTENTS

Old News ;-)

[Sep 16, 2019] Getting started with the Linux tac command Opensource.com

Notable quotes:
"... standard output ..."
Sep 16, 2019 | opensource.com

Getting started with the Linux tac command Learn when to use the tac command instead of cat, and why you might want to. 09 Sep 2019 Seth Kenlon (Red Hat) Feed 17 up 1 comment Image credits : Image credits: Jeff Macharyas, CC BY-SA 4.0. Donald, the cat. x Subscribe now

Get the highlights in your inbox every week.

https://opensource.com/eloqua-embedded-email-capture-block.html?offer_id=70160000000QzXNAA0

The tac command is essentially the cat command, but its purpose is to concatenate files in reverse. Like cat , it has a convenient fallback mode to print to standard output (STDOUT) if no output file is provided, making it one of those commands that are more often used as a lazy pager -- like less and more -- than the function it is named for.

The cat command is often overused and abused, and tac is often taken as a joke command like ddate or cowsay . It often gets paraded out in April Fool's day articles detailing stupid terminal tricks. So, it may come as a surprise that tac actually has a legitimate reason to exist.

It's actually a useful command.

What is the purpose of tac?

The tac man page does a rather poor job of describing its own function:

Write each FILE to standard output, last line first.

Taking that statement as it's written, tac should print the last line of a file, then print the file starting back at line one:

$ cat metasyntactic.list
foobar
foo
bar
baz

$ tac metasyntactic.list
baz
foobar
foo
bar

That's not what it does, though. Its info page is much clearer:

copies each FILE ('-' means standard input),
or standard input if none are given,
to standard output, reversing the records
(lines by default) in each separately.

For example:

$ tac metasyntactic.list
baz
bar
foo
foobar

Ignoring the fact that tac gives you everything in reverse, it has a few surprisingly useful and unique options.

Tac and separators

As the info page indicates, the file doesn't have to be delimited by line, meaning that tac is equally as effective with, for example, a CSV file. You define a file's separator character with the --separator or -s option, along with the delimiter used in the file.

For a CSV file, the character is probably a comma ( , ), but you can define any character. If a file doesn't terminate with the separator character, though, then you get an unexpected result:

$ tac --separator = "," metasyntactic.csv
bazbar,foo,foobar

There is no separator character between the first two items. The file's final record (the string following the final separator, in this case, a comma) is not itself followed by a comma, so it's treated as a non-record by tac . To account for this issue, use the --before or -b option, which places the separator character before each record:

$ tac --separator = "," --before metasyntactic.csv
baz,bar,foo,foobar

The separator character doesn't have to be a single character. It can also be a regular expression (regex).

Tac and regular expressions

A full explanation of regex is out of scope for this article, but it's worth mentioning that extended POSIX is supported by means of an environment variable . Extended regex greatly enhances the readability of a regular expression, and for the sake of simplicity, that's what this example uses. Assume you have a file containing strings all separated by integers:

$ cat metasyntactic.txt
foobar123foo456bar789baz898

More Linux resources

You can reliably predict that the strings you care about are separated by integers, but you cannot reliably predict what those integers will be. That's exactly the problem regex is meant to solve.

To use regex in your tac command, use the --regex or -r option before your --separator definition. Also, unless it's already set in your environment, you must activate the REG_EXTENDED environment variable. You can set this variable to anything but zero to activate it, and you can do that in all the usual ways:

$ REG_EXTENDED = 1 tac --regex \
--separator = '[0-9]+' metasyntactic.txt
89baz898bar765foo432foobar1

The regex option doesn't handle non-terminated records well, though, even using the --before option. You may have to adjust your source file if that feature is important to you.

When to use tac

These simple yet useful parsing options make tac worth using as an uncomplicated, minimalist parsing command. For those simple jobs that aren't quite worth writing an AWK or Perl expression for, tac just might be a sensible solution.

The tac command is limited, obviously, because it doesn't manipulate records in any way aside from reversing them. But sometimes that's the only list manipulation you need.

For instance, if you're packaging software for distribution, it's not unusual to have a list of dependencies that are required for installation. Depending on how you gathered this list, you may have it in the order you established the dependencies were required instead of the order in which they must be installed.

This practice is relatively common because compiler errors hit the high-level dependencies first. That is, if your system is missing libavcodec then GCC stops and alerts you; but since GCC hasn't gotten a chance to probe your system for libvorbis and libvpx , for example, it can't tell you that those dependencies are also missing (and, often, required to exist on your system before compiling libavcodec ).

So, your list of dependency grows in top-down form as you discover what libraries your system needs to build the libraries that the libraries need (and so on). At the end of such a process, tac is the quick and easy way to reverse that list.

Another common annoyance is log files. Entries are generally appended to a log file, so admins use tail to see the latest errors. That works well, but there are times you want to see a "chunk" of entries without knowing how far back you need to go. The tac command piped to less or more puts the latest entries at the top of your screen.

Finally, many configuration files have no clear termination marker for a given section. You can look up awk and sed commands to devise a way to determine when a block in a config file ends, or you can use tac to reverse the order such that once your parser has found the first relevant entry in that block, it also knows when to stop reading, because what used to be the header is now a footer.

Tac on

There are plenty of other great uses for tac , and probably a bunch of reasons that tac is too rudimentary to be a solution. Your system likely has it installed, however, so remember this command the next time you find that edge case in your workflow that really really needs to be at tac ked in reverse.

[Mar 13, 2019] Getting started with the cat command by Alan Formy-Duval

Mar 13, 2019 | opensource.com

6 comments

Cat can also number a file's lines during output. There are two commands to do this, as shown in the help documentation: -b, --number-nonblank number nonempty output lines, overrides -n
-n, --number number all output lines

If I use the -b command with the hello.world file, the output will be numbered like this:

   $ cat -b hello.world
   1 Hello World !

In the example above, there is an empty line. We can determine why this empty line appears by using the -n argument:

$ cat -n hello.world
   1 Hello World !
   2
   $

Now we see that there is an extra empty line. These two arguments are operating on the final output rather than the file contents, so if we were to use the -n option with both files, numbering will count lines as follows:

   
   $ cat -n hello.world goodbye.world
   1 Hello World !
   2
   3 Good Bye World !
   4
   $

One other option that can be useful is -s for squeeze-blank . This argument tells cat to reduce repeated empty line output down to one line. This is helpful when reviewing files that have a lot of empty lines, because it effectively fits more text on the screen. Suppose I have a file with three lines that are spaced apart by several empty lines, such as in this example, greetings.world :

   $ cat greetings.world
   Greetings World !

   Take me to your Leader !

   We Come in Peace !
   $

Using the -s option saves screen space:

$ cat -s greetings.world

Cat is often used to copy contents of one file to another file. You may be asking, "Why not just use cp ?" Here is how I could create a new file, called both.files , that contains the contents of the hello and goodbye files:

$ cat hello.world goodbye.world > both.files
$ cat both.files
Hello World !
Good Bye World !
$
zcat

There is another variation on the cat command known as zcat . This command is capable of displaying files that have been compressed with Gzip without needing to uncompress the files with the gunzip command. As an aside, this also preserves disk space, which is the entire reason files are compressed!

The zcat command is a bit more exciting because it can be a huge time saver for system administrators who spend a lot of time reviewing system log files. Where can we find compressed log files? Take a look at /var/log on most Linux systems. On my system, /var/log contains several files, such as syslog.2.gz and syslog.3.gz . These files are the result of the log management system, which rotates and compresses log files to save disk space and prevent logs from growing to unmanageable file sizes. Without zcat , I would have to uncompress these files with the gunzip command before viewing them. Thankfully, I can use zcat :

$ cd / var / log
$ ls * .gz
syslog.2.gz syslog.3.gz
$
$ zcat syslog.2.gz | more
Jan 30 00:02: 26 workstation systemd [ 1850 ] : Starting GNOME Terminal Server...
Jan 30 00:02: 26 workstation dbus-daemon [ 1920 ] : [ session uid = 2112 pid = 1920 ] Successful
ly activated service 'org.gnome.Terminal'
Jan 30 00:02: 26 workstation systemd [ 1850 ] : Started GNOME Terminal Server.
Jan 30 00:02: 26 workstation org.gnome.Terminal.desktop [ 2059 ] : # watch_fast: "/org/gno
me / terminal / legacy / " (establishing: 0, active: 0)
Jan 30 00:02:26 workstation org.gnome.Terminal.desktop[2059]: # unwatch_fast: " / org / g
nome / terminal / legacy / " (active: 0, establishing: 1)
Jan 30 00:02:26 workstation org.gnome.Terminal.desktop[2059]: # watch_established: " /
org / gnome / terminal / legacy / " (establishing: 0)
--More--

We can also pass both files to zcat if we want to review both of them uninterrupted. Due to how log rotation works, you need to pass the filenames in reverse order to preserve the chronological order of the log contents:

$ ls -l * .gz
-rw-r----- 1 syslog adm 196383 Jan 31 00:00 syslog.2.gz
-rw-r----- 1 syslog adm 1137176 Jan 30 00:00 syslog.3.gz
$ zcat syslog.3.gz syslog.2.gz | more

The cat command seems simple but is very useful. I use it regularly. You also don't need to feed or pet it like a real cat. As always, I suggest you review the man pages ( man cat ) for the cat and zcat commands to learn more about how it can be used. You can also use the --help argument for a quick synopsis of command line arguments.

Victorhck on 13 Feb 2019 Permalink

and there's also a "tac" command, that is just a "cat" upside down!
Following your example:

~~~~~

tac both.files
Good Bye World!
Hello World!
~~~~
Happy hacking! :)
Johan Godfried on 26 Feb 2019 Permalink

Interesting article but please don't misuse cat to pipe to more......

I am trying to teach people to use less pipes and here you go abusing cat to pipe to other commands. IMHO, 99.9% of the time this is not necessary!

In stead of "cat file | command" most of the time, you can use "command file" (yes, I am an old dinosaur from a time where memory was very expensive and forking multiple commands could fill it all up)

Uri Ran on 03 Mar 2019 Permalink

Run cat then press keys to see the codes your shortcut send. (Press Ctrl+C to kill the cat when you're done.)

For example, on my Mac, the key combination option-leftarrow is ^[^[[D and command-downarrow is ^[[B.

I learned it from https://stackoverflow.com/users/787216/lolesque in his answer to https://stackoverflow.com/questions/12382499/looking-for-altleftarrowkey...

Geordie on 04 Mar 2019 Permalink

cat is also useful to make (or append to) text files without an editor:

$ cat >> foo << "EOF"
> Hello World
> Another Line
> EOF
$

16 cat command examples for beginners in Linux

Example:14 Squeeze blank repeated lines using -s option

Let's take am example of file 'linux_blank' , which consists of multiple repeated blank lines.

file-with-blank-space

Now remove the blank repeated lines in the output using below command.
[root@linuxtechi ~]# cat -s linux_blank
test

test1
test2

test3

test4
[root@linuxtechi ~]#

Example:16 Display non-printing characters using -v option.

-v option in the cat command is used to show the non-printing characters in the output. This option become useful when we are suspecting the CRLF ending lines, in that case it will show ^M at the end of each line.
[root@linuxtechi tmp]# cat test_file
hi there
[root@linuxtechi tmp]# cat -v test_file
hi there^M
[root@linuxtechi tmp]#

cat reimplmentation from Perl Power Tools

#!/usr/local/bin/perl -wT

#
# $Id: cat,v 1.1.1.1 2001/06/06 08:54:05 sdague Exp $
#
# $Log: cat,v $
# Revision 1.1.1.1 2001/06/06 08:54:05 sdague
# initial import
#
# Revision 1.1.1.1 2001/05/13 19:55:38 sdague
# added initial import of PPT work
#
# Revision 1.1 1999/02/26 03:21:14 abigail
# Initial revision
#
#

use strict;
use Getopt::Std;

my ($VERSION) = '$Revision: 1.1.1.1 $' =~ /([.\d]+)/;

# Get the options.

# Print a usuage message on a unknown option.
# Requires my patch to Getopt::Std of 25 Feb 1999.
$SIG {__WARN__} = sub {
if (substr ($_ [0], 0, 14) eq "Unknown option") {
$0 =~ s{.*/}{};
print <) {

if ($squeeze_empty) {
my $is_empty = /^$/;
if ($is_empty && $was_empty) {
next;
}
$was_empty = $is_empty;
}

$_ = sprintf "%6d $_", ++ $count if $number_lines ||
$number_non_blanks && /\S/;

$_ =~ s/$/\$/ if $ends;
if ($nonprinting) {
$_ =~ s/([\x80-\xFF])/"M-" . ("\x7F" & $1)/ge;
$_ =~ s/([\x00-\x08\x0B-\x1F])/"^" . chr (0100 + ord $1)/ge;
$_ =~ s/\x7F/^?/g;
}
if ($tabs) {
$_ =~ s/\x09/^I/g;
}

print;
}

__END__

=pod

=head1 NAME

cat -- concatenate and print files.

=head1 SYNOPSIS

cat [-benstuv] [file ...]

=head1 DESCRIPTION

I reads and prints the files in order they are given. If no files
are given, I is processed. A lone dash represents
I as well.

=head2 OPTIONS

I accepts the following options:

=over 4

=item -b

Number all the non blank lines, starting at 1.

=item -e

Print a dollar sign (B<$>) at the end of each lines. Implies I<-v>.

=item -n

Number all the lines, starting at 1.

=item -s

The I option. Sequential empty lines are squeezed into a
single empty line.

=item -t

Display tabs as I<^I>. Implies I<-v>.

=item -u

Unbuffer output.

=item -v

Display non-printable characters in a printable way. Characters in the
range I<\000> - I<\037>, with the exception of tabs and linefeeds, are
printed as I<^X>, where I is the symbol I<\0100> higher. I is
printed as I<^?>. Characters whose highest bit is set are printed as
I, followed by the representation of the character with the high
bit stripped.

=back

=head1 ENVIRONMENT

The working of I is not influenced by any environment variables.

=head1 BUGS

I has no known bugs.

=head1 STANDARDS

This I implementation is compliant with the B
specification, also known as B.

This I implementation is compatible with the B implementation.

=head1 REVISION HISTORY

$Log: cat,v $
Revision 1.1.1.1 2001/06/06 08:54:05 sdague
initial import

Revision 1.1.1.1 2001/05/13 19:55:38 sdague
added initial import of PPT work

Revision 1.1 1999/02/26 03:21:14 abigail
Initial revision

=head1 AUTHOR

The Perl implementation of I was written by Abigail, I.

=head1 COPYRIGHT and LICENSE

This program is copyright by Abigail 1999.

This program is free and open software. You may use, copy, modify, distribute
and sell this program (and any modified variants) in any way you wish,
provided you do not restrict others to do the same.

=cut

Tip Concatenating files with cat


Listing 3. Displaying non-printing characters
$ cat -t /etc/X11/XF86Config
...

# Multiple FontPath entries are allowed (they are concatenated together)

# By default, Red Hat 6.0 and later now use a font server independent of

# the X server to render fonts.


^IFontPath^I"/usr/X11R6/lib/X11/fonts/TrueType"

^IFontPath^I"unix/:7100"

EndSection
...


$ cat -E /etc/X11/XF86Config

...

# Multiple FontPath entries are allowed (they are concatenated together)$

# By default, Red Hat 6.0 and later now use a font server independent of$

# the X server to render fonts.$

$

    FontPath   "/usr/X11R6/lib/X11/fonts/TrueType"$

    FontPath   "unix/:7100"$

$

EndSection$

...

$ cat -v /etc/X11/XF86Config
...

^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@M-|M-8^X^@^@^@

P^@^O"M-X^O M-@^M^@^@^@M-^@^O"M-@M-k^@M-8*^@

@M-^H$M-@M-9|A(M-@)M-yM-|M-sM-*M-hW^A^@^@j^@

M-|M-sM-%1M-@M-9^@^B^@^@M-sM-+fM-^A= ^@ ^@

F^@^@   ^@M-9^@^H^@^@M-sM-$M-G^E(l!M-@M-^?

^IM-A5^@^@^D^@PM-^]M-^\X1M-H%^@^@^D^@tyM-G
...

Tip Reading text streams in chunks with head and tail

Reversing files with tac

What if you wanted to reverse the order of lines in a file? That is the job of the tac command. (Note that tac is cat spelled backwards.) It reverses the order of the lines or fields in a list of files.

It does not reverse the order of files -- this you must do yourself by listing them in reverse order after the tac command. For an example of how tac works, compare results of ls -l | tail and ls -l | tail | tac on some files in your home directory.

Questions or comments? I'd love to hear from you -- send mail to [email protected].

Next time, we'll take a look at the sort and tsort commands. See you then!

The Power of Z Commands – Zcat, Zless, Zgrep, Zdiff Examples

Example 1: View Compressed File and Uncompress with zcat

Compressing a file using gzip creates a compressed file with *.gz extension. You can view a compressed file with zcat with the following way. Which would be as same as the uncompressed file operation 'cat filename'. zcat uncompresses the file and shows it in the stdout.

$ zcat filename.gz | more
$ ls -l big-file.*
-rw-r--r-- 1 ramesh ramesh 24853275 May  9 15:14 big-file.txt

$ gzip big-file.txt 
[Note: Compress the file]

$ ls -l big-file.*
-rw-r--r-- 1 ramesh ramesh 9275204 May  9 15:14 big-file.txt.gz

$ zcat big-file.txt.gz 
[Note: View the file without uncompressing it]

zcat big-file.txt.gz > big-file.txt
[Note: Uncompress the file]
Example 2: View a gzipped file which don't have the gz suffix.

You can uncompress a gzipped file which don't have the gz suffix. If you try to uncompress a gzipped file which don't have the gz suffix with "gunzip" or "gzip -d" command you will face the following error.

gunzip: auth.log: unknown suffix -- ignored

But this zcat will uncompress the file and shows the content as shown below.

$ cat > test-file.txt
This is a test file used for gunzip and zcat testing

zcat is awesome command.  

$ gzip test-file.txt

$ mv test-file.txt.gz test-file-no-ext

$ gzip -d test-file-no-ext
gzip: test-file-no-ext: unknown suffix -- ignored

$ zcat test-file-no-ext
This is a test file used for gunzip and zcat testing

zcat is awesome command.

Example 3: Display the file content without worrying about whether it is compressed or not

When you are not sure whether a file is compressed or not, you can still view the file without worrying about it's compression status as shown below.

In this example, If the input-file is compressed zcat will display the content by uncompressing it. If the input-file is not compressed zcat will display the content as it is.

$ zcat -f input-file

Example 4: Paging the compressed file with zless / zmore.

You can paginate a compressed file with zless command or zmore command as shown below.

$ zcat filename.gz | more
$ zcat filename.gz | less

(or)

$ zless filename.gz
$ zmore filename.gz

Note: To open any kind of file type, refer to our previous article Open & View 10 Different File Types with Linux Less Command – The Ultimate Power of Less.

zcat Command

Expands a compressed file to standard output.

zcat [ -n ] [ -V ] [ File ... ]

The zcat command allows the user to expand and view a compressed file without uncompressing that file. The zcat command does not rename the expanded file or remove the .Z extension. The zcat command writes the expanded output to standard output.

Flags

-n Omits the compressed file header from the compressed file.

Note:

Use the -n option if the file was compressed using the -n option.
-V Writes the current version and compile options to standard error.

Parameters

File ... Specifies the compressed files to expand.

Return Values

If the zcat command exits with a status of 1 if any of the following events occur:

If no error occurs, the exit status is 0.

Exit Status

0 Successful completion.
>0 An error occurred.

Examples

To view the foo.Z file without uncompressing it, enter:

zcat foo.Z

The uncompressed contents of the foo.Z file are written to standard output. The file is not renamed.

Related Information

The compress command, pack command, uncompress command, unpack command.

Useless Use of Cat Award

The venerable Randal L. Schwartz hands out Useless Use of Cat Awards from time to time; you can see some recent examples in Deja News. (The subject line really says "This Week's Useless Use of Cat Award" although the postings are a lot less frequent than that nowadays). The actual award text is basically the same each time, and the ensuing discussion is usually just as uninteresting, but there are some refreshing threads there among all the flogging of this dead horse.

The oldest article Deja News finds is from 1995, but it's actually a followup to an earlier article. By Internet standards, this is thus an Ancient Tradition.

Exercise: Try to find statistically significant differences between the followups from 1995 and the ones being posted today.

(See below for a reconstruction of the Award text.)

Briefly, here's the collected wisdom on using cat:

The purpose of cat is to concatenate (or "catenate") files. If it's only one file, concatenating it with nothing at all is a waste of time, and costs you a process.
The fact that the same thread ("but but but, I think it's cleaner / nicer / not that much of a waste / my privelege to waste processes!") springs up virtually every time the Award is posted is also Ancient Usenet Tradition.

Of course, as Heiner points out, using cat on a single file to view it from the command line is a valid use of cat (but you might be better off if you get accustomed to using less for this instead).

In a recent thread on comp.unix.shell, the following example was posted by Andreas Schwab as another Useful Use of Cat on a lone file:

	{ foo; bar; cat mumble; baz } | whatever
Here, the contents of the file mumble are output to stdout after the output from the programs foo and bar, and before the output of baz. All the generated output is piped to the program whatever. (Read up on shell programming constructs if this was news to you :-)

[ Jan 16, 2007 ] Save time with text editing one-liners by Michael Stutz ([email protected])

Edits with cat

Use cat, whose name stands for together, to concatenate files and standard input streams, as in Listing 1. The slackers of the world also use it as a general pager (cat file) and a complete text-editing environment (cat > file). Its syntax is unrivaled in its simplicity and, for text editing one-liners, it gives you quick ways to append or insert text without an editor.

Listing 1. Using cat to concatenate files and standard input streams
$ (cat - input1 - input2 - input3 - input4) | mailx ted
Ted,

Take a look at these example files.

This is the first file ...
Ctrl-D

This is the second file ...
Ctrl-D

This is the third file -- note the fourth paragraph below ...
Ctrl-D

And here's the last file ...
Ctrl-D
$ 

Add text to the end of a file

The slackers are on to something, though. When you need to append text to the end of a file, there's nothing quicker than cat:

$ cat >> file
> line
> line
> line

Ctrl-D
$

While you're adding lines, pressing Ctrl-U erases the current line, Ctrl-Z suspends the process, and Ctrl-C aborts everything. When you're done, press Ctrl-D on a line of its own. (These are some of the default Korn shell control keys, but they work for most shells and editing modes.)

If the data you're entering is an X selection that you're pasting from another window, this one-liner is generally quicker to use than calling up an editor, opening the target file, moving to the end of the file, pasting the selection, saving the file, and exiting the editor. It can also be more useful when you're pasting formatted or specially formatted text, and you want to keep the formatting because some text editors and editing modes reformat the X selection when you paste it.

Although this operation is a common, everyday practice, you always have to be careful that you use the shell operator for appending redirection (>>) and not the regular redirection operator (>); if you mistakenly use the latter, you'll overwrite the contents of the file with the text you mean to append.

To add the entire contents of one file to the end of another file, give the filename:

$ cat footnotes.txt >> file

If you're appending only a single line instead of multiple lines or an entire file, you can use echo instead of cat:

$ echo "192.255.255.255     bigblue" >> /etc/hosts

To append lines of text that are itemized beginning with 1, use cat's -n option; lines are preceded with the line number (offset with up to five space characters) and a tab character. Add the -b option to suppress the numbering of blank lines:

$ cat -nb > file
This line is numbered
And so is this

Another numbered line

Ctrl-D
$ cat file
     1  This line is numbered
     2  And so is this

     3  Another numbered line

$

Insert text at the beginning of a file

You can insert text at the beginning of a file with cat by specifying the standard input with a hyphen (-) and writing to a new file:

$ cat - file > newfile
This is the beginning of the file
And then the old file is inserted
Below this line:
Ctrl-D
$

Although it's simple, the disadvantage of this one-liner is that it creates a new file. If you want to insert text into the original file, the renaming shuffle you have to do makes this almost more trouble than it's worth. Better ways are just ahead with ed.

Show nonprinting characters

cat has several useful options. Some of them control the way it outputs nonprinting characters, such as tabs and control characters. To determine whether a file or a group of text files has embedded control characters, use these options. For instance, if a file has trailing blanks, you can see them:

$ cat -vet input.txt

This line has trailing blanks.    $
This line does not.$
$

These options differ according to your UNIX implementation; Table 1 gives the standard IBM AIX® operating system options.

Recommended Links

dog -- enhanced cat with networking capabilities

Reference

Gnu version options

       -A, --show-all Equivalent to -vET

       -b, --number-nonblank 
	      number nonblank output lines

       -e     equivalent to -vE

       -E, --show-ends
	      display $ at end of each line

       -n, --number
	      number all output lines

       -s, --squeeze-blank
	      never more than one single blank line

       -t     equivalent to -vT

       -T, --show-tabs
	      display TAB characters as ^I

       -u     (ignored)

       -v, --show-nonprinting
	      use ^ and M- notation, except for LFD and TAB

       --help display this help and exit

       --version
	      output version information and exit

       With no FILE, or when FILE is -, read standard input.



Etc

Society

Groupthink : Two Party System as Polyarchy : Corruption of Regulators : Bureaucracies : Understanding Micromanagers and Control Freaks : Toxic Managers :   Harvard Mafia : Diplomatic Communication : Surviving a Bad Performance Review : Insufficient Retirement Funds as Immanent Problem of Neoliberal Regime : PseudoScience : Who Rules America : Neoliberalism  : The Iron Law of Oligarchy : Libertarian Philosophy

Quotes

War and Peace : Skeptical Finance : John Kenneth Galbraith :Talleyrand : Oscar Wilde : Otto Von Bismarck : Keynes : George Carlin : Skeptics : Propaganda  : SE quotes : Language Design and Programming Quotes : Random IT-related quotesSomerset Maugham : Marcus Aurelius : Kurt Vonnegut : Eric Hoffer : Winston Churchill : Napoleon Bonaparte : Ambrose BierceBernard Shaw : Mark Twain Quotes

Bulletin:

Vol 25, No.12 (December, 2013) Rational Fools vs. Efficient Crooks The efficient markets hypothesis : Political Skeptic Bulletin, 2013 : Unemployment Bulletin, 2010 :  Vol 23, No.10 (October, 2011) An observation about corporate security departments : Slightly Skeptical Euromaydan Chronicles, June 2014 : Greenspan legacy bulletin, 2008 : Vol 25, No.10 (October, 2013) Cryptolocker Trojan (Win32/Crilock.A) : Vol 25, No.08 (August, 2013) Cloud providers as intelligence collection hubs : Financial Humor Bulletin, 2010 : Inequality Bulletin, 2009 : Financial Humor Bulletin, 2008 : Copyleft Problems Bulletin, 2004 : Financial Humor Bulletin, 2011 : Energy Bulletin, 2010 : Malware Protection Bulletin, 2010 : Vol 26, No.1 (January, 2013) Object-Oriented Cult : Political Skeptic Bulletin, 2011 : Vol 23, No.11 (November, 2011) Softpanorama classification of sysadmin horror stories : Vol 25, No.05 (May, 2013) Corporate bullshit as a communication method  : Vol 25, No.06 (June, 2013) A Note on the Relationship of Brooks Law and Conway Law

History:

Fifty glorious years (1950-2000): the triumph of the US computer engineering : Donald Knuth : TAoCP and its Influence of Computer Science : Richard Stallman : Linus Torvalds  : Larry Wall  : John K. Ousterhout : CTSS : Multix OS Unix History : Unix shell history : VI editor : History of pipes concept : Solaris : MS DOSProgramming Languages History : PL/1 : Simula 67 : C : History of GCC developmentScripting Languages : Perl history   : OS History : Mail : DNS : SSH : CPU Instruction Sets : SPARC systems 1987-2006 : Norton Commander : Norton Utilities : Norton Ghost : Frontpage history : Malware Defense History : GNU Screen : OSS early history

Classic books:

The Peter Principle : Parkinson Law : 1984 : The Mythical Man-MonthHow to Solve It by George Polya : The Art of Computer Programming : The Elements of Programming Style : The Unix Hater’s Handbook : The Jargon file : The True Believer : Programming Pearls : The Good Soldier Svejk : The Power Elite

Most popular humor pages:

Manifest of the Softpanorama IT Slacker Society : Ten Commandments of the IT Slackers Society : Computer Humor Collection : BSD Logo Story : The Cuckoo's Egg : IT Slang : C++ Humor : ARE YOU A BBS ADDICT? : The Perl Purity Test : Object oriented programmers of all nations : Financial Humor : Financial Humor Bulletin, 2008 : Financial Humor Bulletin, 2010 : The Most Comprehensive Collection of Editor-related Humor : Programming Language Humor : Goldman Sachs related humor : Greenspan humor : C Humor : Scripting Humor : Real Programmers Humor : Web Humor : GPL-related Humor : OFM Humor : Politically Incorrect Humor : IDS Humor : "Linux Sucks" Humor : Russian Musical Humor : Best Russian Programmer Humor : Microsoft plans to buy Catholic Church : Richard Stallman Related Humor : Admin Humor : Perl-related Humor : Linus Torvalds Related humor : PseudoScience Related Humor : Networking Humor : Shell Humor : Financial Humor Bulletin, 2011 : Financial Humor Bulletin, 2012 : Financial Humor Bulletin, 2013 : Java Humor : Software Engineering Humor : Sun Solaris Related Humor : Education Humor : IBM Humor : Assembler-related Humor : VIM Humor : Computer Viruses Humor : Bright tomorrow is rescheduled to a day after tomorrow : Classic Computer Humor

The Last but not Least Technology is dominated by two types of people: those who understand what they do not manage and those who manage what they do not understand ~Archibald Putt. Ph.D


Copyright © 1996-2021 by Softpanorama Society. www.softpanorama.org was initially created as a service to the (now defunct) UN Sustainable Development Networking Programme (SDNP) without any remuneration. This document is an industrial compilation designed and created exclusively for educational use and is distributed under the Softpanorama Content License. Original materials copyright belong to respective owners. Quotes are made for educational purposes only in compliance with the fair use doctrine.

FAIR USE NOTICE This site contains copyrighted material the use of which has not always been specifically authorized by the copyright owner. We are making such material available to advance understanding of computer science, IT technology, economic, scientific, and social issues. We believe this constitutes a 'fair use' of any such copyrighted material as provided by section 107 of the US Copyright Law according to which such material can be distributed without profit exclusively for research and educational purposes.

This is a Spartan WHYFF (We Help You For Free) site written by people for whom English is not a native language. Grammar and spelling errors should be expected. The site contain some broken links as it develops like a living tree...

You can use PayPal to to buy a cup of coffee for authors of this site

Disclaimer:

The statements, views and opinions presented on this web page are those of the author (or referenced source) and are not endorsed by, nor do they necessarily reflect, the opinions of the Softpanorama society. We do not warrant the correctness of the information provided or its fitness for any purpose. The site uses AdSense so you need to be aware of Google privacy policy. You you do not want to be tracked by Google please disable Javascript for this site. This site is perfectly usable without Javascript.

Last modified: February 19, 2020