Posted By: Anonymous
Say I have a file
/templates/apple and I want to
- put it in two different places and then
- remove the original.
/templates/apple will be copied to
and then after that I’d like to remove the original.
cp the best way to do this, followed by
rm? Or is there a better way?
I want to do it all in one line so I’m thinking it would look something like:
cp /templates/apple /templates/used | cp /templates/apple /templates/inuse | rm /templates/apple
Is this the correct syntax?
You are using
| (pipe) to direct the output of a command into another command. What you are looking for is
&& operator to execute the next command only if the previous one succeeded:
cp /templates/apple /templates/used && cp /templates/apple /templates/inuse && rm /templates/apple
cp /templates/apple /templates/used && mv /templates/apple /templates/inuse
To summarize (non-exhaustively) bash’s command operators/separators:
|pipes (pipelines) the standard output (
stdout) of one command into the standard input of another one. Note that
stderrstill goes into its default destination, whatever that happen to be.
stderrof one command into the standard input of another one. Very useful, available in bash version 4 and above.
&&executes the right-hand command of
&&only if the previous one succeeded.
||executes the right-hand command of
||only it the previous one failed.
;executes the right-hand command of
;always regardless whether the previous command succeeded or failed. Unless
set -ewas previously invoked, which causes
bashto fail on an error.