Questions tagged [cat]

The cat command is a standard Unix program used to concatenate and display files. The name is from catenate, a synonym of concatenate.

cat - concatenate files and print on the standard output.

The cat command is used for:

  1. Display text file on screen
  2. Read text file
  3. Create a new text file
  4. File concatenation
  5. Modifying file

Example

$ cat -n file #shows the content of a file, including the line-numbers(-n flag)

Concatenating two files into third file:

cat file1 file2 > 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"

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

Without two named files, the use of cat has no significant benefits.

Useful links

1519 questions
471
votes
21 answers

Concatenate multiple files but include filename as section headers

I would like to concatenate a number of text files into one large file in terminal. I know I can do this using the cat command. However, I would like the filename of each file to precede the "data dump" for that file. Anyone know how to do…
Nick
  • 5,411
  • 4
  • 19
  • 7
311
votes
11 answers

How do I read the first line of a file using cat?

How do I read the first line of a file using cat?
Doboy
  • 10,411
  • 11
  • 40
  • 48
221
votes
14 answers

Can linux cat command be used for writing text to file?

Is something like this: cat "Some text here." > myfile.txt Possible? Such that the contents of myfile.txt would now be overwritten to: Some text here. This doesn't work for me, but also doesn't throw any errors. Specifically interested in a…
IAmYourFaja
  • 55,468
  • 181
  • 466
  • 756
185
votes
9 answers

Concatenating Files And Insert New Line In Between Files

I have multiple files which I want to concat with cat. Let's say File1.txt foo File2.txt bar File3.txt qux I want to concat so that the final file looks like: foo bar qux Instead of this with usual cat File*.txt > finalfile.txt foo bar…
neversaint
  • 60,904
  • 137
  • 310
  • 477
148
votes
9 answers

Useless use of cat?

This is probably in many FAQs - instead of using: cat file | command (which is called useless use of cat), correct way supposed to be: command < file In the 2nd, "correct" way - OS does not have to spawn an extra process. Despite knowing that, I…
Leonid Volnitsky
  • 8,854
  • 5
  • 38
  • 53
101
votes
6 answers

Why sudo cat gives a Permission denied but sudo vim works fine?

I am trying to automate the addition of a repository source in my arch's pacman.conf file but using the echo command in my shell script. However, it fails like this:- sudo echo "[archlinuxfr]" >> /etc/pacman.conf sudo echo "Server =…
Calvin Cheng
  • 35,640
  • 39
  • 116
  • 167
74
votes
4 answers

Recursively cat all the files into single file

I have bunch of files sitting in folders like data\A\A\A\json1.json data\A\A\A\json2.json data\A\A\B\json1.json ... data\Z\Z\Z\json_x.json I want to cat all the jsons into one single file?
frazman
  • 32,081
  • 75
  • 184
  • 269
72
votes
5 answers

is there a way to see the actual contents of a symlink?

When you do cat some-symlink-to-some-real-file it shows the contents of the real file, not what is within the symlink itself. Is there a way to see what's actually in it?
fabio
  • 2,269
  • 5
  • 22
  • 34
62
votes
2 answers

How to get the last line of a file using cat command

I am writing a shell script in OSX(unix) environment. I have a file called test.properties with the following content: cat test.properties gets the following output: //This file is intended for //blah blah purposes 123 Using cat command, how can I…
TheWaterProgrammer
  • 7,055
  • 12
  • 70
  • 159
40
votes
5 answers

Remove the first word in a text stream

How would I remove the first word from each line of text in a stream? For example, $ cat myfile some text 1 some text 2 some text 3 I want: $ cat myfile | magiccommand text 1 text 2 text 3 How would I go about this using Bash? I could use awk…
Trcx
  • 4,164
  • 6
  • 30
  • 30
39
votes
7 answers

How to display contents of all files under a directory on the screen using unix commands

Using cat command as follows we can display content of multiple files on screen cat file1 file2 file3 But in a directory if there are more than 20 files and I want content of all those files to be displayed on the screen without using the cat…
bvb
  • 633
  • 2
  • 7
  • 15
39
votes
3 answers

Concatenate numerical values in a string

I would like to store this output in a string: > x=1:5 > cat("hi",x) hi 1 2 3 4 5 So I use paste, but I obtain this different result: > paste("hi",x) [1] "hi 1" "hi 2" "hi 3" "hi 4" "hi 5" Any idea how to obtain the string: "hi 1 2 3 4 5" Thank…
kahlo
  • 2,314
  • 3
  • 28
  • 37
35
votes
3 answers

Howto merge two avi files using ffmpeg?

I'm unable to merge two avi videos together. google is full of below examples: cat file1.avi file2.avi file3.avi > video_draft.avi after appending the data together using cat above, you need to re-index the draft movie like this: mencoder…
teslasimus
  • 1,238
  • 5
  • 15
  • 23
27
votes
7 answers

in R, can I stop print(cat("")) from returning NULL? and why does cat("foo") return foo>

If I enter print(cat("")) I get NULL I want to use cat() to print out the progress of an R script, but I don't understand why it is returning NULL at the end of all of my concatenated strings, and more importantly, how to get it to stop?
David LeBauer
  • 31,011
  • 31
  • 115
  • 189
26
votes
4 answers

Bash: add string to the end of the file without line break

How can I add string to the end of the file without line break? for example if i'm using >> it will add to the end of the file with line break: cat list.txt yourText1 root@host-37:/# echo yourText2 >> list.txt root@host-37:/# cat…
Crazy_Bash
  • 1,755
  • 10
  • 26
  • 38
1
2 3
99 100