Hot answers tagged

168

When you simply run grep --color it implies grep --color=auto which detects whether the output is a terminal and if so enables colors. However, when it detects a pipe it disables coloring. The following command: grep --color=always -R "search string" * | less Will always enable coloring and override the automatic detection, and you will get the color ...


86

dd by default uses a very small block size -- 512 bytes (!!). That is, a lot of small reads and writes. It seems that dd, used naively in your first example, was generating a great number of network packets with a very small payload, thus reducing throughput. On the other hand, gzip is smart enough to do I/O with larger buffers. That is, a smaller number ...


77

On my system, man less says s filename Save the input to a file. This only works if the input is a pipe, not an ordinary file. Works for me!


72

There is a distinction between command line arguments and standard input. A pipe will connect standard output of one process to standard input of another. So ls | echo Connects standard output of ls to standard input of echo. Fine right? Well, echo ignores standard input and will dump its command line arguments - which are none in this case to - its ...


54

Pipe Viewer has this feature. cat /dev/urandom | pv -L 3k | foo


32

Evidently, PowerShell implicitly 'unboxes' a single-item array to a single object, And zero item results to $null. How can I prevent this from happening? You can't. How do you deal with this? Use the array constructor (@(...)) to force a collection (possibly with zero or one elements) return: $res = @(ls | %{$_.Name} | ?{$_.Contains("Prog")})...


30

zcat foo.sql.gz | mysql -uroot -ppassword foo This will also leave foo.sql.gz as it is.


25

The following examples can be used to avoid creating intermediary files: tar with gzip: tar cf - A | gzip -9 > B.tar.gz gzip without tar: gzip -9c A > B.gz tar without gzip: tar cf B.tar A Renaming (moving) A to B first doesn't make sense to me. If this is intended, then just put a mv A B && before either of the above commands and ...


25

Simply insert unbuffer before any command to make it think it is writing to an interactive output even if it is actually piping into another executable. This will preserve color in the case of ls. For example unbuffer ls -l --color=auto | tee output.log If you don't already have it installed, on Ubuntu and other Debian-ish Linux distributions you can ...


23

Try this: curl -vs -o /dev/null http://somehost/somepage 2>&1 That will suppress the progress meter, send stdout to /dev/null and redirect stderr (the -v output) to stdout.


23

If you're comfortable with output and input redirection, the explanation is really quite easy. Command1 | Command2 does the same as Command1 > tempfile Command2 < tempfile but without tempfile. The output of Command1 is directly connected to the input of Command2 and the transfer happens in-memory.


22

Or does it know when its output is being piped to another command, and format its output differently in this case? Yes. From the full manual (available through info ls if the documentation is installed): If standard output is a terminal, the output is in columns (sorted vertically) and control characters are output as question marks; otherwise, the ...


21

You can put this in your .bashrc file: export GREP_OPTIONS="--color=always" or create an alias like this: alias grepc="grep --color=always" and you will need to use the -R option for less, as pointed out by therefromhere


19

Put the pipes at the end of line with the comments after it: $ echo 'foo' | sed 's/f/a/' | # change first f to a sed 's/o/b/' | # change first o to b sed 's/o/c/' # change second o to c abc


19

The symlink contents "pipe:[20043922]" are a unique ID; the other end of the pipe will have a matching ID. (find /proc -type l | xargs ls -l | fgrep 'pipe:[20043922]') 2>/dev/null should show you both ends of the pipe.


17

Use a caret. eg. echo %0 on^|off


16

ls *.txt | xargs cat >> all.txt might work a bit better, since it would append to all.txt instead of creating it again after each file. By the way, cat *.txt >all.txt would also work. :-)


16

The accepted answer doesn't work on the Mac -- as @benroth says, pressing s just moves down a line -- but you can use a different method. In less --help: |Xcommand Pipe file between current pos & mark X to shell command. and A mark is any upper-case or lower-case letter. Certain marks are predefined: ^ means beginning of the file ...


16

ls -t ~/Downloads | head -1 | xargs -I {} mv ~/Downloads/{} ~/Documents This will work with files that have spaces in their names.


15

I'd say that Juliano has got the right answer if you have that tool, but I'd also suggest that this is a neat little K&R style exercise: just write a specialized version of cat that reads one character at a time from stdin, outputs each to stdout and then usleeps before moving on. Be sure to unbuffer the standard output, or this will run rather jerkily. ...


14

It's useless in the sense that using it like that doesn't accomplish anything the other, possibly more efficient options can't (i.e. producing proper results). But cat is way more powerful than just cat somefile. Consult man cat or read what I wrote in this answer. But if you absolutely positively only need the contents of a single file, you might get some ...


14

You can also use bash' command substitution operator (backticks) as mv `ls -t ~/Downloads | head -1` ~/Documents as a one-shot solution if you do not want to move multiple files in one go. See the bash man-page: Command Substitution Command substitution allows the output of a command to replace the command name. There are two forms: $...


14

The following is simplified a bit to help new users. Well, first, it's necessary to understand the concept of standard input and standard output. In Linux and other UNIX-like operating systems, each process has a standard input (stdin) and a standard output (stdout). The usual situation is that stdin is your keyboard and stdout is your screen or terminal ...


14

You're nearly done: $ locate updatedb | head -1 | xargs vim sometimes (under certain terminals) you need reset the terminal after editing. $ reset


13

Better to avoid cat; write it this way if line editing matters: $ < filename grep pattern The reason is that pushing all the data through cat costs memory and CPU resources. Another benefit of passing the filename as an argument rather than redirect stdin is that it allows the command the option to mmap() the file.


13

To your title question: No. Getting stdin from file contents (input redirection) is not the same as piping one program's output to another program's input. But, as your cat actually just prints a file's contents, the result is effectively the same in that example. But even just the following produce very different results: $ cat * | sort $ sort < * ...


13

tar -xzOf file.tar.gz file_you_want_to_extract | ssh user@host 'cat > /path/to/destination_file' -x : Extract -z : Through gzip -f : Take in a file as the input. -O : Extract to stdout The file_you_want_to_extract is extracted from file.tar.gz to the standard output, piped into ssh, which runs cat on the remote host and writes its standard in to the ...


13

You are confusing two very different kinds of input: STDIN and arguments. Arguments are a list of strings provided to the command as it starts, usually by specifying them after the command name (e.g. echo these are some arguments or rm file1 file2). STDIN, on the other hand, is a stream of bytes (sometimes text, sometimes not) that the command can (...


12

While none of the shells I know can make pipes without forking, some do have better than the basic shell pipeline. In bash, ksh and zsh, assuming your system supports /dev/fd (most do nowadays), you can tie the input or the output of a command to a file name: <(command) expands to a file name that designates a pipe connected to the output from command, ...


12

ls | echo prints just a blank line because echo reads no input; the last command of the pipeline is actually echo that prints nothing but a blank line. Generally: a | b makes sure that the output of a become the input of b. I suggest you to read the Pipelines section of man bash. If you really want to use ls and echo together here's some (pretty ...



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