Questions tagged [cat]

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

Filter by
Sorted by
Tagged with
291 votes
9 answers
1.4m views

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's user avatar
  • 3,023
192 votes
8 answers
365k views

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 /...
Amelio Vazquez-Reina's user avatar
185 votes
6 answers
526k views

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's user avatar
  • 1,959
161 votes
9 answers
53k views

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 ...
kizzx2's user avatar
  • 1,833
145 votes
7 answers
34k views

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 ...
JigarGandhi's user avatar
  • 4,920
135 votes
3 answers
102k views

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 ...
strugee's user avatar
  • 14.9k
98 votes
7 answers
106k views

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 ...
Sebastian's user avatar
  • 8,787
95 votes
9 answers
30k views

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....
0xC0000022L's user avatar
  • 16.6k
89 votes
7 answers
330k views

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 #...
Eugene Yarmash's user avatar
86 votes
8 answers
218k views

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 ...
jkj2000's user avatar
  • 1,149
85 votes
4 answers
39k views

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 ...
polarise's user avatar
  • 1,041
82 votes
7 answers
12k views

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, ...
Gunchars's user avatar
  • 911
79 votes
5 answers
21k views

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 ...
rookie's user avatar
  • 833
74 votes
18 answers
80k views

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? ...
Ram Rachum's user avatar
  • 1,815
62 votes
3 answers
58k views

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: ...
user avatar
59 votes
4 answers
8k views

Should I care about unnecessary cats?

A lot of command-line utilities can take their input either from a pipe or as a filename argument. For long shell scripts, I find starting the chain off with a cat makes it more readable, especially ...
user avatar
56 votes
2 answers
170k views

cat files in current folder and all subfolders [duplicate]

I want to cat a file in current folder and all files in all subfolders (and subsubfolders). Here is my directory structure $ tree . ├── f │   └── foo └── yo I want to cat foo and yo. I've tried ...
Mohsen's user avatar
  • 2,575
54 votes
1 answer
5k views

Change permisions of a file with my cat's help

root@system:~# less myfile -bash: /bin/less: Input/output error The root filesystem is dead. But my cat is still alive (in my memory): root@system:~# cat > /tmp/somefile C^d root@system:~# He's ...
loopbackbee's user avatar
  • 4,542
52 votes
5 answers
16k views

How can I display syntax highlighting on a file?

Is there a variant of cat that outputs syntax-highlighted lines of code when used on a source file? An idea: maybe vi[m] or another editor can be asked to dump the syntax-highlighted contents of said ...
Paolo's user avatar
  • 1,525
50 votes
2 answers
230k views

How to assign the cat output of a bash script to a variable in another script

I have a bash script that produces a cat output when it takes an argument. I also have another bash script that executes the first bash script with an an argument that I want to produce cat outputs ...
eltigre's user avatar
  • 1,175
49 votes
8 answers
97k views

Command to output file content to stdout?

I know cat can do this, but its main purpose is to concatenate rather than just to display the content. I also know about less and more, but I'm looking for something simple (not a pager) that just ...
confused00's user avatar
46 votes
4 answers
37k views

How do I "cat and follow" a file?

A file is being sequentially downloaded by wget. If I start unpacking it with cat myfile.tar.bz2 | tar -xj, it may unpack correctly or fail with "Unexpected EOF", depending on what is faster. How to ...
Vi.'s user avatar
  • 5,678
42 votes
12 answers
82k views

How can I "cat" a file and remove commented lines?

I'd like to know if there is a way that I could cat file like php.ini and remove all lines starting with ; For example, if the file contained this: ; - Show all errors, except for notices ; ;...
cwd's user avatar
  • 45.2k
41 votes
4 answers
133k views

How to use "cat" command on "find" command's output?

I want to redirect the output of the find command to cat command so I can print the data of the given file. So for example if the output of find is /aFile/readme then the cat should be interpreted ...
Eksapsy's user avatar
  • 521
38 votes
4 answers
39k views

How to pipe a remote file to stdout over scp or sftp?

Using ssh, it is easy to print the contents of a file using ssh host 'cat file.txt' When ssh is disabled, and only SFTP is enabled, running the previous command gives the following error: This ...
Rob W's user avatar
  • 918
37 votes
7 answers
4k views

Is it a UUOC (useless use of cat) to redirect one file to another?

If I want to make the contents of file2 match the contents of file1, I could obviously just run cp file1 file2. However, if I want to preserve everything about file2 except the contents—owner, ...
Wildcard's user avatar
  • 36.5k
34 votes
7 answers
47k views

How to make bash put prompt on a new line after cat command?

What I get: host:~ user$ cat example.txt some texthost:~ stas$ What I want to get: host:~ user$ cat example.txt some text host:~ stas$ Is there a way I can make cat behave like this? I'm using ...
Stanislav Shabalin's user avatar
33 votes
6 answers
11k views

Is 'cat' a shell built-in or an external program?

When I use the type command to find out if cat is a shell built-in or an external program I get the output below: -$ type cat cat is hashed (/bin/cat) -$ Does this mean that cat is an external ...
sps's user avatar
  • 1,426
32 votes
1 answer
28k views

Append same text to many files using cat or echo?

How can I write the same content to many text files by using cat or echo in only one command? For example I want to write "hello" to file1 and file2. I tried: echo "hello" >> file1 file2 but ...
neo0's user avatar
  • 755
31 votes
7 answers
66k views

Is there a way to show only uncommented lines in a text file/script? [duplicate]

Many a times when manually grepping through a file, there are so many comments that your eyes glaze over and you start to wish there was a way in which you just could show it to display only those ...
shirish's user avatar
  • 12.3k
31 votes
2 answers
48k views

Fastest way to concatenate files

I've got 10k+ files totaling over 20GB that I need to concatenate into one file. Is there a faster way than cat input_file* >> out ? The preferred way would be a bash command, Python is ...
fsperrle's user avatar
  • 495
31 votes
2 answers
3k views

When using cat to create file, I have to type ctrl+d twice to finish the input, is this expected? [duplicate]

I just learned a trick to create a new file with the cat command. By my testing, if the last line is not followed by a newline, I have to type ctrl+d twice to finish the input, as demonstrated below. ...
Fajela Tajkiya's user avatar
29 votes
5 answers
9k views

Why pipe to cat only to redirect? [duplicate]

I occasionally see things like: cat file | wc | cat > file2 Why do this? When will the results (or performance) differ (favourably) from simply: cat file | wc > file2
OJFord's user avatar
  • 1,963
29 votes
6 answers
15k views

cat a very large number of files together in correct order

I have about 15,000 files that are named file_1.pdb, file_2.pdb, etc. I can cat about a few thousand of these in order by doing: cat file_{1..2000}.pdb >> file_all.pdb However, if I do this ...
sodiumnitrate's user avatar
28 votes
1 answer
31k views

How to tail/grep/awk the last N bytes of a file, rather than lines

I have an application that is logging to a plain text log file (myapp.log) but it doesn't seem to be writing new line characters at the end of each log entry. If I execute a command like tail -n 50 ...
Baldrick's user avatar
  • 7,622
28 votes
1 answer
9k views

How to implement a horizontal cat? [duplicate]

Standard cat concatenates files line by line (row by row, if you will). I find myself needing a horizontal cat command more and more often recently; i.e. a command that takes a list of files and ...
Konrad Rudolph's user avatar
26 votes
9 answers
71k views

Command to display first few and last few lines of a file

I have a file with many rows, and each row has a timestamp at the starting, like [Thread-3] (21/09/12 06:17:38:672) logged message from code..... So, I frequently check 2 things from this log file. ...
mtk's user avatar
  • 27.4k
25 votes
2 answers
35k views

Ctrl+D for ending terminal line input

If I do $ cat > file.txt textCtrl-DCtrl-D Question 1: If I don't press enter, why do I have to press Ctrl-D twice? If I do $ cat > file.txt pa bam pshhhCtrl-Z [2]+ Stopped ...
mist's user avatar
  • 1,105
25 votes
6 answers
96k views

How to pause listing long text file with cat in command prompt after 10lines then press any key

How to pause listing long text file with cat in command prompt after 10lines then press any key. for example: cat myfiles.txt bla bla bla bla bla bla . . . bla press enter to continue
easyyu's user avatar
  • 357
25 votes
5 answers
29k views

How to cat files together, adding missing newlines at end of some files

I have a bunch of .text files, most of which end with the standard nl. A couple don't have any terminator at end. The last physical byte is (generally) an alphameric character. I was using cat *....
HiTechHiTouch's user avatar
24 votes
5 answers
23k views

Difference between cat and '>' to zero out a file

Are these two commands any different on how they go about zero-ing out files? Is the latter a shorter way of doing the former? What is happening behind the scenes? Both $ cat /dev/null > file....
KM.'s user avatar
  • 2,224
24 votes
4 answers
11k views

tee + cat: use an output several times and then concatenate results

If I call some command, for instance an echo I can use the results from that command in several other commands with tee. Example: echo "Hello world!" | tee >(command1) >(command2) >(command3)...
Trylks's user avatar
  • 393
23 votes
8 answers
17k views

Change tab size of "cat" command

When I am in vim I can change the tab size with the following command: :set ts=4 Is it possible to set tab size for cat command output too?
B Faley's user avatar
  • 4,313
23 votes
2 answers
30k views

Unexpected results testing serial loopback using echo and cat

So I have a standard RS232 serial port that is looped back to itself by simply running a wire from Tx to Rx. I'm testing loopback by running echo and cat in two separate terminals: cat /dev/ttyS1 ...
Kristina's user avatar
  • 751
22 votes
3 answers
39k views

Why does the less-than sign not work as a replacement for cat in bash?

I heard about "useless use of cat" and found some suggestions, but the following outputs nothing in my bash shell. < filename Using cat works as expected though. cat filename I'm using Fedora ...
bug's user avatar
  • 2,478
22 votes
2 answers
5k views

cat gives different error when opening non-existing file

Both of the following commands try to open a non-existing file foo, but the error messages are a little different. What could be the reason? $ cat foo cat: cannot open foo $ cat < foo -bash: foo: ...
Zain Rafi's user avatar
  • 265
22 votes
2 answers
90k views

How can I concatenate all files in a directory together in one command line operation? [duplicate]

I have 1000 csv files in a directory. I would like to concatenate them all together in order. They are named img_size_1.csv to approx img_size_1000.csv This answer is close but assumes a list file. ...
codecowboy's user avatar
  • 3,422
22 votes
4 answers
14k views

Is it possible to merge video files using `cat`?

I wonder if it's possible to merge video files using the cat command? I mean will the resultant file play seamlessly?
2hamed's user avatar
  • 441
22 votes
3 answers
79k views

continuous reading from named pipe (cat or tail -f)

I have configured rsyslog to log certain log events to /dev/xconsole: *.*;cron.!=info;mail.!=info |/dev/xconsole /dev/xconsole is a named pipe (fifo). If I want to see what is being logged, I ...
Martin Vegter's user avatar
21 votes
9 answers
37k views

Displaying file names with contents when using the "cat" command

Is there a command to show the directory or file name when using cat to display the contents of files? For example: assume two files f1.txt and f2.txt are in ./tmp ./tmp/f1.txt ./tmp/f2.txt Then ...
user avatar

1
2 3 4 5
14