Hot answers tagged


Use sed Usage $ cat file Line 1 Line 2 Line 3 Line 4 Line 5 Line 6 Line 7 Line 8 Line 9 Line 10 To print one line (5) $ sed -n 5p file Line 5 To print multiple lines (5 & 8) $ sed -n -e 5p -e 8p file Line 5 Line 8 To print specific range (5 - 8) $ sed -n 5,8p file Line 5 Line 6 Line 7 Line 8 To print range with other specific line (5 - 8 & ...


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


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


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


head -c 50 file This returns the first 50 bytes. Mind that the command is not always implemented the same on all OS. On Linux and macOS it behaves this way. On Solaris (11) you need to use the gnu version in /usr/gnu/bin/


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


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


Yes: Install Homebrew brew install coreutils ln -s /usr/local/bin/gtac /usr/local/bin/tac apparently not needed with latest Homebrew, see comment by Ran Ever-Hadani below or use MacPorts to install coreutils in a similar way.


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


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


var=$( cat foo.txt ) would store the output of the cat in variable var. var=$( ./myscript ) would store the output of myscript in the same variable.


The most obvious one is cat. But, also have a look at head and tail. There are also other shell utillities to print a file line by line: sed, awk, grep. But those are to alternate the file content or to search inside the file. I made a few tests to estimate which is the most effective one. I run all trough strace to see which made the least system calls. My ...


Using find, sort and xargs: find . -maxdepth 1 -type f -name 'file_*.pdb' -print0 | sort -zV | xargs -0 cat >all.pdb The find command finds all relevant files, then prints their pathnames out to sort that does a "version sort" to get them in the right order (if the numbers in the filenames had been zero-filled to a fixed width we would not have needed -...


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


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.


try: find . -type f -exec cat {} +


Even though everybody uses cat filename to print a files text to the standard output first purpose is concatenating. From cat's man page: cat - concatenate files and print on the standard output Now cat is fine for printing files but there are alternatives: echo "$(<filename)" or printf "%s" "$(<filename)" The ( ) return the value of an ...


One way of doing it is by using sed: cat -n text.txt | sed '11d' where 11 is the number of the line you want removed. Or to remove all but 11: cat -n text.txt | sed '11!d' Ranges are also possible: cat -n text.txt | sed '9,12!d' And cat -n isn't even needed: sed '9,12!d' text.txt


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


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


All of the following commands are equivalent. They read the bytes of the CD /dev/sr0 and write them to a file called image.iso. cat /dev/sr0 >image.iso cat </dev/sr0 >image.iso tee </dev/sr0 >image.iso dd </dev/sr0 >image.iso dd if=/dev/cdrom of=image.iso pv </dev/sr0 >image.iso cp /dev/sr0 image.iso tail -c +1 /dev/sr0 >image....


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.


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


There are several possibilities, all depending on the exact parameters of your situation right now. I'm going to assume Linux in the following examples where applicable, but similar functionality exists on other platforms in most cases. You might be able to get the dynamic loader to run an executable for you. Assuming cat is dynamically-linked, your ...


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


For people who can run scp, you can do this: scp remotehost:/path/to/remote/file /dev/stdout


Your cut command works if you use a pipe to pass data to it: cat ${file} | cut -c1-50 Or, avoiding a useless use of cat and making it a little safer: cut -c1-50 < "$file" Note that the commands above will print the first 50 characters (or bytes, depending on your cut implementation) of each input line. It should do what you expect if, as you say, ...


In Unix, most objects you can read and write - ordinary files, pipes, terminals, raw disk drives - are all made to resemble files. A program like cat reads from its standard input like this: n = read(0, buffer, 512); which asks for 512 bytes. n is the number of bytes actually read, or -1 if there's an error. If you did this repeatedly with an ordinary ...


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

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