Advertisement

How do you combine Linux Commands?

Started by April 02, 2006 10:31 AM
2 comments, last by Fruny 18 years, 9 months ago
Uhhmmm... I've heard of this stuff somewhere... For example, I wanted to show the output of the ls command and df command in a single output to screen or file... How would I do that? Thanks!
We should never stop learning...
There are many ways of "combining" Linux commands. You want to output two commands to the same file, that's an easy one.

ls >somefiledf >>somefile


A single > will redirect output to a file, erasing whatever was there. A double >> will append to a file. You can now use less or cat on that file to display it. Or you could use a subshell. When using a subshell, all output of the commands run in the subshell can be piped as one. Here's how you can run these two commands, write their output to a file and view them in less all at once.

(ls; df) | tee somefile | less


Lots of things you can do, lots of different ways you can do it. I recommend reading the Bash man page if you're interested in stuff like this.
Advertisement
Quote: Original post by Tradone
piping using the | operator is one widely used method.

and I don't know the deal with the &'s
but for example,
make & make install
is a common method as well.


That isn't correct. A single & will start a background task. A double && is a boolean operator. "make && make install" will run make, and if it succeeds (returns zero), it will run make install. This takes advantage of the short-circuit behavior of the && operator. In this case, since he's running ls and df (the return value of either is not really relevant), neither & nor && will help.
A single & will run the process in the background.
foo && bar will run bar only if foo succeeded (zero exit code) (short-circuit evaluation of 'and').
foo || bar will run bar only if foo failed (non-zero exit code) (short-circuit evaluation of 'or').
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

This topic is closed to new replies.

Advertisement