1

Here are my commands for a task.

find -name myname.tar
tar -xvf myname.tar
rm -r myname/*

These multi line commands are working fine, but I need a one line command. Then I used this command,

find -name myname.tar | tar -xvf myname.tar | rm -r myname/*

I get this error:

"rm -r myname/*
rm: cannot remove `myname/*': No such file or directory "

can some one help me in this?

Thanks

2
  • Good question! I've been looking for this a while now!! Commented Sep 20, 2014 at 9:16
  • Isn't the effet the same though? Commented Sep 20, 2014 at 9:30

3 Answers 3

7

Most possibly you are looking for a way to execute the next command, if the previous one ran without an error.

  • In UNIX errors from command line tools typically change the program's exit status
  • In most UNIX shells, the && operator checks the exit status and moves on, if it is 0 (for OK)

So what you seem to want is

find -name myname.tar && tar -xvf myname.tar && rm -r myname/*
Sign up to request clarification or add additional context in comments.

1 Comment

I have a process which I want to execute in the background, so I place a & at the end of the command. If I place && after I get -bash: syntax error near unexpected token &&'`.
2

There are two common ways to do this:

Join the commands together with a semicolon ; which will run the commands one after the other.

echo "Hello"; false; echo "There";
>> Hello
>> There

The other is join commands with a &&

The && will ONLY do the next command if the one before it had a zero exit code.

echo "Hello" && false && echo "There";
>> Hello

The '|' symbol does something different entirely. It runs the two commands, feeling the standard out stdout of the one into the standard in stdin of the other. (wc -w counts the number of words in stdin.)

echo "Two Words" | wc -w
>> 2

Comments

1

What you were using (|) is called a peer, which passes data from one command to another. for ex.:

$ cat test.php | grep '<'
<?php

to run several commands one after another you can use ';':

echo 123; echo 456

or if you want your commands to depend on each others success, you can '&&':

$ eho 123; echo 456
eho: command not found
456

$ eho 123 && echo 456
eho: command not found

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.