- Linux – How To Copy a Folder [ Command Line Option ]
- How To Copy a Folder with cp Command
- Linux cp command examples
- More examples of cp command to copy folders on Linux
- Use Linux rsync Command to copy a folder
- Conclusion
- How can I Copy Files in a Folder into Another Folder on Linux Terminal
- File copying in terminal
- Cp Command
- mv command
- Rsync Command
- Final thoughts
- About the author
- Sidratul Muntaha
- How to Copy files and folders in Linux
- What is cp command?
- Common syntax for cp command:
- Common options for cp command:
- 1) Copying files with cp command
- 2) Copying files with a different name
- 3) Copying multiple files in Linux
- 4) How to copy directories recursively
- 5) Copying multiple directories recursively
- 6) Copying specific format files in Linux
- 7) Copying all files at once in Linux
- 8) How to copy hidden files (‘.’ dot files) in Linux
- 9) Backup the file with cp command
- 10) Copying files with attributes
- 11) How to avoid file overwriting with cp command?
- 12) Prompting confirmation with cp command
- 13) Copying link files in Linux
- Conclusion:
Linux – How To Copy a Folder [ Command Line Option ]
How To Copy a Folder with cp Command
The cp command is a Linux command for copying files and directories. The syntax is as follows:
Linux cp command examples
In this example copy /home/vivek/letters/ folder and all its files to /usb/backup/ directory:
cp -avr /home/vivek/letters /usb/backup
Where,
- -a : Preserve the specified attributes such as directory an file mode, ownership, timestamps, if possible additional attributes: context, links, xattr, all.
- -v : Verbose output.
- -r : Copy directories recursively.
More examples of cp command to copy folders on Linux
Copy a folder called /tmp/conf/ to /tmp/backup/:
$ cp -avr /tmp/conf/ /tmp/backup/
Sample outputs:
Fig.01: cp command in action
Use Linux rsync Command to copy a folder
You can also use rsync command which is a fast and extraordinarily versatile file copying tool. It can make copies across the network. The syntax is as follows for the rsync command
To backup my home directory, which consists of large files and mail folders to /media/backup, enter:
$ rsync -avz /home/vivek /media/backup
I can copy a folder to remote machine called server1.cyberciti.biz as follows:
$ rsync -avz /home/vivek/ server1.cyberciti.biz:/home/backups/vivek/
Where,
- No ads and tracking
- In-depth guides for developers and sysadmins at Opensourceflare✨
- Join my Patreon to support independent content creators and start reading latest guides:
- How to set up Redis sentinel cluster on Ubuntu or Debian Linux
- How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
- How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
- A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
- How to protect Linux against rogue USB devices using USBGuard
Join Patreon ➔
- -a : Archive mode i.e. copy a folder with all its permission and other information including recursive copy.
- -v : Verbose mode.
- -z : With this option, rsync compresses the file data as it is sent to the destination machine, which reduces the amount of data being transmitted something that is useful over a slow connection.
Fig.02: rsync command in action
Conclusion
You just learned how to copy a folder on a Linux like operating system using the cp command and rsync command. In conclusion, use rsync for a network folder transfer and cp for a local disk transfer.
🐧 Get the latest tutorials on Linux, Open Source & DevOps via
Источник
How can I Copy Files in a Folder into Another Folder on Linux Terminal
File copying in terminal
Copying the content of one directory into another is a very simple task. However, you have plenty of options of doing it. Linux is blessed with numerous tools to manipulate files and directories through various actions.
All the steps are demonstrated on Ubuntu 18.04.1. At first, let’s create a handful of dummy files to work with.
Now, let’s create the dummy files using touch.
Let’s create another folder within dir_A with a handful of dummy files.
Now, the entire structure of the directory dir_A looks like this.
Cp Command
The cp command is, by far, the most common tool for copying files. It’s a tool that comes by default with any Linux system. Copying files and directories are the sole purpose of cp. It supports a wide range of features.
This is the basic structure of the cp command.
For example, let’s make a copy of the dummy1 file with the name dummy1_copy.
Now, let’s try copying multiple files at once. For such an operation, cp requires the destination be a directory. Here, I’ll be using wildcard expression to grab all the dummy files and put them into the
How about copying a directory?
Here, we used two different flags. Let’s have a quick breakdown of them.
- -r: Stands for a recursive copy (including any hidden file(s)). Must-have flag if copying involves an entire directory or more.
- -v: Stands for verbose. The cp tool will output every single action it’s performing.
Want to follow the symbolic links? Add the “-L” flag.
Let’s say the destination directory already has a file with the same name. If you want to avoid overwriting, use the “-n” flag. It’ll ensure that in case of conflict, cp won’t overwrite the file.
If you’re performing a sensitive copy/paste action, it’s better to have the finest control over what happens, right? In such a case, use the “-i” flag. It stands for an interactive mode where cp will ask for confirmation every time a conflict arises.
If you’re more interested, the man page is always one of the best documentation. Learn more about cp.
mv command
The mv tool is similar to the cp tool. However, instead of copying, mv moves files and directories. Compared to cp, it’s a simpler one.
The command structure of mv is similar to cp.
To move the contents of dir_A to dir_A_copy, run the following command.
Here, the “-v” flag is for verbose mode. If you want to move just the contents of dir_A, use the following one instead. In this case, the target directory must exist beforehand.
The mv tool doesn’t have much to work with. For all the available options, check out the man page.
Rsync Command
This is one of the most popular and powerful tools for file copying, used mostly for backup. It holds the ability to copy files over a remote connection. Unlike cp and mv, it also comes with a ton of advanced file copying options that offer the utmost customization of its behavior.
Rsync is mostly famous for its delta-transfer algorithm that dramatically reduces the amount of data that needs to be exchanged. In the case of remote syncing, this saves up a lot of bandwidth.
Rsync doesn’t generally come pre-installed in any Linux distro. However, because of its popularity, it’s now available on all the major Linux distros. Use Pkgs.org to find out the rsync package for your distro. Assuming that you got rsync installed, let’s get started.
First, the basic command structure of rsync. It’s quite similar to cp.
Before getting started, here are a handful of popular rsync arguments.
- -v: Verbose mode, outputs whatever action being performed on the console screen.
- -vv: Verbose mode with more details
- -r: Recursive mode, must be present if copying directories
- -z: Compress data
- -a: Archive data
- -n: Dry run, should be run in combination with verbose mode. Simply outputs the actions if the command was to run for real
- –delete: Delete the file(s) and directory(s) on the destination directory if the source directory doesn’t contain them any longer
- -no-whole-file: Forces rsync to use its delta transmission algorithm, no matter what. Useful in scenarios where you must ensure the least amount of data write.
- –remove-source-file: Delete source file(s) after copying.
Let’s check them out in action. First, the directory copy. Run the following command.
For further detailed output, let’s use the “-vv” flag.
Now, let’s check out the cool part of rsync. We discussed how rsync intelligently copies only the files that are modified, saving bandwidth and disk write. It’s time to check it out in action. In this example, the action will be performed within the dir_A directory.
Despite delta-transmission enabled, rsync may not apply it when you’re performing local data transfer.
Now, let’s check out the dry run feature. Before running any rsync command for real, it’s always important that you test it out beforehand. This is where the dry run comes in. Rsync will output what will happen if the command was to actually run.
Rsync can also work with remote directories, no problem. All it needs is a proper SSH connection with SSH keys configured.
Want to perform a move operation? To put that in rsync’s perspective, rsync will copy the contents of the source directory and then, delete the source content.
Check out the result of the operation.
Final thoughts
The cp and rsync are the most common tools you can use for copying files in a folder on the Linux terminal. Both of them are also really good for scripting. Just make sure to test the functions out before implementation.
About the author
Sidratul Muntaha
Student of CSE. I love Linux and playing with tech and gadgets. I use both Ubuntu and Linux Mint.
Источник
How to Copy files and folders in Linux
Being a Linux user, copying files and directories is one of the most common tasks.
Linux users don’t spend a day without using the cp (copy) command according to my personal experience. cp command is used to copy a single file or group of files or directory.
To perform the copy operation, you must have at least read permission in the source file and write permission in the target directory.
In this article, we will explain how to use the cp command.
To perform remote file copy, use the commands such as rsync command or scp command or pscp command.
What is cp command?
‘cp’ command is one of the basic and most widely used Linux commands for copying files and directories from one location to another.
When copying files from source to destination, the source file remains the same and the target file may have the same or different name.
Common syntax for cp command:
Common options for cp command:
Options | Description |
---|---|
-v | Verbose mode (Show progress) |
-r/R | Copy directories recursively |
-n | Don’t overwrite an existing file |
-d | Copy a link file |
-i | Prompt before overwrite |
-p | Preserve the specified attributes |
-b | Make a backup of each existing destination file |
To demonstrate file copy operation in detail, we will be performing a copy operation between user1 and user2 .
1) Copying files with cp command
Use the following command to copy a file from one location to another.
In the following example, we will copy the source file “/home/user1/cp-demo.txt” to the target directory “/home/user2” with the same name:
You can check whether the specific file is copied to the target directory using the ls command as shown below:
2) Copying files with a different name
Use the following command to copy a file from one place to another with a different name. You need to give a new name to the target file.
In the following example, we will copy a file named “/home/user1/cp-demo.txt” to the target directory with a new name “/home/user2/cp-demo11.txt” :
Use the ls command to verify this:
3) Copying multiple files in Linux
The following cp command copies multiple files from one location to another.
In this example, we will copy the following three files named “cp-demo.txt, cp-demo-1.txt and cp-demo-9.txt” to the target.
No additional option is required to copy multiple files, and the file name(s) must be separated with a space as shown below:
Use the ls command to verify the copied files. You should see them in the target directory as shown below:
4) How to copy directories recursively
To copy a directory recursively from one location to another, use the -r/R option with the cp command. It copies everything, including all its files and subdirectories.
In the following example, we will copy the ‘/home/user1/cp-demo-folder’ directory to ‘/home/user2/’ and the target directory name will remain the same:
Use the ls command to verify the outcome. You should see them in the target directory as shown below:
5) Copying multiple directories recursively
Use the following command to copy multiple directories at once. In the following example, we will copy the public_html & public_ftp folders to the target directory named /home/2daygeek/cp-test.
Output can be verified using the ls command:
6) Copying specific format files in Linux
Sometimes you may have to copy files with a specific extension for a certain purpose. If so, you should use the cp command with the «wildcard (*)» option and the file extension name.
In the following example, we will copy all the files containing the *.sh extension from the source to the target directory. Similarly, you can copy any file extensions such as .jpg, .png, .txt, .php, and .html.
This can be verified using the ls command:
7) Copying all files at once in Linux
To copy all the files except the directory from one location to another, use the following cp command with wildcard (*) option.
cp command excludes directories by default, and the -r option must be added to copy them.
In this example, we will copy all the files from ‘/home/user1/’ to ‘/home/user2′ :
Verify the output using the ls command as shown below:
8) How to copy hidden files (‘.’ dot files) in Linux
By default, the cp command does not copy “dot (.)” or ‘hidden’ files in Linux.
Use the following command to copy all types of files, including link files (soft link or hard link), folders and hidden or dot files from source to destination.
This example is very useful to copy user’s home directory which has several hidden files:
You can verify this by using the ls command with the “-a” option:
9) Backup the file with cp command
cp command allows you to backup the file if the same file name exists in the target directory using the —backup option.
In this example, we will copy the /home/user1/passwd-up.sh file to the target directory /home/user2/.
It will backup the “passwd-up.sh” file in the target directory during the copy operation, if you say “yes” when you see the message below:
It adds the “Tilde (
)” symbol at the end of the old file, the same can be verified in the following output.
10) Copying files with attributes
By default, Linux replaces file attributes such as username, group name, date and time when copying files from a user, and does not carry the original file attributes.
To preserve the original attributes of the files, use the -p option with the cp command.
Upon querying we can see that the file “service-1.sh” has retained its original attributes as shown below:
11) How to avoid file overwriting with cp command?
When copying a file, use the -n option with the cp command to avoid overwriting an existing file.
It will only copy the source file, if the target directory does not have a file with the same name. If a duplicate file exists in the target, then the command will run, but won’t make any changes:
Upon querying using the ls command, it can be seen that no action is taken against the file because it still has the old attributes, which can be seen in the above example (refer section 10):
12) Prompting confirmation with cp command
Use the -i option with the cp command to prompt for confirmation before overwriting the file, in case the file exists in the target location.
The following example prompts for confirmation when copying the “cp-demo-9.txt” file because the file already exists in the target directory:
13) Copying link files in Linux
By default, the cp command excludes link files while performing the copy operation. Use the -d option with the cp command to copy the link files as shown below:
It has been successfully copied, and the soft link file can be found in the below output:
Conclusion:
In this tutorial, we have demonstrated 13 methods that are widely used by the Linux administrators in their daily operations. If you would like to explore other options, please visit the man page of the cp command by using the following command:
Источник