Hot answers tagged

111

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 ...


91

In the first case, cat opens the file, and in the second case, the shell opens the file, passing it as cat's standard input. Technically, they could have different effects. For instance, it would be possible to have a shell implementation that was more (or less) privileged than the cat program. For that scenario, one might fail to open the file, while the ...


79

You are looking for fold -w 80 -s text.txt -w tells the width of the text, where 80 is standard. -s tells to break at spaces, and not in words. This is the standard way, but there are other systems, which need "-c" instead of "-w".


68

If the file(s) in question contain really lots of data sending the signal can actually get to cat before it finishes. What you really observe is the finite speed of your terminal - cat sends the data to the terminal and it takes some time for the terminal to display all of it. Remember, that usually it has to somehow redraw the whole output window for each ...


64

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 ...


59

type tells you what the shell would use. For example: $ type echo echo is a shell builtin $ type /bin/echo /bin/echo is /bin/echo That means that if, at the bash prompt, you type echo, you will get the built-in. If you specify the path, as in /bin/echo, you will get the external command. which, by contrast is an external program that has no special ...


53

cat < file1 > file2 is not a UUOC. Classically, < and > do redirections which correspond to file descriptor duplications at the system level. File descriptor duplications by themselves don’t do a thing (well, > redirections open with O_TRUNC, so to be accurate, output redirections do truncate the output file). Don’t let the < > symbols ...


52

I suggest the sed solution, but for the sake of completeness, awk 'NR >= 57890000 && NR <= 57890010' To cut out after the last line: awk 'NR < 57890000 { next } { print } NR == 57890010 { exit } Speed test: 100,000,000-line file generated by seq 100000000 > test.in Reading lines 50,000,000-50,000,010 Tests in no particular ...


47

Try it with -f or --force: zcat -f -- * Since zcat is just a simple script that runs exec gzip -cd "$@" with long options that would translate to exec gzip --stdout --decompress "$@" and, as per the man gzip (emphasize mine): -f --force Force compression or decompression even if the file has multiple links or the corresponding file ...


44

cat is hashed (/bin/cat) is just like cat is /bin/cat (that is, it's an external program). The difference is that you already ran cat in this session, so bash has already looked it up in $PATH and stored the resulting location in a hash table so it doesn't have to look it up again in this session. To see all the commands that have been hashed in your ...


38

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 ...


37

Both of those examples are useless uses of cat. Both are equivalent to wc < file1 > file2. There is no reason to use cat in this example, unless you are using cat file as a temporary stand-in for something that dynamically generates output.


34

Passing the file through pygmentize-f terminal will attempt to detect the type from the filename and highlight it appropriately.


31

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: # You need printf '%s\n' "$var" here because if you use printf '%s' "$var" # on a variable that doesn't end with a newline then the while loop will # completely miss the last line of the variable. ...


31

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. ...


31

#!/bin/bash if [[ "$#" -ne 1 ]]; then echo "Usage: $0 [INPUT FILE]" 1>&2 exit 1 fi cat "$1"


30

This sounds like a job for paste: paste -d ' ' a.dat 1.dat Output: a b 1 2 c d 3 4


30

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 ...


29

There are multiple ways to do this. The simplest is probably this: cat <<EOF | sh touch somefile echo foo > somefile EOF Another, which is nicer syntax in my opinion: ( cat <<EOF touch somefile echo foo > somefile EOF ) | sh This works as well, but without the subshell: { cat <<EOF touch somefile echo foo > somefile EOF } | ...


28

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 ...


27

On OS/X like on many systems (BSDs, Solaris, AIX, IRIX...), the functionality of GNU tac is available in tail with the -r option. So no need to install GNU tac: tail -r the-file


27

cat file | wc | cat > file2 would usually be two useless uses of cat as that's functionally equivalent to: < file wc > file2 However, there may be a case for: cat file | wc -c over < file wc -c That is to disable the optimisation that many wc implementations do for regular files. For regular files, the number of bytes in the file ...


26

Yes: Install Homebrew brew install coreutils ln -s /usr/local/bin/gtac /usr/local/bin/tac or use MacPorts to install coreutils in a similar way.


25

zless It seems a pity about zcat, as libz has an API that supports reading from both compressed and uncompressed files transparently. But the manpage does say that zcat is equivalent to gunzip -c.


25

Nope, cat is surely the best way to do this. Why use python when there is a program already written in C for this purpose? However, you might however want to consider using xargs in case the command line length exceeds ARG_MAX and you need more than one cat. Using GNU tools, this is equivalent to what you already have: find . -maxdepth 1 -type f -name '...


24

In addition to fold, take a look at fmt. fmt tries to choose line breaks intelligently to make text look good. It doesn't break long words, rather it wraps only by spaces. It will also join adjacent lines, which is good for prose but bad for log files or other formatted text.


24

You can use: sed -e '/^;/d' php.ini


23

If you want lines X to Y inclusive (starting the numbering at 1), use tail -n +$X /path/to/file | head -n $((Y-X+1)) tail will read and discard the first X-1 lines (there's no way around that), then read and print the following lines. head will read and print the requested number of lines, then exit. When head exits, tail receives a SIGPIPE signal and ...


22

Just ask cat to concatenate that file with the stdin: cat cmd - | interactive


22

There are several things to consider here. i=`cat input` can be expensive and there's a lot of variations between shells. That's a feature called command substitution. The idea is to store the whole output of the command minus the trailing newline characters into the i variable in memory. To do that, shells fork the command in a subshell and read its ...



Only top voted, non community-wiki answers of a minimum length are eligible