How to Copy a Directory with the cp Command on Linux

Download Article
A simple step-by-step guide to copying folders and their contents
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

Download Article
  1. How.com.vn English: Use cp -r /source/directory /target/directory/.
    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

Download Article
  1. How.com.vn English: Use cp /source/directory/* /target/directory/.
    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

Download Article
  1. How.com.vn English: Use cp /source/directory/filename /target/directory/.
    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

Download Article
  1. How.com.vn English: Enter the parent directory and use cp with a wildcard.
    • 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

Download Article
  1. How.com.vn English: Use mv /old/name /new/name.
    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

Download Article
  1. How.com.vn English: You can use scp to copy directories remotely.
    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/ [email protected]:/home/myusername/.
Section 7 of 7:

Troubleshooting

Download Article
  1. How.com.vn English: Step 1 Permission denied
    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. How.com.vn English: Step 2 No such file or directory
    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. How.com.vn English: Step 3 Symlinks aren't being copied.
    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
200 characters left
Include your email address to get a message when this question is answered.
Submit
      Advertisement

      Tips

      Submit a Tip
      All tip submissions are carefully reviewed before being published
      Thanks for submitting a tip for review!
      Advertisement

      About This Article

      How.com.vn English: Nicole Levine, MFA
      Written by:
      How.com.vn Technology Writer
      This article was co-authored by How.com.vn staff writer, Nicole Levine, MFA. Nicole Levine is a Technology Writer and Editor for How.com.vn. She has more than 20 years of experience creating technical documentation and leading support teams at major web hosting and software companies. Nicole also holds an MFA in Creative Writing from Portland State University and teaches composition, fiction-writing, and zine-making at various institutions.
      How helpful is this?
      Co-authors: 3
      Updated: May 27, 2024
      Views: 277
      Categories: Linux
      Thanks to all authors for creating a page that has been read 277 times.

      Is this article up to date?

      ⚠️ Disclaimer:

      Content from Wiki How English language website. Text is available under the Creative Commons Attribution-Share Alike License; additional terms may apply.
      Wiki How does not encourage the violation of any laws, and cannot be responsible for any violations of such laws, should you link to this domain, or use, reproduce, or republish the information contained herein.

      Notices:
      • - A few of these subjects are frequently censored by educational, governmental, corporate, parental and other filtering schemes.
      • - Some articles may contain names, images, artworks or descriptions of events that some cultures restrict access to
      • - Please note: Wiki How does not give you opinion about the law, or advice about medical. If you need specific advice (for example, medical, legal, financial or risk management), please seek a professional who is licensed or knowledgeable in that area.
      • - Readers should not judge the importance of topics based on their coverage on Wiki How, nor think a topic is important just because it is the subject of a Wiki article.

      Advertisement