- Basic Syntax of cp Command
- Copying Directories with cp Command
- Copying Directories with rsync Command
- Conclusion
- Comments & Discussion:
- Команда cp: правильное копирование папок с файлами в *nix
- Выводы
- Послесловие
- How to copy directory in Linux
- How to copy Directory on Linux: Using cp command
- How to Copy Directory on Linux – Real Example 1
- How to copy multiple files in Linux
- Copy only the content with cp command
- Using the option -T to copy files on Linux
- How to avoid file overwriting a file with the cp command?
- How to Copy multiple directories with cp on Linux
- Copy Preserve Permissions Linux
- Copy Files and Preserve Creation Date on Linux
- How to force copy in Linux
- Prompt Before Overwrite
- How to copy link files on Linux
- How to copy symbolic links
- Copy newest file in directory Linux
- How to create a Hard Link
- How to copy Directory (s) to Remote Hosts on Linux
- How to Copy Directory and files using rsync on Linux
- Real Example – How to use rsync
- How to copy directory and files using scp on Linux
- Real Example – How to use scp
- Conclusion
- 2 thoughts on “How to copy directory in Linux”
Last Updated: September 21st, 2020 by Hitesh J in Guides, Linux
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:
cp -avR /etc /var /opt/
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:
rsync -a /etc /opt
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:
rsync -a /etc/ /opt
You can use rsync with -v option to display the verbose output as shown below:
rsync -av /etc /opt
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:
rsync -avP /etc /opt
You can also use -z option with rsync command to compress file data during the transfer:
rsync -avz /etc /opt
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:
rsync -a /etc /usr /opt/
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!
Команда cp: правильное копирование папок с файлами в *nix
В этой статье будут раскрыты некоторые неочевидные вещи связанные с использованием wildcards при копировании, неоднозначное поведение команды cp при копировании, а также способы позволяющие корректно копировать огромное количество файлов без пропусков и вылетов.
Допустим нам нужно скопировать всё из папки /source в папку /target.
Первое, что приходит на ум это:
Сразу исправим эту команду на:
Ключ -a добавит копирование всех аттрибутов, прав и добавит рекурсию. Когда не требуется точное воспроизведение прав достаточно ключа -r .
После копирования мы обнаружим, что скопировались не все файлы — были проигнорированы файлы начинающиеся с точки типа:
.profile
.local
.mc
и тому подобные.
Почему же так произошло?
Потому что wildcards обрабатывает shell ( bash в типовом случае). По умолчанию bash проигнорирует все файлы начинающиеся с точек, так как трактует их как скрытые. Чтобы избежать такого поведения нам придётся изменить поведение bash с помощью команды:
Чтобы это изменение поведения сохранилось после перезагрузки, можно сделать файл wildcard.sh c этой командой в папке /etc/profile.d (возможно в вашем дистрибутиве иная папка).
А если в директории-источнике нет файлов, то shell не сможет ничего подставить вместо звёздочки, и также копирование завершится с ошибкой. Против подобной ситуации есть опции failglob и nullglob . Нам потребуется выставить failglob , которая не даст команде выполниться. nullglob не подойдёт, так как она строку с wildcards не нашедшими совпадения преобразует в пустую строку (нулевой длины), что для cp вызовет ошибку.
Однако, если в папке тысячи файлов и больше, то от подхода с использованием wildcards стоит отказаться вовсе. Дело в том, что bash разворачивает wildcards в очень длинную командную строку наподобие:
На длину командной строки есть ограничение, которое мы можем узнать используя команду:
Получим максимальную длину командной строки в байтах:
Получим что-то типа:
Итак, давайте будем обходиться вовсе без wildcards.
Давайте просто напишем
И тут мы столкнёмся с неоднозначностью поведения cp . Если папки /target не существует, то мы получим то, что нам нужно.
Однако, если папка target существует, то файлы будут скопированы в папку /target/source.
Не всегда мы можем удалить заранее папку /target, так как в ней могут быть нужные нам файлы и наша цель, допустим, дополнить файлы в /target файлами из /source.
Если бы папки источника и приёмника назывались одинаково, например, мы копировали бы из /source в /home/source, то можно было бы использовать команду:
И после копирования файлы в /home/source оказались бы дополненными файлами из /source.
Такая вот логическая задачка: мы можем дополнить файлы в директории-приёмнике, если папки называются одинаково, но если они отличаются, то папка-исходник будет помещена внутрь приёмника. Как скопировать файлы из /source в /target с помощью cp без wildcards?
Чтобы обойти это вредное ограничение мы используем неочевидное решение:
Те кто хорошо знаком с DOS и Linux уже всё поняли: внутри каждой папки есть 2 невидимые папки «.» и «..», являющиеся псевдопапками-ссылками на текущую и вышестоящие директории.
- При копировании cp проверяет существование и пытается создать /target/.
- Такая директория существует и это есть /target
- Файлы из /source скопированы в /target корректно.
Итак, вешаем в жирную рамочку в своей памяти или на стене:
Поведение этой команды однозначно. Всё отработает без ошибок вне зависимости от того миллион у вас файлов или их нет вовсе.
Выводы
Если нужно скопировать все файлы из одной папки в другую, не используем wildcards, вместо них лучше использовать cp в сочетании с точкой в конце папки-источника. Это скопирует все файлы, включая скрытые и не завалится при миллионах файлов или полном отсутствии файлов.
Послесловие
vmspike предложил аналогичный по результату вариант команды:
ВНИМАНИЕ: регистр буквы T имеет значение. Если перепутать, то получите полную белиберду: направление копирования поменяется.
Благодарности:
How to copy directory in Linux
On Linux and Unix systems, copying files and directories are one of the countless everyday tasks you’ll perform regularly.
cp is a command for copying files and directories on any Unix and Linux system.
In this post, we will demonstrate how to employ the cp command and taking advantage of it.
How to copy Directory on Linux: Using cp command
The generic syntax for the cp command is as below:
cp [OPTIONS] SOURCE… DESTINATION
The SOURCE can include one or more directories and files as arguments. The DESTINATION argument must be an individual file or directory.
Both file_name and file_name.bkp are files. In this example, we are creating a copy from the fileA (making a backup).
Where source and destination are folders, we copy the “source” directory inside the “destination” folder. Note that in this example, we have used the -R argument. Why? It is because we are telling the cp command to copy this directory recursively. Thus, in the case of directories, the -R argument is necessary.
How to Copy Directory on Linux – Real Example 1
Let’s do a real example. For example, you decided to format your computer to install a new version of Linux Ubuntu. You would like to backup your home directory to your USB external drive and save all files from it.
Here in this example, my home directory is bitslovers. My USB device is mounted on /mnt/media/usb01, so it is our destination folder.
How to copy multiple files in Linux
To copy more than one file at once, you need to specify them separated by space. The last parameter should be your destination directory. For example:
Copy only the content with cp command
But, sometimes, you need only copy the content of the directory only, not the folder itself, to inside the destination. First, consider that scenario: You need to move all your photos from your USB device to a specific folder inside your computer that you are splitting by year.
So, the folder (source) that contains the photos is on /mnt/media/usb01/DCM0001. We want to copy all files from DCM0001 to /home/bitslovers/Pictures/2021 (destination). Our goal is not to copy the DCM0001 folder itself. We intend to copy only the files from it.
Here is the command:
Note: to copy the content of a directory recursively. You have to use the cp command with the -R parameter and define the source directory attended by a wildcard (*) character.
Using the option -T to copy files on Linux
Alternatively, to copy only the files and subdirectories but not the source directory, we can also use the -T option, so our example will be like this:
What is the difference between using -T and not use? It’s simple but crucial: The downside of using the cp command with a wildcard (*) is that this method does not copy the hidden files and directories.
Note: on Linux, the hidden files and folders start with a dot. If you would like to go deep into the Linux world, we have a complete guide.
How to avoid file overwriting a file with the cp command?
Sometimes we want to copy a file, but we don’t want to override it if it already exists.
To achieve that, we can use the -n option with the cp command to skip and not overwriting an existing file.
So, it will only copy the source file if the destination directory does not have it with the same file name. In other words, if the file exists, then the command will run but won’t apply any changes:
In addition, for security, if you would like that cp command to always prompt for confirmation before overriding the files, you can use the option -i.
So, if the file backup-apache2.sh already exists on the destination folder, you will see a message like this:
How to Copy multiple directories with cp on Linux
To copy various directories on Linux, you have to apply the cp command and enter the different directories to be copied as well as the destination folder, for example:
Imagine one scenario as an example: we need to copy the “/etc” directory and all homes directories located in the “/home/bitslovers” directory.
To accomplish that, we would run the following command
Copy Preserve Permissions Linux
Sometimes we need to copy files to another location. Still, we need to keep the same attributes like mode, ownership, and timestamps. So the standard behavior, when you execute the copy command, the new file will have different attributes. For example, suppose you are copying one file using sudo. In that case, the new copy will be owned by the root user automatically.
This scenario could be an issue if the previous owner needs to access this file because this new file belongs to the root user. So, to fix that issue, we can copy the file using “-p” as a parameter for the cp command.
- -p same as –preserve=mode,ownership,timestamps
Let me show you an example:
So, in this example above, the new file under the backup folder will be accessible for the user bitslovers.
Copy Files and Preserve Creation Date on Linux
If you need to preserve the timestamps (the creation date):
How to force copy in Linux
To force a copy on Linux, we need to append the option -f on the cp command, for example:
Prompt Before Overwrite
When the cp no prompt before overwrite, you need to use the option -i.
If the file .bashrc already exists on the home directory, you will see a prompt:
cp: overwrite ‘/home/bitslovers/.bashrc’?
Type n or y to confirm.
How to copy link files on Linux
The cp command, by default, does not copy link files while executing the copy. So, you can add -d option to copy the link files like the example below:
How to move files from a specific extension to a particular folder?
Following, use xargs and cp to copy all the images to an external hard drive.
The command xargs is a powerful combination with any Linux command. I want to recommend reading the post xargs examples to give you professional skills to handle complex scenarios. The limitation is your imagination!
How to copy symbolic links
Let’s suppose that you need to copy a directory, and we have a couple of symbolic links on it. And we need to copy all files from this directory to a new location, but not as symbolic links. So, we can use the -L option to copy all files without reference to the original files, removing the Symbolic Link.
Copy newest file in directory Linux
If you would like to copy only files that were changed and copy new files that don’t exist on the destination, you can use the option -u.
In other words, copy only when the SOURCE file is newer than the destination file or when the destination file is missing.
In the example above, we are copying all files from the docs folder, which is newer than those already existing on the bitslovers folder. Also, we are copy all files that do not exist yet in the bitslovers folder.
How to create a Hard Link
The cp command allows us to copy files creating Hard Links for each one. Thus, hard Link is an alternative for creating a backup automatically and replicating all changes in real-time to the backup file. And in my opinion, it is much better than the Symbolic Link.
Let me explain why:
Hard Link behaves differently when the Link’s source (what is being linked to) is moved or removed.
However, because symbolic links never update (they only include a string that is the target’s pathname), hard links always refer to the source, even if moved or removed. Therefore, I would say that Hard Link it’s an unbreakable version of Symbolic Link.
For instance, if we have a file blog-post.doc. If we make a hard link to the file and then delete the file, we can still access the file using Hard Link. However, if we make a Symbolic Link of the file and remove the file, we can’t access the file through it, and the Symbolic Link becomes dangling.
Hard Link extends the reference number of a location while Symbolic Link works as a shortcut (like Windows).
Cool, right? Let me show you how to copy and create Hard Link:
In the example above, if you accidentally delete one file from the Documents or tmp folder, you can still access the file. And, any change on the files will be replication to each other.
How to copy Directory (s) to Remote Hosts on Linux
Sometimes, you may need to copy a directory and files to a server to install some application or make a backup.
We will need to transfer using a network, which could be the internet or a local network.
How to Copy Directory and files using rsync on Linux
Like the cp command, we need to specify the origin and destination. The rsync follows the same approach. Most importantly, always use -r, which means recursive for this command. It will guarantee that you copy all content followed by option -a, which means “ALL,” this will copy all non-regular files.
Before continuing, you can check if you have the rsync command installed so that you can use that command:
You will see an output like this:
If you don’t have installed, it’s simple to install it:
For Ubuntu or any Debian based Linux:
If you are running CentOS or Fedora or any RedHard based:
So, the basic syntax for rysnc command:
Real Example – How to use rsync
Our scenario as an example: Let’s suppose that you need to back up the configuration files from your web server running apache2.
We are connected to the server (3.324.174.100). In this example that we have the webserver apache2 running.
And, we are copying that folder “/etc/apache2” from that server to another remote host (3.324.174.201).
Note: You must have SSH access to the remote host (in my example, 3.324.174.201) and, besides privileges, write on the destination directory (on example /home/bitslovers). In other words, you have to have the credentials for the bitslovers user.
Like the cp command, you can choose to copy the “/etc/apache2” content instead of the directory itself by adding a wildcard character following the directory to be copied.
How to copy directory and files using scp on Linux
The scp is very similar to the rsync command and the way you use it too.
So, to copy directories using it, we need also to use the “-r” option that means the “recursive” method, besides our origin and destination.
Let’s use the same example from our rsync command. We need to copy the “/etc/apache2” directory to a backup server at 3.324.174.201 in the “/home/bitslovers/apache2” folder.
the following command accomplishes that:
Real Example – How to use scp
Alternatively, you can revert the scp command like this:
Revert this command sometimes is helpful because sometimes we are already connected to the destination server. We don’t want to disconnect. In other words, we revert the origin and destination if you are already connected to the destination server.
There is any difference between rsync and scp?
The significant difference between these commands is how they copy files:
scp essentially reads the source file and writes it to the destination. It does a traditional linear copy. It could be locally or over a network.
Otherwise, the rsync command also copies files locally or over a network. But it applies a unique delta transfer algorithm and some optimizations to gain the operation a lot quicker.
Conclusion
Copying files and directories on Linux with the cp, scp, and rsync command is straightforward. Also, we can combine the cp command with the Find command, so in that way, we can perform more elaborate copy operations.
Also, a good tip: To transfer or copy files, consider compressing the files to a zip or tar.gz format. So, you can save time for big files or multiple files.
To learn more about different options available to use with the commands, type man cp, man scp, or man rsync in your terminal.
2 thoughts on “How to copy directory in Linux”
Hello! Do you use Twitter? I’d like to follow you if that would be okay.
I’m undoubtedly enjoying your blog and look forward to
new updates.