19.02.2024

Copying files on Linux

Copying files is an important task when working on the Linux operating system. In it, copying files and folders can be done in different ways, and each one has its own characteristics and advantages for increasing productivity and efficiency. In this article we will list some methods for copying files and folders that will help you successfully complete this task.

cp

One commonly used method for copying files is the cp command. Its syntax is simple and clear:

cp [parameters] <source> <destination>

To copy the testfile from the current directory to the /home/testuser directory, simply run the following command:

cp testfile /home/testuser/

If you need to copy a file with a new name, simply specify the desired name in the destination argument:

cp testfile /home/testuser/newfile

List of useful command keys:

rsync

One of the significant advantages of rsync is the ability to continue copying if it is interrupted. This is especially useful when copying large files or when working over a network.

rsync [options] <source> <destination>

Installing rsync:

apt install && apt upgrade -y
apt install rsync

Example usage for copying the testfile.txt file to the /home/testuser directory:

rsync testfile.txt /home/testuser/

Using the scp command to copy files over the network

The scp command allows you to copy files between different systems over a network. Its syntax is similar to cp:

scp [parameters] <source> <destination>

An example of using the scp command to copy a file.txt file from a remote server to the local computer:

scp testuser@remote:/path/to/testfile.txt /local/path/

The scp command also allows you to copy files between remote servers:

scp testuser@remote1:/path/to/testfile.txt username@remote2:/path/to/

The scp command options allow you to configure authentication, secure connection, and other settings.

Conclusion

Copying files on Linux is one of the main tasks when working with this operating system. We looked at several methods, including the cp command for copying files locally, rsync for advanced capabilities, and scp for copying files over a network. You can choose the most suitable method depending on your needs and requirements. It should also be noted that all these commands have many options that allow you to customize the copying process in a way that suits you. For more information about these options, you can refer to the documentation available on the Linux system.