Say I have a file /templates/apple
and I want to
So, /templates/apple
will be copied to /templates/used
AND /templates/inuse
and then after that I’d like to remove the original.
Is 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?
The semicolon (;) operator allows you to execute multiple commands in succession, regardless of whether each previous command succeeds. For example, open a Terminal window (Ctrl+Alt+T in Ubuntu and Linux Mint).
Linux allows you to enter multiple commands at one time. The only requirement is that you separate the commands with a semicolon. Running the combination of commands creates the directory and moves the file in one line.
Concatenate Commands With “&&“ The “&&” or AND operator executes the second command only if the preceding command succeeds.
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
Or
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 stderr
still goes into its default destination, whatever that happen to be.|&
pipes both stdout
and stderr
of 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 -e
was previously invoked, which causes bash
to fail on an error.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With