Questions tagged [cat]

cat is a standard Unix utility used for concatenating files or printing specific file on the standard output.

cat is a standard Unix utility used for concatenating files or printing specific file on the standard output.

The cat program is given files in a sequence as arguments, it will output their contents to the standard output in the same sequence.

Examples

Printing the contents of a file on the standard output.

cat -n file.txt # n stands for line-numbers

Concatenating two files into third file:

cat file1.txt file2.txt > output_file

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    
cat -- "$file" | grep -- "$pattern"
cat -- "$file" | less

can instead be written as:

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

External reference

Further reading

675 questions
280
votes
9 answers

How can I display the contents of a text file on the command line?

I would like to display the contents of a text file on the command line. The file only contains 5-6 characters. Is there an easy way to do this?
Sam Weinberg
  • 2,913
  • 2
  • 13
  • 5
189
votes
8 answers

cat line X to line Y on a huge file

Say I have a huge text file (>2GB) and I just want to cat the lines X to Y (e.g. 57890000 to 57890010). From what I understand I can do this by piping head into tail or viceversa, i.e. head -A /path/to/file | tail -B or alternatively tail -C…
Amelio Vazquez-Reina
  • 40,169
  • 77
  • 197
  • 294
174
votes
6 answers

With the Linux "cat" command, how do I show only certain lines by number

If I use cat -n text.txt to automatically number the lines, how do I then use the command to show only certain numbered lines.
Geckono1
  • 1,849
  • 2
  • 10
  • 3
161
votes
9 answers

dd vs cat -- is dd still relevant these days?

I recently realized we can use cat as much as dd, and it's actually faster than dd I know that dd was useful in dealing with tapes where block size actually mattered in correctness, not just performance. In these days, though, are there situations…
kizzx2
  • 1,833
  • 2
  • 12
  • 9
144
votes
7 answers

How to kill a runaway cat?

Many times I accidentally run the cat command on files that have contents up to few thousand lines. I try to kill the cat command with Ctrl+ C or Ctrl+Z, but both only take effect after the total output of cat is displayed in the terminal, so I have…
JigarGandhi
  • 4,820
  • 10
  • 27
  • 38
131
votes
3 answers

How do you use output redirection in combination with here-documents and cat?

Let's say I have a script that I want to pipe to another command or redirect to a file (piping to sh for the examples). Assume that I'm using bash. I could do it using echo: echo "touch somefile echo foo > somefile" | sh I could also do almost the…
strugee
  • 14,723
  • 17
  • 73
  • 119
97
votes
7 answers

Is there a simple command for outputting tab-delimited columns?

E.g. I have a file (produced with echo -e "var1\tvar2\t\var3\tvar4" > foo) that are output as: $ cat foo case elems meshing nlsys uniform 2350 0.076662 2.78 non-conformal 348 0.013332 0.55 scale 318 0.013333 …
Sebastian
  • 8,677
  • 4
  • 39
  • 49
95
votes
9 answers

Is there a tool that combines zcat and cat transparently?

When handling log files, some end up as gzipped files thanks to logrotate and others not. So when you try something like this: $ zcat * you end up with a command line like zcat xyz.log xyz.log.1 xyz.log.2.gz xyz.log.3.gz and then with: gzip:…
0xC0000022L
  • 16,189
  • 24
  • 102
  • 168
87
votes
7 answers

How can I read line by line from a variable in bash?

I have a variable which contains multiline output of a command. What's the most effecient way to read the output line by line from the variable? For example: jobs="$(jobs)" if [ "$jobs" ]; then # read lines from $jobs fi
Eugene Yarmash
  • 14,675
  • 14
  • 50
  • 57
83
votes
8 answers

get first X characters from the cat command?

I have a text file I'm outputting to a variable in my shell script. I only need the first 50 characters however. I've tried using cat ${filename} cut -c1-50 but I'm getting far more than the first 50 characters? That may be due to cut looking for…
jkj2000
  • 1,109
  • 2
  • 9
  • 9
83
votes
4 answers

How can I get the tac command on OS X?

I like to use tac to reverse the output of cat. However, it's not available in the Mavericks terminal. I tried to find it on MacPorts and again it's not available. Can anyone please show me how to get tac? It's very helpful for reading log files.
polarise
  • 1,021
  • 1
  • 7
  • 9
82
votes
7 answers

How safe is it to cat an arbitrary file?

Sometimes when I cat a binary file by mistake, my terminal gets garbled up. Nothing a quick reset can't fix, but couldn't an attacker theoretically create a file that, when displayed on a terminal, would execute some arbitrary code? Through an…
Gunchars
  • 911
  • 1
  • 6
  • 7
79
votes
5 answers

Difference between "cat" and "cat <"

I was working through a tutorial and saw use of both cat myfile.txt and cat < myfile.txt. Is there a difference between these two sequences of commands? It seems both print the contents of a file to the shell.
rookie
  • 833
  • 1
  • 6
  • 6
74
votes
18 answers

Is there ever a reason to use `cat` when you've got `less`?

I used to use cat to view files. Then I learned that less is usually better, and is a must if the file is longer than a few dozen rows. My question: Is there ever a reason to use cat instead of less? Is there any situation where cat is a better…
Ram Rachum
  • 1,795
  • 2
  • 15
  • 18
59
votes
3 answers

Is it better to use cat, dd, pv or another procedure to copy a CD/DVD?

Background I'm copying some data CDs/DVDs to ISO files to use them later without the need of them in the drive. I'm looking on the Net for procedures and I found a lot: Use of cat to copy a medium:…
user129371
1
2 3
44 45