Copy all file command linux

How to Copy Files and Directories in Linux

Copying files and directories is one of the most common tasks you’ll perform when working on the command line. There are several commands for copying files in Linux, with cp and rsync being the most widely used tools.

It is common practice to use the cp command to copy files and rsync to copy directories.

To be able to copy files and directories, you must have at least read permissions on the source file and write permission on the destination directory.

Copying Files with the cp Command #

On Linux and Unix operating systems, the cp command is used for copying files and directories.

The most simple use case is to copy a file in the current working directory. For example, to copy a file named file.txt to a file named file_backup.txt in the current directory , you would run the following command:

cp file.txt file_backup.txt

If the destination file exists, it will be overwritten. To get a confirmation prompt before overwriting the files, use the -i option.

cp -i file.txt file_backup.txt

By default, when using the cp command to copy a file, the new file will be owned by the user performing the command. Use the -p option to preserve the file mode, ownership , and timestamps :

cp -p file.txt file_backup.txt

Another option that can be useful is -v . When using this option, the command prints what is being done:

cp -v file.txt file_backup.txt

Copy a file to a directory #

To copy a file to a directory, specify the absolute or the relative path to the directory. When the destination directory is omitted, the file is copied to the current directory.

In the following example, we are copying the file file.txt to the /backup directory:

When specifying only the directory name as a destination, the copied file will have the same name as the original file.

If you want to copy the file under a different name, you need to specify the desired file name:

cp file.txt /backup/new_file.txt

The command above will copy the file to the specified directory as new_file.txt .

Copy multiple files #

To copy multiple files and directories at once, specify the names of source files and directories followed with the destination directory as the last argument:

cp file.txt dir file1.txt file2.txt dir1

When copying multiple files, the destination must be a directory.

The cp command also allows you to use pattern matching. For example, to copy all .png files from the current directory to the /backup directory, you would use:

Copying Directories with cp Command #

To copy a directory, including all its files and subdirectories, use the -R or -r option. In the following example, we are copying the directory Pictures to Pictures_backup :

cp -R Pictures Pictures_backup

The command above will create the destination directory and recursively copy all files and subdirectories from the source to the destination directory.

Читайте также:  Arch linux update keys

If the destination directory already exists, the source directory itself and its content are copied to the destination directory. To copy only the files and subdirectories but not the target directory, use the -T option:

cp -RT Pictures Pictures_backup

The options used when copying files can also be used when copying directories. The main difference is that when copying directories, you need to use the -R option.

Copying Files and Directories with the rsync Command #

rsync is a fast and versatile command line-utility that synchronizes files and directories between two locations. It can be used to copy files to local and remote locations.

rsync includes many options that control every aspect of its behavior

The most useful option is -a that recursively copy directories, transfer special and block devices and preserve symbolic links, modification times, group, ownership, and permissions.

To copy a single file from one to another location, you would run the following command:

rsync -a file.txt file_backup.txt

If the destination file exists, rsync will overwrite it.

The same command can be used to copy a directory:

rsync -a /var/www/public_html/ /var/www/public_html_backup/

rsync threats the source directories that end with a trailing slash / differently. If you add a trailing slash on the source directory, the command will copy only the source directory’s contents to the destination directory. When the trailing slash is omitted, rsync will copy the source directory inside the destination directory. The safest option is always to include the trailing slash / on both the destination and source.

To learn more about rsync check the following articles:

Conclusion #

We have shown you how to copy files and directories in Linux and Unix-based systems, using the cp and rsync utilities.

If you have any questions, please leave a comment below.

Источник

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.

Читайте также:  Kali linux tor all traffic

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:

cp-sl-pic-png-sl-pictures-sl.png

Additionally, you can specify the name explicitly:

cp ~/pic.png ~/pictures/wallpaper.png

cp-sl-pic-png-sl-pictures-sl-wallpaper-png.png

If you want to copy one directory into another, use the -r option:

cp-r-sl-reports-sl-documents.png

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:

cp-rt-sl-reports-sl-documents.png

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

cp-sl-reports-sl-april-sl-documents.png

If you want to preserve permissions mode and ownership, use the -p option:

cp -p ~/reports/march* ~/documents/

cp-p-sl-reports-sl-march-sl-documents-sl.png

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:

sudo-cp-sl-etc-sl-passwd-bask.png

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:

rsync-sl-pic-png-sl-pictures-sl.png

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

rsync-progress-sl-disk-img-sl-images.png

If you want to copy the contents of one directory into another, use the -r option. For example:

rsync -r ~/reports/ ~/documents/

rsync-r-sl-reports-sl-sl-documents-sl.png

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/

rsync-r-sl-reports-sl-documents-sl.png

If you want to preserve permission mode and ownership, use the -a option:

rsync -a ~/reports ~/documents/

rsync-a-sl-reports-sl-documents-sl.png

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:

xcp-sl-pic-png-sl-pictures-sl.png

You can also specify a new name for the file:

xcp ~/pic.png ~/pictures/wallpaper.png

Читайте также:  Linux mint clipboard manager

xcp-sl-pic-png-sl-pictures-sl-wallpaper-png.png

Copying directories is also straightforward. All contents of the source directory are copied into the destination directory by default:

xcp-r-sl-reports-sl-documents-sl.png

But if you want to copy the ~/reports directory into ~/documents itself, use the -T option:

xcp-rt-sl-reports-sl-documents-sl.png

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 ‘./[^/]*/march8*’ -exec cp <> ~/documents ;

find-sl-reports-regex-sl-sl-sl-marc.png

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.

Источник

Оцените статью
Adblock
detector