134

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.

Edmuntu
  • 1,353

7 Answers7

179

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
...
Zanna
  • 72,312
16

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.

Byte Commander
  • 110,243
16

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
Rakesh C
  • 316
12

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.

slowko
  • 932
8

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.

TheOdd
  • 3,012
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.

4

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
Robi
  • 41