53

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
2
  • 1
    cp -R /tf/Custom_App /tf/Custom_App_backups/Custom_App_2017-12-21
    – jesse_b
    Dec 21, 2017 at 13:29
  • How about to define alias or function for that two things ;) Dec 21, 2017 at 13:43

4 Answers 4

68

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

9
  • 2
    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, 2017 at 16:57
  • 1
    @TobySpeight, ... in GNU cp.
    – ilkkachu
    Dec 22, 2017 at 11:22
  • 2
    The /tf/Custom_app/. trick is just what I needed. Sep 21, 2018 at 9:32
  • cp -a also works.
    – flow2k
    May 22, 2019 at 21:44
  • 3
    @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, 2019 at 14:29
5

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.

0

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)

-1

You can easily proceed by creating your target directory:

mkdir Custom_App_2017-12-21
cd Custom_App_2017-12-21/
cp -R /tf/Custom_App/* .

The last command will copy all your relevant files into a newly-made directory.

2
  • 1
    You will have to mention your assumptions: 1) All relevant files have non-hidden filenames. 2) The * does not expand to too many names.
    – Kusalananda
    Jun 3, 2022 at 8:26
  • 1
    (0) Also, you didn’t state your assumption that the OP has done cd /tf/Custom_App_backups.  (1) The question says, “Currently, I'm running these two commands …  Is there a way to combine the two commands into one, … in one command?”  You have replaced the OP’s two commands with three.  (2) You haven’t added anything here.  In fact, you have subtracted — you have replicated Abel Melquiades Callejo’s answer, but made it worse. Jun 3, 2022 at 22:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .