Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

How can I execute simple commands like

cp -pr /scripts/htdocs/sob/summaries/* /scripts/htdocs/sob/summaries.prod

cp -p /scripts/htdocs/sob/summaries/index.php /scripts/htdocs/sob/summaries.prod/index.php

rm -R cake.old nbproject

with a single command call?

share|improve this question

5 Answers 5

up vote 5 down vote accepted

Put the following into command.sh:

#!/bin/bash
cp -pr /scripts/htdocs/sob/summaries/* /scripts/htdocs/sob/summaries.prod
cp -p /scripts/htdocs/sob/summaries/index.php /scripts/htdocs/sob/summaries.prod/index.php
rm -R cake.old nbproject

Then make that file executable:

$ chmod +x command.sh

Then run it:

$ ./command.sh
share|improve this answer

If you're just copy/pasting this, separate the commands with semicolons.

cp -pr /scripts/htdocs/sob/summaries/* /scripts/htdocs/sob/summaries.prod; cp -p /scripts/htdocs/sob/summaries/index.php /scripts/htdocs/sob/summaries.prod/index.php; rm -R cake.old nbproject

Note that the first command you posted will copy the index.php file, so running your second copy directly after that is unnecessary.

share|improve this answer

You can either separate them with semicolons, as the others said, or with double ampersands (&&) like this:

cp -pr /scripts/htdocs/sob/summaries/* /scripts/htdocs/sob/summaries.prod && cp -p /scripts/htdocs/sob/summaries/index.php /scripts/htdocs/sob/summaries.prod/index.php && rm -R cake.old nbproject

The difference is that with the ampersands each command will be executed only if the previous one completed successfully while with the semicolon all commands will be executed regardless of the previous ones.

share|improve this answer

You should use rsync instead of cp as it's more efficient if any of the files or directories remain the same across runs. rsync also handles things like symlinks and oddities like sparse files better than cp. Use a text editor (pico is fine to start with) and create your ~/script.sh file (this version also has some error checking):

#!/bin/bash
rsync -av /scripts/htdocs/sob/summaries/* /scripts/htdocs/sob/summaries.prod || \
  { echo "rsync failed" >&2 && exit 1; };
rm -rf /somedir/cake.old /somedir/nbproject || { echo "rm -rf failed" >&2 && exit 1; };

Save that file, and run chmod 755 script.sh to make it executable. Then typing ~/script.sh will execute your script.

Note you want to put a absolute directory on that rm -rf command so you don't accidentally blow away the wrong directory in some other location. I also eliminated your second copy since it's covered in the first recursive copy.

rm -rf is a more standard idiom than rm -R. The -f forces removal of files/directories even if the permissions disallow it (but only for files or directories you already own).

share|improve this answer

You can separate them with semi-colons or put them in a script, mark it as executable, and just call the script.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.