55

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

74

You use -r (copy recursively) and specify the new name

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

replace with the real paths... for example to copy stuff from my home directory to an existing directory backup and name the new directory stuff-backup (if this directory already exists, note that stuff will be copied into it, not overwrite it).

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
...
  • 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 Nov 3 '16 at 21:13
  • @Edmuntu sorry for what? Yes, lots of commands have a -v or --verbose flag :) – Zanna Nov 3 '16 at 21:29
  • sorry for initial short" thank you"....hit return key in error. – Edmuntu Nov 3 '16 at 21:43
10

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.

  • 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. – Edmuntu Nov 3 '16 at 21:21
  • @Edmuntu In the future, use SHIFT+ENTER while commenting to go down a line. – TheOdd Nov 5 '16 at 16:01
  • @OwenHines Just that comments strip any linebreak and additional whitespace and having them in the editor doesn't change anything in the result. – Byte Commander Nov 5 '16 at 17:07
  • @ByteCommander Oh right, nevermind. Well, now he knows. – TheOdd Nov 5 '16 at 17:47
6

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.

5

I find it easier to change to the directory I'm copying from first. For this example change to a directory everyone has called /boot. Anyone can copy and paste the commands below into their Terminal.

cd /boot
sudo mkdir /boot_backup
sudo cp -r . /boot_backup
du /boot_backup -h

752K    /boot_backup/extlinux/themes/debian-wheezy
756K    /boot_backup/extlinux/themes
832K    /boot_backup/extlinux
2.5M    /boot_backup/grub/i386-pc
20K     /boot_backup/grub/locale
2.3M    /boot_backup/grub/fonts
7.2M    /boot_backup/grub
565M    /boot_backup

For the cp command the current directory is identified as . which is the /boot directory we changed to. The -r option makes it recursive to include all sub-directories.

To ensure it worked run du to list all sub-directories and total file sizes in the new directory /boot_backup in this case.

After finishing this walk-through, use: sudo rm -r /boot_backup to remove the new directory and it's sub-directories.

  • 1
    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 – BlueCacti Nov 3 '16 at 13:28
  • 1
    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 Nov 5 '16 at 14:23
  • 1
    @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. – WinEunuuchs2Unix Nov 5 '16 at 14:27
5

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
  • 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 Nov 3 '16 at 21:38
4

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.

  • Thank You@Owen Hines. Sound like this is definitely the command for my task.Cheers – Edmuntu Nov 3 '16 at 21:34
  • 1
    @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. – TheOdd Nov 3 '16 at 21:36
1

You generally want to keep the file permissions the same when making a copy, not a new set of default creation permissions. That means use -a flag for cp.

cp -a file filenew

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.