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 ;) Dec 21 '17 at 13:43

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

  • 1
    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. Dec 21 '17 at 16:57
  • 1
    @TobySpeight, ... in GNU cp.
    – ilkkachu
    Dec 22 '17 at 11:22
  • 2
    The /tf/Custom_app/. trick is just what I needed. Sep 21 '18 at 9:32
  • cp -a also works.
    – flow2k
    May 22 '19 at 21:44
  • 2
    @flow2k, GNU utilities in particular have loads of non-standard options, many of them very useful. cp -a of course appears also in e.g. FreeBSD and OpenBSD but it's still not a standard feature, that is, not specified by POSIX. (cp -T that was mentioned earlier seems a GNUism, it's not in POSIX, and not in the BSDs as far as I can see.)
    – ilkkachu
    May 31 '19 at 14:29

Alternatively, you can do it like so:

mkdir /tf/Custom_App_backups/Custom_App_2017-12-21 # prepare the target location
cp -R /tf/Custom_app/. /tf/Custom_App_backups/Custom_App_2017-12-21 # copy only the contents

This will allow you to specify your custom location beforehand. Also, notice that it uses the suffix /. This allows you to only copy the contents and exclude its containing folder -- in this case it is the Custom_app folder.


I came across this page searching for the same advice. On my Ubuntu 20.04 (Focal Fossa) system, I attempted this command and used the same syntax as what @ilkkachu presented. Except I added a trailing slash to my dest folder, eg, dest/ . The result copied the source folder plus its siblings! When I retried without the trailing slash I got the desired result.

So, for future reference, be aware that adding a trailing slash to the dest folder will copy the source siblings as well (.. at least, that was MY experience)

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.