PDF download Download Article
A simple step-by-step guide to copying folders and their contents
PDF download Download Article

If you want to copy a directory (folder) in Linux to another directory, you can use the cp command. To copy the entire directory recursively, including all of its subdirectories, you'll want to use cp -r . This guide will teach you the best ways to copy directories and their files in any version of Linux.

Copying Directories in Linux with cp

  • To copy an entire directory, including subdirectories: cp -r /source/directory /target/directory/
  • To copy the contents of a directory but not the directory itself: cp /source/directory/* /target/directory/
  • To copy multiple directories: Use cd to enter the parent directory of those you want to copy, then use cp -r directory1 directory2 directory3 /target/directory/
Section 1 of 7:

Copying an Entire Directory

PDF download Download Article
  1. This command copies the source directory (including all of the files and subfolders inside) to the target destination.
    • If you don't have permission to access the source directory or write to the target directory, preface the command with sudo .
    • By default, cp does not warn you if you'll be overwriting an existing file. If you're worried about accidentally overwriting a file, add the -i option to use interactive mode. E.g., cp -r -i /home/jen/pictures /home/john/ . [1]
    • You can use the ls -a command to view all files and folders within a directory.
  2. Advertisement
Section 2 of 7:

Copying a Directory's Contents

PDF download Download Article
  1. If you only want to copy the files from a directory, not the directory itself, use the wildcard (*).
    • For example, let's say you want to copy the pictures from /home/jen/pictures into /home/john/pictures:
      • Since /home/john/pictures already exists, you don't want to copy /home/jen/pictures/, as this will create /home/john/pictures/pictures. Instead, you'll just want to copy the image files into the existing pictures folder in John's home directory.
      • To copy the pictures without creating a new subdirectory, you'd use cp /home/jen/pictures/* /home/john/pictures/ .
Section 3 of 7:

Copying a File to Another Directory

PDF download Download Article
  1. If you just want to copy one file to a different directory, you can use the cp command without any special options. Just make sure you add a forward slash after the target directory's name to specify that the destination is a directory, not a new file called filename .
    • For example, to copy tree.jpg from the pictures folder in user jen's home directory to /home/john/pictures/, you'd use cp /home/jen/pictures/tree.jpg /home/john/pictures/ .
    • You can also copy multiple files from the directory this way. For example, cp /home/jen/tree.jpg /home/jen/frog.jpg /home/jen/cat.jpg /home/john/pictures/ .
  2. Advertisement
Section 4 of 7:

Copying Multiple Directories

PDF download Download Article
    • If you want to copy multiple directories to another parent directory, start by using the cd command to enter the directory containing the ones you want to copy.
      • For example, if you want to copy /home/jen/pictures and /home/jen/music to /home/john, use cd /home/jen to enter /home/jen.
      • To copy the directories, use cp -r pictures music /home/john/ . The -r option ensures you'll copy any subdirectories.
Section 5 of 7:

Renaming a Directory

PDF download Download Article
  1. The mv (move) command is used to rename directories and files and will not harm the files or subfolders within the directory.
  2. Advertisement
Section 6 of 7:

Copying a Directory to a Remote Location

PDF download Download Article
  1. The syntax is scp -r /source/directory username@hostname:/destination/directory . Scp makes it easy to copy directories to remote Linux systems securely without using ftp or sftp.
    • For example, let's say you want to copy the pictures directory from your home directory to your account on a remote server called chihuahua.wikihow.com .
    • scp -r /home/myusername/pictures/ myusername@chihuahua.wikihow.com:/home/myusername/ .
Section 7 of 7:

Troubleshooting

PDF download Download Article
  1. error. If you get "permission denied" when using cp -r to copy a directory, there may already be a directory with that name at the target destination, containing at least one file with the same name. It's also possible that you do not have write permissions in the target directory.
    • For example, let's say you get this error when copying /home/jen/pictures to /home/john/pictures.
      • Use ls -l /home/john to check for a directory called "pictures."
      • If /home/john/pictures exists, run ls -l /home/john/pictures to see if there are already files in the directory with the same name(s) as the ones you're trying to copy from /home/jen/pictures. And because ls -l shows permissions, make sure you have write permissions on the target directory.
    • You can also try using cp -rp /source/directory /target/directory/ instead to preserve permissions from the original directory.
  2. error. This error indicates that either the source or target directory does not exist. Use ls -l to check both the source and target directories.
  3. If the directory you're copying contains symbolic links to other directories, use cp -a instead of cp -r .
    • If you've already copied the directory and now want to copy the symlinks , use cp -d /source/directory/symlink /target/directory/ .
  4. Advertisement

Expert Q&A

Ask a Question
      Advertisement

      Tips

      Submit a Tip
      All tip submissions are carefully reviewed before being published
      Name
      Please provide your name and last initial
      Thanks for submitting a tip for review!
      Advertisement

      About This Article

      Thanks to all authors for creating a page that has been read 2,378 times.

      Is this article up to date?

      Advertisement