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 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
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
Name
Please provide your name and last initial
Thanks for submitting a tip for review!
Advertisement

You Might Also Like

Transfer Files from One Linux Server to Another Transfer Files from One Linux Server to Another
Copy Files in Linux Copy Files in Linux: A Complete Guide
Copy Files in Command PromptCopy Files in Command Prompt
Rename Directory Linux Rename Linux Directory in Ubuntu, Fedora, & More
Cygwin Command Cheat SheetCygwin Command Cheat Sheet
Use SSHUse SSH
Linux How to SymlinkCreate Symbolic Links in Linux: Everything You Need to Know
Copy Files to USB Using CMD Copy Files to a USB Using Command Prompt (CMD)
Move Files in Ubuntu Move Files in Ubuntu
Use the Command Terminal in Windows 10 to Move and Copy Files/Folders Use the Windows 10 CLI to Move & Copy Files or Folders
Tar a Directory Tar a Directory and Subdirectories in Linux
Use MS DOS Use MS DOS
Change Directories in Command Prompt Navigate to a Different Folder in the Command Prompt
CD to iCloud Drive on Mac CD to Your iCloud Drive on a Mac
Advertisement

About This Article

Stan Kats
Co-authored by:
Professional Technologist
This article was co-authored by Stan Kats and by wikiHow staff writer, Nicole Levine, MFA. Stan Kats is a Professional Technologist and the COO and Chief Technologist for The STG IT Consulting Group in West Hollywood, California. Stan provides comprehensive technology solutions to businesses through managed IT services, and for individuals through his consumer service business, Stan's Tech Garage. Stan holds a BA in International Relations from The University of Southern California. He began his career working in the Fortune 500 IT world. Stan founded his companies to offer an enterprise-level of expertise for small businesses and individuals. This article has been viewed 2,715 times.
How helpful is this?
Co-authors: 3
Updated: May 27, 2024
Views: 2,715
Categories: Linux
Thanks to all authors for creating a page that has been read 2,715 times.

Is this article up to date?

Advertisement