World Library  
Flag as Inappropriate
Email this Article

Cat (Unix)

Article Id: WHEBN0039127836
Reproduction Date:

Title: Cat (Unix)  
Author: World Heritage Encyclopedia
Language: English
Subject: Sort (Unix), Split (Unix), Wall (Unix), NewDos/80, TRS-DOS
Collection: Standard Unix Programs, Unix Sus2008 Utilities, Unix Text Processing Utilities
Publisher: World Heritage Encyclopedia
Publication
Date:
 

Cat (Unix)

The cat program is a standard Unix utility that will output the contents of a specific file and can be used to concatenate and list files. The name is an abbreviation of catenate, which is the verb from which the adjective concatenate was derived, the adjective thereafter being mistakenly reused as a synonymous verb for catenate.

Contents

  • History 1
  • Usage 2
    • Options 2.1
  • Use cases 3
    • Text use 3.1
    • Other files 3.2
  • Unix culture 4
    • Jargon File definition 4.1
    • Useless use of cat 4.2
      • Benefits of using cat 4.2.1
      • Culture 4.2.2
  • Other operating systems 5
  • See also 6
  • References 7
  • External links 8
    • Manual pages 8.1
    • Other 8.2

History

Cat is probably the oldest distinctively Unix utility. It was part of Version 1, and replaced pr, a PDP-7 utility for copying a single file to the screen.[1]

Usage

The Single Unix Specification specifies that when the "cat" program is given files in a sequence as arguments, it will output their contents to the standard output in the same sequence. It mandates the support of one option flag, u (unbuffered), by which each byte is written to standard output without buffering as it is read. Many operating systems do this by default and ignore the flag.

If one of the input filenames is specified as a single hyphen (-), then cat reads from standard input at that point in the sequence. If no files are specified, cat reads from standard input only.

The cat command-syntax is:

cat [options] [file_names]

cat will concatenate (put together) the input files in the order given, and if no other commands are given, will print them on the screen as standard output. It can also be used to print the files into a new file as follows:

cat [options] [file_names] > newfile.txt


You can also use a pipe to send the data to a different program. For example to view two files in sequence line by line using the less command, you would use the following command:

cat file1 file2 | less


Options

Both the BSD versions of cat (as per the OpenBSD manpage) and the GNU coreutils version of cat specify the following options:

(GNU only: ), number non-blank output lines
-e
implies -v but also display end-of-line characters as (GNU only: -E the same, but without implying -v)
-n
(GNU only: --number), number all output lines
-s
(GNU only: --squeeze-blank), squeeze multiple adjacent blank lines
-t
implies -v but also display tabs as (GNU only: -T the same, but without implying -v)
-u
use unbuffered I/O for stdout. POSIX does not say what the behavior is without this option.
-v
(GNU only: --show-nonprinting), displays nonprinting characters as if they were visible, except for tabs and the end of line character

Use cases

cat can be used to pipe a file to a program that expects plain text or binary data on its input stream. cat does not destroy non-text bytes when concatenating and outputting. As such, its two main use cases are text files and certain format-compatible types of binary files.

Text use

As a simple example, to concatenate 2 text files and write them to a new file, you can use the following command:

cat file1.txt file2.txt > newcombinedfile.txt

Some implementations of cat, with option -n, cat can also number lines as follows:

cat -n file1.txt file2.txt > newnumberedfile.txt

Concatenation of text is limited to text files using the same legacy encoding, such as ASCII. cat does not provide a way to concatenate Unicode text files that have a Byte Order Mark or files using different text encodings from each other.

Other files

For many structured binary data sets, the resulting combined file may not be valid; for example, if a file has a unique header or footer, the result will spuriously duplicate these. However, for some multimedia digital container formats, the resulting file is valid, and so cat provides an effective means of appending files. Video streams can be a significant example of files that cat can concatenate without issue, e.g. the MPEG program stream (MPEG-1 and MPEG-2) and DV (Digital Video) formats, which are fundamentally simple streams of packets.

Unix culture

Jargon File definition

The Jargon File version 4.4.7 lists this as the definition of cat:

Useless use of cat

UUOC (from comp.unix.shell on Usenet) stands for "useless use of cat". comp.unix.shell observes: "The purpose of cat is to concatenate (or catenate) files. If it is only one file, concatenating it with nothing at all is a waste of time, and costs you a process." This is also referred to as "cat abuse". Nevertheless the following usage is common:

cat filename | command arg1 arg2 argn

This can be rewritten using redirection of stdin instead, in either of the following forms (the latter is more traditional):


    

Beyond other benefits, the input redirection forms allow command to seek in the file, whereas the cat examples do not: cat will prevent the command from seeking in the file.

Another common case where cat is unnecessary is where a command defaults to operating on stdin, but will read from a file, if the filename is given as an argument. This is the case for many common commands; the following examples:

 cat -- "$file" | grep -- "$pattern"
 cat -- "$file" | less

can instead be written as:

 grep -- "$pattern" "$file"
 less "$file"

A common interactive use of cat for a single file is to output the content of a file to standard output. However, if the output is piped or redirected, cat is unnecessary.

Without two named files, the use of cat has no significant benefits. A UUOC campaign will eliminate the inefficiency from shell scripts by using redirection instead.

Benefits of using cat

The primary benefits of using cat, even when unnecessary, are to avoid human error and for legibility. cat with one named file is safer where human error is a concern — one wrong use of the redirection symbol ">" instead of "<" (often adjacent on keyboards) may permanently delete[2] the file you were just needing to read.[3] In terms of legibility, a sequence of commands starting with cat and connected by pipes has a clear left-to-right flow of information, in contrast with the back-and-forth syntax and backwards-pointing arrows of using stdin redirection. Contrast:

command < in | command2 > out
 out

with:

cat in | command | command2 > out

Culture

Since 1995, occasional awards for UUOC have been given out, usually by Perl programmer Randal L. Schwartz. In British hackerdom the activity of fixing instances of UUOC is sometimes called demoggification.[4]

Other operating systems

The equivalent command in the VMS, CP/M, DOS, OS/2, and Microsoft Windows operating system command shells is type.

In DOS/Windows multiple files may be combined with the "copy /b" command syntax, for example:

copy /b file1.txt + file2.txt file3.txt

This copies file1.txt and file2.txt in binary mode to one file, file3.txt.

See also

  • tac
  • paste
  • split, a command that splits a file into pieces which cat can then rejoin.
  • zcat

References

  1. ^
  2. ^ More accurately stated ">" will truncate the file.
  3. ^ The default behavior for redirection is to clobber the file to its immediate right.
  4. ^ moggy is a chiefly British word for "(mongrel) cat", hence demoggification literally means "removal of (non-special) cats".

External links

  • UNIX Style, or cat -v Considered Harmful - A paper by Rob Pike on proper Unix command design using cat as an example.

Manual pages

  • cat(1) original manual page in the First Edition of Unix.
  • : catenate files – Plan 9 from Bell Labs manual

Other

  • Useless Use Of Cat Award
This article was sourced from Creative Commons Attribution-ShareAlike License; additional terms may apply. World Heritage Encyclopedia content is assembled from numerous content providers, Open Access Publishing, and in compliance with The Fair Access to Science and Technology Research Act (FASTR), Wikimedia Foundation, Inc., Public Library of Science, The Encyclopedia of Life, Open Book Publishers (OBP), PubMed, U.S. National Library of Medicine, National Center for Biotechnology Information, U.S. National Library of Medicine, National Institutes of Health (NIH), U.S. Department of Health & Human Services, and USA.gov, which sources content from all federal, state, local, tribal, and territorial government publication portals (.gov, .mil, .edu). Funding for USA.gov and content contributors is made possible from the U.S. Congress, E-Government Act of 2002.
 
Crowd sourced content that is contributed to World Heritage Encyclopedia is peer reviewed and edited by our editorial staff to ensure quality scholarly research articles.
 
By using this site, you agree to the Terms of Use and Privacy Policy. World Heritage Encyclopedia™ is a registered trademark of the World Public Library Association, a non-profit organization.
 


Copyright © World Library Foundation. All rights reserved. eBooks from Project Gutenberg are sponsored by the World Library Foundation,
a 501c(4) Member's Support Non-Profit Organization, and is NOT affiliated with any governmental agency or department.