Hot answers tagged cat
59
In appearance, dd is a tool from an IBM operating system that's retained its foreign appearance (its parameter passing), which performs some very rarely-used functions (such as EBCDIC to ASCII conversions or endianness reversal… not a common need nowadays).
I used to think that dd was faster for copying large blocks of data on the same disk (due to more ...
49
Although both commands allow you to view the content of a file, their original purposes are quite different.
less extends the capabilities of more. The latter was created to view the content of a file one screenful at a time. less adds features such as backward movements and better memory management (no need to read the entire file before being able to see ...
20
Most terminal emulators will send back some response, if they receive certain escape sequences (have a look at the xterm control sequences documentation). E.g., you can send \e[0c to a VT100-like emulator and it will send back the device attributes,
something like \e[?1;2c
(This is probably what Keith observed.) But these answers are not arbitrary strings. ...
19
The "definitive" answer is of course brought to you by The Useless Use of cat Award.
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.
Instantiating cat just so your code reads differently makes for just one more process and one more set ...
18
It may be useful to explain how files work at the lowest level:
A file is a stream of bytes, zero or more in length. A byte is 8 bits. Since there are 256 combinations of 8 bits, that means a byte is any number from 0 to 255. So every file is, at its lowest level, a big hunk of numbers ranging from 0 to 255.
It is completely up to programs and users to ...
18
Whether such output can be exploited depends on the terminal program, and what that terminal does depending on escape codes that are being sent.
I am not aware of terminal programs having such exploitable features, and the only problem now would be if there is an unknown buffer overflow or something like that, that could be exploited.
With some older ...
14
You can use a while loop with process substitution:
while read -r line
do
echo "$line"
done < <(jobs)
To read a multiline variable, a simple way is:
printf %s "$var" | while IFS= read -r line
do
echo "$line"
done
Also, please don't call your variable jobs because that is a shell command and may cause confusion.
13
I usually use the column program for this, it's in a package called bsdmainutils on Debian:
column -t foo
Output:
case elems meshing nlsys
uniform 2350 0.076662 2.78
non-conformal 348 0.013332 0.55
scale 318 0.013333 0.44
smarter 504 0.016666 0.64
submodel 360 .009999 0.40
unstruct-quad 640 ...
12
Yes. It has to do with ^D really does: it just stops the current read(2) call.
If the program does int rdbytes = read(fd, buffer, sizeof buffer); and you press ^D inbetween, read() returns with the currently read bytes in the buffer, returning their number. The same happens on line termination; the \n at the end is always delivered.
So only a ^D at the ...
12
AProgrammer's suggestion of using xargs is often best, but another option is to use redirection into a while loop, which allows additional commands to be made and variables to be set:
while read -r dir; do mkdir $dir; done < myfile
An example of a more complicated structure would be:
now=`date +%Y%m%d.%H%M%S`
while read -r dir; do
...
11
The first command here emulates the formatting you see in vim. It intelligently expands tabs to the equivalent number of spaces, based on a tab-STOP (ts) setting of every 4 columns.
printf "ab\tcd\tde\n" |expand -t4
Output
ab cd de
To keep the tabs as tabs and have the tab STOP positions set to every 4th column, then you must change the way ...
11
Those trailing newlines are added by nano, not by cat.
Use nano's -L parameter:
-L (--nonewlines)
Don't add newlines to the ends of files.
Or ~/.nanorc's nonewlines command:
set/unset nonewlines
Don't add newlines to the ends of files.
10
I personally prefer view for static content or tail -f for dynamic content.
This does not answer your question, though. There is a saying "why use more if you have less" ;-)
But there are cases where I prefer cat to less:
I usually work with X11-windows. These windows have a scroll-buffer which can be set to some hundred lines.
Doing a cat for - let's say ...
10
Run reset. From the man page:
When invoked as reset, tset sets cooked and echo modes, turns off cbreak and raw modes, turns on newline translation and resets any unset special characters to their default values before doing the terminal initialization described above. This is useful after a program dies leaving a terminal in an abnormal state.
10
The dd command includes LOTS of options that cat is not able to accommodate. Perhaps in your usage cases cat is a workable substitute, but it is not a dd replacement.
One example would be using dd to copy part of something but not the whole thing. Perhaps you want to rip out some of the bits from the middle of an iso image or the partition table from a hard ...
10
Yes, it is possible. But not all formats support it.
ffmpeg FAQ:
A few multimedia containers (MPEG-1, MPEG-2 PS, DV) allow to join video files by merely concatenating them.
When converting to RAW formats you also have a high chance that the files can be concatenated.
ffmpeg -i input1.avi -qscale:v 1 intermediate1.mpg
ffmpeg -i input2.avi -qscale:v 1 ...
9
tput sgr0, tput rmacs, or tput reset. reset is usually but not always even more complete than tput reset. (Turn off all special output modes; turn off alternate character set, which is usually but not always included in sgr0; send terminal reset string which often does those and more.)
It can be useful to embed tput sgr0 at the beginning of $PS1.
9
An ISO file is a complete, formatted filesystem image. All cat or dd does is do a bit-for-bit copy of that filesystem image to your target media.
There is no magic going on behind the scenes. The ISO filesystem preparation was done beforehand (often by a specialized tool). All cat does is write that collection of bytes out. It doesn't interpret the .iso at ...
9
To give you the formula which involves the wc-based check:
(($(wc -l<input_file)<=$(tput lines))) && echo 'will fit' || echo 'not enough'
There is a $LINES shell variable which can also be used:
(($(wc -l<input_file)<=LINES)) && echo 'will fit' || echo 'not enough'
But $LINES is updated only when at the command prompt. To ...
8
Cat dumps the contents of the input file to standard output, which in this case is a text device: your terminal window. Since an image consists of binary data, what you are seeing is the raw binary data interpreted as though it were ASCII text. For example, a byte with the value 65 would be displayed as upper case A, 66 is upper case B, etc. If you open ...
8
You don't need to pipe a file thru grep, grep takes filename(s) as command line args.
grep -v '^#' file1 file2 file3
will print all lines EXCEPT those that begin with a # char.
you can change the comment char to whatever you wish.
If you have more than one comment char (assuming its at the beginning of a line)
egrep -v '^(;|#|//)' filelist
8
At least in bash, as long as there are no filenames containing spaces and newlines, this:
mkdir $(< myfile)
works. So we have a useless use of for, xargs too.
< does not start a new process in bash, in contrast to cat, but I don't know for ksh.
8
To concatenate files you use
cat file1 file2 file3 ...
To get a list of quoted filenames sorted by time, newest first, you use
ls -t
Putting it all together,
cat $(ls -t) > outputfile
You might want to give some arguments to ls (eg, *.html).
But if you have filenames with spaces in them, this will not work. My file.html will be assumed to be two ...
8
Before the shell starts processing any data, it needs to make sure all the input and output is squared away.
So in your case using > foo.txt basically tells the system: "create a (new) file named foo.txt and stick all the output from this command into that file".
The problem is, as you found out, that that wipes out the previous contents.
Related, ...
8
You can use sed or awk to make it with one command. However you'll loose at speed, cause sed and awk will need to run through the whole file anyway.
From a speed point of view it's much better to make a function or every time to combination of tail + head. This does have the downside of not working if the input is a pipe, however you can use proccess ...
7
No one has yet mentioned that you can use dd to create sparse files, though truncate can also be used for the same purpose.
dd if=/dev/zero of=sparse-file bs=1 count=1 seek=10GB
This is almost instant and creates an arbitrary large file that can be used as a loopback file for instance:
loop=`losetup --show -f sparse-file`
mkfs.ext4 $loop
mkdir myloop
...
7
There are people who argue violently that the only purpose of cat is to concatenate files. For every other use, there is more (or less).
But this fails to take into account one quite important fact: cat is one character less to type. I use those commands so often that one character less is a no-brainer.
Another reason is when you want to create a file. I ...
7
To process the output of a command line by line (explanation):
jobs |
while IFS= read -r line; do
process "$line"
done
If you have the data in a variable already:
printf %s "$foo" | …
printf %s "$foo" is almost identical to echo "$foo", but prints $foo literally, whereas echo "$foo" might interpret $foo as an option to the echo command if it begins ...
7
You are looking for
fold text.txt -w 80 -s
-w tells the width of the text, where 80 is standard.
-s tells to break at spaces, and not in words.
That's the way it's called on debian/ubuntu there are other systems, which need "-c" instead of "-w".
7
egrep can save you the use of cat. In other words, create less processes (egrep vs cat+egrep) and use less buffers (pipe from cat to egrep vs no pipe).
It is generally a good idea to limit the use of cat if you simply want to pass a file to a command that can read it on its own.
With this said, the following command will remove comments, even if they are ...
Only top voted, non community-wiki answers of a minimum length are eligible
