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 fact-checked, ensuring the accuracy of any cited facts and confirming the authority of its sources.
This article has been viewed 2,378 times.
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/
Steps
Copying an Entire Directory
-
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] X Research source
- You can use the ls -a command to view all files and folders within a directory.
Copying a Directory's Contents
-
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/ .
- For example, let's say you want to copy the pictures from /home/jen/pictures into /home/john/pictures:
Copying a File to Another Directory
-
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/ .
Copying Multiple Directories
-
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.
- 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.
Renaming a Directory
-
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.
Copying a Directory to a Remote Location
-
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/ myusername@chihuahua.wikihow.com:/home/myusername/ .
Troubleshooting
-
1Permission 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.
- For example, let's say you get this error when copying /home/jen/pictures to /home/john/pictures.
-
2No 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.
-
3Symlinks 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/ .
Expert Q&A
Tips
- To learn more about the cp command, run the command man cp .Thanks
- Use the mkdir command to create a directory in Linux.Thanks