Sign in to follow this  

How do you combine Linux Commands?

This topic is 4268 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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!

Share this post


Link to post
Share on other sites
There are many ways of "combining" Linux commands. You want to output two commands to the same file, that's an easy one.


ls >somefile
df >>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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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').

Share this post


Link to post
Share on other sites
Sign in to follow this