6

Currently, I'm running these two commands to create a quick backup of the directory. Is there a way to combine the two commands into one, so that I am copying and renaming the new directory in one command?

#cp -R /tf/Custom_App /tf/Custom_App_backups/
#mv /tf/Custom_App_backups/Custom_App /tf/Custom_App_backups/Custom_App_2017-12-21
  • 1
    cp -R /tf/Custom_App /tf/Custom_App_backups/Custom_App_2017-12-21 – Jesse_b Dec 21 '17 at 13:29
  • How about to define alias or function for that two things ;) – Vlastimil Dec 21 '17 at 13:43
13

You should be able to do just

cp -R /tf/Custom_App /tf/Custom_App_backups/Custom_App_2017-12-21

However, if the target directory already exists, this would append the final part of the source path to the destination path, creating /tf/Custom_App_backups/Custom_App_2017-12-21/Custom_App, and then copy the rest of the tree within that.

To prevent this, use /tf/Custom_App/. as the source. Of course, in that case you might want to rm -r /tf/Custom_App_backups/Custom_App_2017-12-21 first, if you don't want older files lying around there after the copy.

The difference between /some/dir and /some/dir/. was discussed a while back in cp behaves weirdly when . (dot) or .. (dot dot) are the source directory

  • An alternative to passing . as the source directory is to use the -T flag to tell cp to overwrite the destination rather than creating a new member inside it. – Toby Speight Dec 21 '17 at 16:57
  • @TobySpeight, ... in GNU cp. – ilkkachu Dec 22 '17 at 11:22
  • The /tf/Custom_app/. trick is just what I needed. – Martin Bonner Sep 21 '18 at 9:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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