54

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
AllisonC
  • 731

4 Answers4

69

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

ilkkachu
  • 138,973
  • 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. – Toby Speight 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. – Martin Bonner supports Monica Sep 21 '18 at 09:32
  • cp -a also works. – flow2k May 22 '19 at 21:44
  • @MartinBonner, I agree with you. The answer should have been
    cp -R /tf/Custom_app/. /tf/Custom_App_backups/Custom_App_2017-12-21
    
    – Abel Melquiades Callejo May 27 '19 at 16:59
  • @AbelMelquiadesCallejo, wasn't it, then? The second-to-last paragraph says "to prevent this, use /tf/Custom_App/. as the source." (there's the magic trailing dot). – ilkkachu May 27 '19 at 18:34
  • @flow2k, -a includes -R in the versions of cp where it's supported, so yeah, it works. It's just not a standard option, and I don't think it affects the issue at hand. – ilkkachu May 27 '19 at 18:38
  • @ilkkachu What do you mean when you say -a is not a standard option? I do see it listed on https://www.gnu.org/software/coreutils/manual/html_node/cp-invocation.html#cp-invocation. – flow2k May 28 '19 at 02:07
  • 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 '19 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.

  • 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 03 '22 at 08: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. – G-Man Says 'Reinstate Monica' Jun 03 '22 at 22:10