Tag Info

Hot answers tagged

92

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


68

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


35

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!


30

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


28

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


20

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.


20

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


17

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


14

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.


14

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


12

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


12

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


12

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


12

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


11

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


11

Yes, as many as you like. $ ls -al gives this total 25 drwxr-xr-x+ 1 nifle None 4096 2010-04-11 11:34 . drwxrwxrwt+ 1 nifle root 0 2010-02-09 18:46 .. -rw------- 1 nifle None 4581 2010-06-10 20:34 .bash_history -rwxr-xr-x 1 nifle None 1150 2010-02-09 16:42 .bash_profile -rwxr-xr-x 1 nifle None 3754 2010-02-09 16:42 .bashrc -rwxr-xr-x 1 nifle None ...


10

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


10

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


10

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


10

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


10

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


9

When using pipes, you want to consider the order of operations before fashioning your pipeline. You'll also want to have a good understanding of what each command does. For clarification: /etc/passwd and /etc/group are not directories, but files. You're on the right track using ls for directories, but in this case it is not needed. To get you started, here ...


9

ls detects it when you pipe its output. You can see it in the documentation: If standard output is a terminal, the output is in columns (sorted vertically) and control characters are output as question marks; otherwise, the output is listed one per line and control characters are output as-is. If you want each file in the output to be placed on a ...


9

I think you want 'Process Substitution' http://tldp.org/LDP/abs/html/process-sub.html. It also works on zsh, though it has more options and the syntax may be different. It creates a pseudo file (/dev/fd/something) for each substitution. It's pretty useful. The command can only read as a stream, meaning it can not go back and forth with fseek. It needs to ...


9

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


9

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


9

It's not less that needs to change. The output of your other programs is being redirected to a pipe. Those programs detect that their output is not being sent to a tty and they disable their coloring. You're stuck with having to do something special with the source programs to color their output even when redirected to a pipe. I think I have a solution for ...


9

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.


8

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


8

pipes don't store data on disk. /bin/echo foo | grep bar doesn't create any files. try strace -f sh -c '/bin/echo foo | grep bar' to see all the system calls made by a shell when running a pipeline. echo is a shell builtin, so I suggested /bin/echo to make the shell run an executable. /tmp doesn't have to be on disk. It can be mounted on tmpfs (i.e. ...



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