- Copy Files and Directories in Linux
- An Introduction to cp
- Before You Begin
- How to Use the cp Command to Copy Files and Directories in Linux
- How to Copy a File in Linux
- How to Copy Files in Linux
- How to Copy Files in Linux
- 1. cp
- 2. rsync
- 3. xcp
- Copying Files in Linux Using Regular Expression
- Special Copying of Files Using Tar Command
- Wrapping Up
Copy Files and Directories in Linux
Estamos traduciendo nuestros guías y tutoriales al Español. Es posible que usted esté viendo una traducción generada automáticamente. Estamos trabajando con traductores profesionales para verificar las traducciones de nuestro sitio web. Este proyecto es un trabajo en curso.
Copying a file is one of the most common Linux tasks. Ubuntu and other Linux distributions use the cp command to copy one or more files, and to copy entire directories. This guide explains how to use the cp command to copy files on Linux. It also lists the different variations of this command and describes the different cp command options.
An Introduction to cp
The cp command is used to copy one or more files on a Linux system to a new location. It is similar to the mv command, except it does not move or remove the original file, which remains in place. Like most Linux commands, cp is run using the command line of a system terminal.
The cp command allows users to copy a file to either the same directory or a different location. It is also possible to give the copy a different name than the original file. The -r option enables the cp command to operate recursively and copy a directory along with any files and subdirectories it contains. cp has a number of options, allowing users to run it interactively, use verbose mode, or preserve the file attributes of the original.
Users must have sudo privileges to copy protected files. Otherwise, sudo is not required.
Before You Begin
- If you have not already done so, create a Linode account and Compute Instance. See our Getting Started with Linode and Creating a Compute Instance guides.
- Follow our Setting Up and Securing a Compute Instance guide to update your system. You may also wish to set the timezone, configure your hostname, create a limited user account, and harden SSH access.
This guide is written for a non-root user. Commands that require elevated privileges are prefixed with sudo . If you are not familiar with the sudo command, see the Users and Groups guide.
How to Use the cp Command to Copy Files and Directories in Linux
The cp command works similarly on most Linux distributions. The command operates in four basic modes.
- Copies a file to the same directory. The new file must have a different name.
- Copies a file to a different directory. It is possible to rename the file or retain the old name.
- Copy multiple files to a different target directory.
- Recursively copy the contents of a directory, including subdirectories, to a different target directory.
There are a number of concerns to be aware of when using cp . For instance, cp does not display a warning when overwriting an existing file. This situation occurs when copying a file to a new directory already containing a file with the same name. This problem is more likely to happen when copying multiple files. To avoid this problem, users can use interactive mode to force Linux to request confirmation before overwriting a file.
cp is often used in conjunction with the ls command. ls lists the contents of the current directory. This is handy for confirming the exact name and location of the source files and directories.
Some of the most important cp command options include the following:
- -f : Forces a copy in all circumstances.
- -i : Runs cp in interactive mode. In this mode, Linux asks for confirmation before overwriting any existing files or directories. Without this option, Linux does not display any warnings.
- -p : Preserves the file attributes of the original file in the copy. File attributes include the date stamps for file creation and last modification, user ID, group IP, and file permissions.
- -R : Copies files recursively. All files and subdirectories in the specified source directory are copied to the destination.
- -u : Overwrites the destination file only if the source file is newer than the destination file.
- -v : Runs cp in verbose mode. This mode provides extra information on the copying process. This is useful for keeping track of progress when copying a large number of files.
The options -H , -L , and -P indicate how the cp command should process symbolic links. See the cp man page for a full description of cp and symbolic links. The options for cp vary between Linux distributions. A list for Ubuntu 22.04 LTS is available in the Ubuntu cp documentation.
How to Copy a File in Linux
One common use of cp is to make a second copy of the source file in the same directory. Supply a different name for the copy to differentiate it from the original. A common convention is to add an extra extension such as .bak or .cp to the existing file name. For example, a standard name for a backup copy of archive.txt is archive.txt.bak .
The cp command operates in the context of the current working directory. However, files can be specified using either an absolute or relative path. Here is the basic cp command to copy a file within the same directory.
How to Copy Files in Linux
File copying is one of the most common tasks for PC users. Of course, you can use a file manager, navigate to a directory with your file, and copy files using a graphical interface. It is pretty simple. But in this article I will show how to copy files in the Linux terminal.
The graphical interface may be not available due to system crashes on home PCs. Although serves do not have it at all. In addition, copying files in the terminal is more flexible and powerful. In this article, I will not only cover the most popular cp command but also other commands such as tar, rsync, and find.
How to Copy Files in Linux
1. cp
The cp command is the most commonly used command for copying files in Unix-like operating systems. It is available out of the box in all distributions and has a lot of features. You can read in detail about this command in the article Command cp in Linux. In this article, I will provide multiple basic examples of using this utility.
Copying files into another folder is the simplest case. For example, use the following command to copy a picture from the home directory into a directory named pictures:
Additionally, you can specify the name explicitly:
cp ~/pic.png ~/pictures/wallpaper.png
If you want to copy one directory into another, use the -r option:
In this case, the reports directory will be copied into the documents folder. If you want to copy the contents of the reports folder into ~/documents, use the -T option:
This command also supports special wildcard symbols * and ?. For example, the following command will copy all files whose names start with april:
cp ~/reports/april* ~/documents
If you want to preserve permissions mode and ownership, use the -p option:
cp -p ~/reports/march* ~/documents/
Finally, there is an interesting use case for creating backups of files. You can Bash brace expansion syntax to make your command shorter, for example:
This command will create a file with the same name and the .back extension.
By default, cp does not print the progress bar for file copying. It can be inconvenient when you are copying large files. However, you can use the progress command to show the progress for all commands from the CoreUtils package. Alternatively, you can use the rsync command instead of cp.
2. rsync
This command was designed to create backups for files and directories. It allows not only to copy files, but also synchronize the content of directories, and it supports transferring files through a network, for example, SSH.
To copy a picture into the ~/pictures directory use this command:
Here you can specify the file name as well as when you use cp. But this utility is more convenient because it can print information about the copying process. You can enable it using the —progress option:
rsync —progress ~/disk.img ~/images
If you want to copy the contents of one directory into another, use the -r option. For example:
rsync -r ~/reports/ ~/documents/
Note, that if you want to copy only the content of the source directory, its name should end with a slash. But if you want to copy the directory itself, just don’t add the slash:
rsync -r ~/reports ~/documents/
If you want to preserve permission mode and ownership, use the -a option:
rsync -a ~/reports ~/documents/
3. xcp
Currently, there are several alternatives to the CoreUtils utilities that are written in Rust. These alternatives are optimized for modern systems and are more user-friendly. One such alternative is the xcp command, which is an alternative to the cp command. To install xcp, use cargo:
cargo install xcp
The xcp command can copy a file from one directory to another just like the cp command, but it also features a progress bar. For example:
You can also specify a new name for the file:
xcp ~/pic.png ~/pictures/wallpaper.png
Copying directories is also straightforward. All contents of the source directory are copied into the destination directory by default:
But if you want to copy the ~/reports directory into ~/documents itself, use the -T option:
Now, you know basic commands that can help to copy files in Linux. Let’s have a look at filtering files for copying.
Copying Files in Linux Using Regular Expression
Sometimes, you may want to filter files for copying by regular expression. You can use the find command to do this. It allows to apply different conditions and regular expressions for searching files and then run a specific command for each file, such as cp, rsync and even xcp.
For example, use this command to copy all files whose names contain the march word and numbers from the ~/reports directory into documents:
find ./reports -regex ‘./[^/]*/march2*’ -exec cp <> ~/documents ;
The regex parameter is used to specify a regular expression for filtering files. Note, that the regular expression is applied to a whole file path. The exec parameter specifies a command which should be executed for each file. The <> is a placeholder that will be replaced with the name of each file that matches the regular expression, and the ; is used to terminate the -exec option. You can use other find conditions in the same way.
Special Copying of Files Using Tar Command
Linux provides multiple ways to perform the same task, and that’s what makes it interesting for many users. Copying files in Linux can be done not only by using cp or other copying tools. For instance, you can use the cat utility and Bash output redirection operators to copy the contents of a file to another file:
cat report > ~/reports/report
This command copies only the content of the file. It can be useful for small text files.
If you want to copy multiple files you can use the tar command. For example, if you want to create a system backup while preserving all permissions, ownership, and symbolic links, you can use tar with Bash pipeline where tar will archive the required files and send the result to stdout, and another tar instance will extract the files into the specified folder. Here’s an example command:
tar cf — /var | ( cd /mnt/var && tar xvf — )
This command will copy all contents from /var into /mnt/var. You can use it to copy Linux folders or a whole root of a system. The cp command can also be used for this purpose, but you need to specify the -a option to preserve permissions and links.
Wrapping Up
Now, you know how to copy files in Linux. As you can see, copying in the terminal is more flexible, and sometimes faster than in a GUI if you remember the proper commands. If you have any questions, feel free to leave them in the comments below!
Found a mistake in the text? Let me know about that. Highlight the text with the mistake and press Ctrl+Enter.