How can I copy a directory and rename it in the same command?

cp mv
79,904

Solution 1

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

Solution 2

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.

Share:
79,904

Related videos on Youtube

AllisonC
Author by

AllisonC

Updated on September 18, 2022

Comments

  • AllisonC
    AllisonC almost 2 years

    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
    
    • jesse_b
      jesse_b over 6 years
      cp -R /tf/Custom_App /tf/Custom_App_backups/Custom_App_2017-12-21
    • Vlastimil Burián
      Vlastimil Burián over 6 years
      How about to define alias or function for that two things ;)
  • Toby Speight
    Toby Speight over 6 years
    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.
  • ilkkachu
    ilkkachu over 6 years
    @TobySpeight, ... in GNU cp.
  • Martin Bonner supports Monica
    Martin Bonner supports Monica almost 6 years
    The /tf/Custom_app/. trick is just what I needed.
  • flow2k
    flow2k about 5 years
    cp -a also works.
  • Abel Melquiades Callejo
    Abel Melquiades Callejo about 5 years
    @MartinBonner, I agree with you. The answer should have been bash cp -R /tf/Custom_app/. /tf/Custom_App_backups/Custom_App_2017-12-21
  • ilkkachu
    ilkkachu about 5 years
    @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
    ilkkachu about 5 years
    @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.
  • flow2k
    flow2k about 5 years
    @ilkkachu What do you mean when you say -a is not a standard option? I do see it listed on gnu.org/software/coreutils/manual/html_node/….
  • ilkkachu
    ilkkachu about 5 years
    @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.)