If you have ever worked on a computer, you know one of the essential things of computing is the ability to move a file from one location to the other. Nowadays online media streaming is quite popular, but earlier people used to download movies, songs, and other items on their computers. Once downloaded, the media files were moved to external devices such as USB or DVD drives to save space on the computer. Moving files in the operating system is one of the most basic tasks that everyone needs to know.
So today, you are going to learn about different commands to copy files and directories from one location to another in Linux. Also, you will learn how different commands work, and what backend processing happens in the OS.
Is Copying Files in Linux Different from Windows?
There are multiple ways of copying the file from one folder to the other in each operating system. But here in this article, we are focusing on finding the methods of copying the files and directories in Linux OS.
The process of copying files in Linux is similar to that in Windows OS. The only difference in Linux is that you can enter the commands to perform the task without even opening the file or the folder that you want to move. In addition to this, with simple commands in Linux, you will be able to create system-wide backups or filter out the specific files that you want to copy.
Things to Know Before Proceeding Further
As a Linux user, you need to keep in mind that all the commands only work in the command line i.e. the terminal of the Linux OS. There are two methods of launching the terminal in Linux. First, you can use the graphical interface to launch the terminal. It will be located on your main screen just like an application icon.
The second method is by pressing either ‘Ctrl’ + ‘Alt’ + ‘F2’ keys or ‘Ctrl’ + ‘Alt’ + ‘T’ keys together.
Difference Between Files and Directories
All the data that you store in your computer is present in two forms, that is, file and folder. The main difference between the file and the directory is that the file stores data in them whereas the directory (folder) is a collection of files stored as a group. A file could be of any type. It could be a text file, an mp3 file, or mp4 depending on the format in which you have saved it. On the other hand, the directories (folders) are used to keep your files organized in your system.
A directory stores files in itself, and without the presence of files, it virtually occupies no space. In comparison to this, a single file depending on its content can range from kilobytes (KB) to gigabytes (GB). Lastly, directories are present in a single format, you can change their name, but the format will remain the same.
Copying Files Using the cp Command in Linux
The main command for copying files in Linux is the cp command. This command can be used for copying both files and directories from one folder to the other. The format of the cp command is:
cp [additional_option] source_file target_file
Now let’s take an example of the cp command and explain it.
cp my_testfile.txt my_testfile2.txt
In the above command, we are creating a copy of my_testfile.txt and also renaming it with the new file name my_testfile2.txt.
One thing you need to keep in mind when performing the copying task using cp is that when you are using cp, it by default runs in the same directory which is currently opened. But there can’t be two files of the same name and format in a single directory. Thus, you need to change the name of the target file of which you are making the copy in the same directory. To highlight the difference in the name of files, you can use any method that you like. For example, you can add mytestfile_new.text to the new file which you are creating using the cp command. In the above example, we have denoted the change in the file name by adding numbers such as my_testfile2.txt.
Note: You are going to receive a warning message when you are changing the file or overwriting it, so be careful about the changes you make. For a better understanding of the cp command, know about the different options which you can use with it. To find the options for any command in Linux, you need to type in -i after writing the command in the command line.
Additional Options for cp Command
The options given below can be used with the cp command to perform various tasks along with copying and storing the file to another location.
- -v verbose: This option can be used when you are copying multiple files of large size. With the help of this command, you will be able to see the progress of each file
- -p preserve: With this command, you are telling the cp command to keep the attributes of the copied file the same. The attributes include creation date and other file permissions such as group ID, file flags, file mode, access control lists, and even the extended attributes.
- -f force: When you are using this command, the file which was present in the first place will get deleted, and a new file will be created. First, the older version of the file will be copied to the latest version, and the older file will be deleted once the new file is made.
- -i interactive: This command will provide the confirmation boxes when you are making any changes to the file and its permission when copying it to another directory. This command is highly advised when you are changing the location of important files.
- –R recursive: With this command, all the files and the sub-directories of the current directory will be copied to the new location which you have provided in the command.
- -u update: when this option is used with the cp command, it will force the command to check whether the source is newer than the written destination of the command. If the source is newer only, then the file mentioned in the command will be copied.
Copying File to Another Directory Using the cp Command in Linux
To copy the file from the directory in which you are currently present to the other directory location, you need to use the following command:
cp my_test_file.txt /directory_new
When you are copying the file to a new directory, you don’t have to change the name of the file as there is no other file with the same name present in the target directory. Moreover, if the file is located in some other folder or path, then you need to provide the path of the source file from where the command will copy it to the newer location. Use the following command format to give the location of the source file:
cp /etc/my_testfile.txt /directory_new
On the other hand, with the cp command, you can even create a new directory in which you are copying the file. The cp command will make a new directory only if the directory with the same name does not exist in the same folder.
Also, to give a new name to the file which you are copying to a different path, you need to use the following command:
cp my_testfile.txt /directory_new/my_testfile2.txt
Copying Multiple Files Using the cp Command
The cp command allows users to copy multiple files with a single command. To do this, you need to specify the name of the source files along with their destination directories written at the end of the command.
cp my_testfile.txt testfile2.txt testfile3.txt directory_new
When you use the cp command, the destination you type has to be a directory. One of the great things about using the cp command is that it allows users to copy the files using pattern matching.
Let’s take an example to show what pattern matching means. Suppose you want to copy all the screenshots present in one directory. But in the same directory, there are both text and pdf files present as well. Surely, their names are different, but it is hard for you to write down all the names of the screenshot files as they are presented in numeric format. In this case, you can use the following command:
cp *.png /backup
This command will match all the files present in the current directory, which has .png in their name and copy it to the /backup directory.
Copying Directories Using the cp Command
If you want to copy a directory from one location to another and include all the files and sub-directories present in the source directory. Then you need to use the -R or -r option of the cp command. Let’s take an example to show you the use of these two options in moving the directory in Linux. In our example, we are copying the screenshots_directory to screenshot_backup using the following command:
cp -R screenshots_directory screenshot_backup
The above command will perform two tasks. First, it will locate the screenshot_backup directory. If it doesn’t find the directory with the same name, the command will create a new directory with the name screenshots_directory in the root folder of the operating system. After that, it will copy all the files and the subdirectories present in the screenshots_directory to screenshot_backup.
Furthermore, you can use the -T option of the cp command to copy the files to another directory that is already created in the first place. This command will copy all the content of the source directory but won’t copy the target directory. You can use the same option with directories as well, but with the -T option, you need to use the -R option to copy directories.
cp -RT screenshots_directory screenshot_backup
Using the rsync Command to Copy Files and Directories in Linux
The resync command-line utility does allow users to copy the files and directories from one path to the other. But the command doesn’t copy the file, it synchronizes the source file that you want to copy between the two locations. It can be used to copy the files to both local and remote locations. There are tons of options that can help a user to control the behavior of rsync commands.
To copy the single file from the source directory to the new location, you need to use the following command:
rsync -a testfile1.txt testfile2.txt
We use -a option with rsync as it will help us in copying the files recursively, block devices, and preserve all the symbolic links which are present in the file such as modification times, group ownership, and other permissions. In case the files are already present in the destination directory, then the rsync command will overwrite it.
Now let’s look at the rsync command, which is used to copy the directories. But, a user has to be cautious when they are using rsync because the command threatens the source directories, which ends with a trailing slash /. What it means is, when you are adding a trailing slash on the source directory in the command, the rsync command will only make a copy of the source directory’s contents in the destination directory.
rsync -a /var/private_docfolder/ /var/www/private_docfolder_backup/
But if you take out the trailing slash (/) from the command, then the rsync command will copy the complete source directory to the new destination directory. Thus, you will end up with a source directory folder in the destination. Therefore, it is recommended to use the trailing slash (/) at the end of both the source directory and destination directory when using the rsync command to copy the directories.
Conclusion
So these were the methods to copy files and folders/directories from one source to another in a Linux system. We would recommend you to stick with the cp command for copying both files and directories. The rsync command can also be used for copying files, but we won’t recommend it as this command was not intended to perform copying. However, it’s up to you to make your decision, choose whichever command fulfills your requirements and copy your files in a hassle-free manner.