- How to Copy a Directory in Linux Command Line [Beginner’s Tip]
- Copy directory in Linux command line
- A few things to note about copying directory in Linux
- The destination directory doesn’t exist? It will be created
- Can’t create nested directory while copying
- Preserve the original file attributes
- Copy a Directory in Linux – How to cp a Folder in the Command Line in Linux and Unix (MacOS)
- How to copy a file to the current directory
- How to copy a file to another directory
- How to copy multiple files to a directory
- How to copy a directory to another directory
- How to copy the entire directory vs the contents of the directory
- How to prevent overwriting files with cp
- The interactive flag
- The no-clobber flag
- Other options
- Copy Directory/Folder in Linux via Command Line
- Basic Syntax of cp Command
- Copying Directories with cp Command
- Copying Directories with rsync Command
- Conclusion
- Comments & Discussion:
- Recent Posts
- Online DNS Tools
- Contact & Legal
How to Copy a Directory in Linux Command Line [Beginner’s Tip]
This quick tip for beginners demonstrates how to copy a directory in Linux using the cp command. You’ll also learn a few additional tips around copying directory.
If you are new to Linux command line, you probably have this question in mind: How do I copy a directory and the files inside it to another directory in Linux command line?
Here’s how to copy a directory in Linux:
cp -r source_directory destination_directory
You probably already know that you can use cp command to copy files in Linux. Do you know that you can use the same cp command to copy a folder in Linux command line?
Hah! You already tried that and perhaps got this error:
cp: -r not specified; omitting directory 'test_dir'
Let me show you a thing or two about copying directory in Linux.
Copy directory in Linux command line
You can definitely use the same cp command but with the recursive option -r to copy a folder with its content to another folder. The above mentioned error also hints that you missed the -r option.
All you got to do is to use the command in this fashion:
cp -r source_directory destination_directory
And now if you use ls command on the destination directory, it should have the entire source directory inside it.
The -r option allows the recursive option. This means the entire content of the directory including its own subdirectories, everything in the directory will be copied to the destination.
A few things to note about copying directory in Linux
Here are a couple of things to note and tips about copying folders.
The destination directory doesn’t exist? It will be created
If the destination directory doesn’t exist (but the path exists), it will be created up to one level (explained in the next section). However, it will now copy the contents of the source directory, not the source directory itself.
For example, if you do this:
cp -r source_directory non_existing_directory
The non_existing_directory will be created with the content of the source_directory but it won’t have the source_directory inside it. Just the files of source_directory will be copied. It would be like non_existing_directory will be a replica of source_directory.
[email protected]:~$ ls test_dir [email protected]:~$ cp -r test_dir new_dir [email protected]:~$ tree . ├── new_dir │ ├── c.xyz │ ├── myzip1.zip │ └── myzip2.zip └── test_dir ├── c.xyz ├── myzip1.zip └── myzip2.zip 2 directories, 6 files
Can’t create nested directory while copying
You cannot use the above command to create nested directory structure.
For example, if you try to use cp -r source_dir dir1/dir2/dir3 but dir2 and dir3 don’t exist, it won’t create the nested directory structure and the command fails.
Preserve the original file attributes
One last tip to keep things short. If you use the -a option along with the -r option, it will preserve the original file information such as file permissions, file timestamps etc. It will archive the directory to the new location instead of creating it afresh.
This much information should be enough for you to know how to copy a directory in Linux. If you have questions or suggestions, please feel free to leave a comment.
Copy a Directory in Linux – How to cp a Folder in the Command Line in Linux and Unix (MacOS)
To copy files or directories in Unix-based operating systems (Linux and MacOS), you use the cp command.
The cp command is a relatively simple command, but its behavior changes slightly depending on the inputs (files vs directories) and the options you pass to it.
To view the documentation or manual for the cp command, run man cp at your terminal:
$ man cp NAME cp -- copy files SYNOPSIS cp [OPTIONS] source_file target_file cp [OPTIONS] source_file . target_directory .
The basic form of this command takes an input source (or sources) that you want to copy (files or directories) and a destination to copy the files or directories to:
cp [OPTIONS] source_file target_file
How to copy a file to the current directory
To copy a file, pass the file you want to copy and the path of where you want to copy the file to.
If you have a file named a.txt , and you want a copy of that file named b.txt :
$ ls a.txt $ cp a.txt b.txt $ ls a.txt b.txt
By default the cp command uses your current directory as the path.
How to copy a file to another directory
To copy a file to a directory that is different from your current directory, you just need to pass the path of the other directory as the destination:
$ ls ../directory-1/ $ cp a.txt ../directory-1/ $ ls ../directory-1/ a.txt
After the cp command, the previously empty directory-1 now contains the file a.txt .
By default the copied file receives the name of the original file, but you can also optionally pass a file name as well:
$ cp a.txt ../directory-1/b.txt $ ls ../directory-1/ b.txt
How to copy multiple files to a directory
To copy more than one file at a time you can pass multiple input sources and a directory as destination:
$ ls ../directory-1/ $ cp first.txt second.txt ../directory-1/ $ ls ../directory-1/ first.txt second.txt
Here the two input sources ( first.txt and second.txt ) were both copied to the directory directory-1 .
How to copy a directory to another directory
If you try to pass a directory as the input source, you get this error:
$ cp directory-1 directory-2 cp: directory-1 is a directory (not copied).
To copy a directory, you need to add the -r (or -R ) flag—which is shorthand for —recursive :
$ ls directory-1 a.txt $ cp -r directory-1 directory-2 $ ls directory-1 directory-2 $ ls directory-2 a.txt
Here directory-1 containing the file a.txt is copied to a new directory called directory-2 —which now also contains the file a.txt .
How to copy the entire directory vs the contents of the directory
There is an interesting edge case when you copy a directory: if the destination directory already exists, you can choose whether to copy the contents of the directory or the entire directory by adding or removing a trailing / from your input.
Here’s the description from the -R option of the man page:
If source_file designates a directory, cp copies the directory and the entire subtree connected at that point. If the source_file ends in a /, the contents of the directory are copied rather than the directory itself.
If you want to copy just the contents of the directory into another directory, add a trailing / to your input.
If you want to copy the contents of the directory and the directory folder itself into another directory, don’t add a trailing / :
$ ls directory-1 directory-2 $ ls directory-2 $ cp -r directory-1 directory-2 $ ls directory-2 directory-1 $ ls directory-2/directory-1 a.txt
Here you can see that because directory-2 already exists—and the input source didn’t have a trailing / —both the contents of directory-1 and the directory itself was copied into the destination.
How to prevent overwriting files with cp
By default, the cp command will overwrite existing files:
$ cat a.txt A $ cat directory-1/a.txt B $ cp a.txt directory-1/a.txt $ cat directory-1/a.txt A
There are two ways to prevent this.
The interactive flag
To be prompted when an overwrite is about to occur, you can add the -i or —interactive flag:
$ cp -i a.txt directory-1/a.txt overwrite directory-1/a.txt? (y/n [n])
The no-clobber flag
Or, to prevent overwrites without being prompted, you can add the -n or —no-clobber flag:
$ cat a.txt A $ cat directory-1/a.txt B $ cp -n a.txt directory-1/a.txt $ cat directory-1/a.txt B
Here you can see that thanks to the -n flag the contents of directory-1/a.txt were not overwritten.
Other options
There are many other useful options to pass to the cp command: like -v for «verbose» output or -f for «force.»
I highly encourage you to read through the man page for all of the other useful options.
If you liked this tutorial, I also talk about topics like this on Twitter, and write about them on my site.
Copy Directory/Folder in Linux via Command Line
There are various commands in Linux operating systems to copy a folder. The cp command helps you to do so. To organize files on your server, you will need to be copying. With cp command, you can copy a directory and an entire subdirectory with its content and everything beneath it. cp and rsync are one of the most popular commands for copying files and directory.
In this tutorial, we will explain how to copy the folder in Linux operating system.
Basic Syntax of cp Command
cp command is used to copy files or directories in Linux. It creates an exact copy of a file on a disk with different name.
basic syntax of cp command is shown below:
cp [OPTION] Source Destination
cp [OPTION] Source-1 Source-2 Source-3 Destination
You should see all the options available with cp command by running the following command:
You should see the following screen:
Copying Directories with cp Command
If you want to copy directory, including all its files and subdirectories, use -R or -r option with cp command.
For example, copy /etc directory to the /opt directory with the following command:
The above command will create a destination directory and copy all files and subdirectories recursively to the /opt directory.
If the destination directory already exists and you want to copy only the files and subdirectories but not the target directories, run the cp command with -T option as shown below:
If you want to preserve the specified attributes such as, ownership, timestamps, context and links, run the cp command with -a option as shown below:
If you want to display output during the copying process, use -v option with cp command:
You should see the output as shown below:
If you want to copy only the content of /etc directory to /opt, you will need to specify the star wildcard as shown below:
If you want to copy multiple directories like, /etc and /var to /opt directory run the following command:
Note : In order to copy directories, you must have read permissions on source directory and write permissions on the destination directory.
Copying Directories with rsync Command
rsync is an advanced file copying tool that allows you to copy directory across local the remote location.
The basic syntax of rsync command is shown below:
rsync [OPTION] SOURCE DESTINATION
You can see all the options available with rsync command as shown below:
You should see the following screen:
To copy /etc directory to /opt, run the following command:
In the above command -a option will copy a directory with all its permission and other information including recursive copy. If the destination directory exists rsync will overwrite it.
If you want to copy only the contents of the /etc directory then put a trailing slash / at the end of /etc:
You can use rsync with -v option to display the verbose output as shown below:
You should see the following screen:
If you want to display progress during the copying process, run the rsync with -P option as shown below:
You can also use -z option with rsync command to compress file data during the transfer:
If you want to exclude a specific directory from the source, run the following command:
rsync -a —exclude ‘directoryname’ /opt/
To excluse multiple directories, run the following command:
If you want to copy multiple directories to /opt, run the following command:
Conclusion
In the above tutorial, we learned how to copy a directory with cp and rsync command. For more information, you can visit the Rsync official documentation at Rsync Doc.
Comments & Discussion:
Thanks for the tips, we use Rsync for local to remote backups and this was helpful for using it to copy local/local!
Recent Posts
- Forcepoint Next-Gen Firewall Review & Alternatives
- 7 Best JMX Monitoring Tools
- Best PostgreSQL Backup Tools
- Best CSPM Tools
- Best Cloud Workload Security Platforms
- Best Automated Browser Testing Tools
- Event Log Forwarding Guide
- Best LogMeIn Alternatives
- Citrix ShareFile Alternatives
- SQL Server Security Basics
- Cloud Security Posture Management Guide
- Cloud Workload Security Guide
- The Best JBoss Monitoring Tools
- ITL Guide And Tools
- 10 Best Enterprise Password Management Solutions
Online DNS Tools
Contact & Legal
WebServerTalk participates in many types affiliate marketing and lead generation programs, which means we may get paid commissions on editorially chosen products purchased through our links. This is not change the outcome of any reviews or product recommedations.