how do you copy a directory and its contents to a new location under a new directory name?

517,112

Solution 1

You can use cp with the -r (copy recursively) flag and specify the new directory name:

cp -r /path/to/directory /path/to/location/new-name

In the above command replace the given paths with the real ones.

For example, to copy stuff from my home directory to an existing directory named backup and name the new directory stuff-backup (if this directory already exists, note that stuff will be copied into it, not overwrite it), you run:

cp -r ~/stuff ~/backup/stuff-backup

~ is a shortcut for your home directory /home/$USER or /home/zanna in my case. You can omit it if the current working directory is your home directory - you can see this from your prompt

zanna@monster:~$
              ^--the ~ here is where I am - home!

You can add the -v (verbose) flag to make cp report each copy being performed:

$ cp -vr stuff backup/stuff-backup
'stuff/thing1' -> 'backup/stuff-backup/thing1'
'stuff/thing2' -> 'backup/stuff-backup/thing2
...

Solution 2

The command you need is simply cp which stands for "copy".

You can use it for example with one of these syntaxes:

cp SOURCEFILE TARGETFILE
cp SOURCEFILE TARGETDIRECTORY

The first variant allows you to specify a new file name for the target file, while the second variant creates a copy with the same name in the target directory. You must of course substitute the place holders in capital letters with valid paths first.

However, cp by default operates on files only, not on directories. To get it to copy a complete directory with all its content recursively, you must add the -r option:

cp -r SOURCEDIRECTORY TARGETDIRECTORY

You can learn more about the cp command by typing man cp in your terminal.

Solution 3

You can use the below command to copy directory from one place to another sub directory.

cp -Rvp /home/edmuntu/data /home/edmuntu/mydata/

Explanation of command:

  • -R = copy directories recursively
  • v = explain what is being done
  • p = It will help your to preserve your permission and ownership with timestamps

Solution 4

As an alternative to the generally useful cp command you can use rsync which has the added benefit of replicating the permissions/timestamps of the original directory structure:

rsync -av dir/ newdir

-a archive -v verbose

note the slash after the source dir.

Solution 5

cp is the command that does the function that you're looking for. It stands for copy and it does just that. The command that would work for you in this situation is

cp -R source/ destination/

This would copy the source/ folder and all of its sub-directories to destination/.

If destination/ doesn't exist, it will be created.

The -R flag stands for recursive, which tells cp to copy all sub-directories.

Share:
517,112

Related videos on Youtube

Edmuntu
Author by

Edmuntu

Updated on September 18, 2022

Comments

  • Edmuntu
    Edmuntu almost 2 years

    I'm new to the Linux command line and am trying to get to grips with the copy command at the moment.

    Can anyone please tell me if it's possible to copy a directory with its subdirectories and associated files to a new directory with a new name, e.g. directory_backup?

    Thanks for any and all help in advance.

  • BlueCacti
    BlueCacti over 7 years
    While this does explain how to copy all files from your current dir to another dir, it doesn't fully answer OPs question on how to copy a dir (and its contents) to a different name dir. There are shorter commands (such as cp -r) which will take care of creating a new directory and copying all files into it
  • Edmuntu
    Edmuntu over 7 years
    Sorry Zanna. I'll put your advice into practice and see how I go. Like the idea of the verbose flag . assume this will also work as a report while other command are being worked.. Cheers.
  • Edmuntu
    Edmuntu over 7 years
    Not used to this comment section Byte Commander...keep hitting return to change line but post instead. Very clear outline in your answer . Look forward to sorting out those directories now that I've been given good instruction on the correct usage of the available tools. Excellent.
  • Zanna
    Zanna over 7 years
    @Edmuntu sorry for what? Yes, lots of commands have a -v or --verbose flag :)
  • Edmuntu
    Edmuntu over 7 years
    Thank You@Owen Hines. Sound like this is definitely the command for my task.Cheers
  • TheOdd
    TheOdd over 7 years
    @Edmuntu Glad I could help, if you found the solution to your issue, can you please mark the answer that worked for you as the correct one by clicking the check mark under it? This will help alert others who view the page of the question being solved.
  • Edmuntu
    Edmuntu over 7 years
    Thank you @Rakesh Chauhan."preserve your permission and ownership with timestamps" ??? Wow....I need to get to it!!!! Yet another aspect of line commands to be explored. Thank you for your reply.
  • Edmuntu
    Edmuntu over 7 years
    sorry for initial short" thank you"....hit return key in error.
  • WinEunuuchs2Unix
    WinEunuuchs2Unix over 7 years
    It does explain how to copy and dir and it's contents--the difference is it changes to the source directory first as a precautionary habit. cp -r as you recommend is already on the third command line. The tutorial is based on the fact everyone has a /boot directory and can try this on their own machine. Had I used cp -r /home/GroundZero/Documents /home/GroundZero/Documents_backup readers could not copy and paste into their terminal--they would have change GroundZero creating more work and chances for error. I hope you appreciate the KISS of this answer with an embedded tutorial.
  • WinEunuuchs2Unix
    WinEunuuchs2Unix over 7 years
    @GroundZero doing a cd ~/Documents/SomeObsucreFolder/2016/Nov/ to set current directory as source directory will change the Terminal prompt to ensure it is spelled correctly and then using cp -r . as the source eliminates retyping the source directory. It is simply what I consider a good habit. Additionally once changing to source directory it's easy to do ls FIleName1, ls FileName2, etc. to esnure the files you want to backup are there without having to repeat the source directory name for the ls command.
  • TheOdd
    TheOdd over 7 years
    @Edmuntu In the future, use SHIFT+ENTER while commenting to go down a line.
  • Byte Commander
    Byte Commander over 7 years
    @OwenHines Just that comments strip any linebreak and additional whitespace and having them in the editor doesn't change anything in the result.
  • TheOdd
    TheOdd over 7 years
    @ByteCommander Oh right, nevermind. Well, now he knows.
  • Hello Universe
    Hello Universe over 3 years
    This is much better than copy as it also copied the permissions
  • Tim Rogers
    Tim Rogers almost 3 years
    I think this is the best answer. From the manual: -a is the same as -dR --preserve=all