If you have ever worked on a computer, you know one of the essential things of computing is moving a file from one location to the other. Before streaming services became too familiar, people used to download movies, songs and other items on their computers and then move them to their external devices such as USB and DVD drives. 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 the different command-line texts, which will help you copy the files and directories from one location to the other in Linux. Along with this, we will show you how different commands work when you click enter on them and what processing happens behind the doors 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 today, we are focusing the premises of this article on finding the different methods of copying the files and directories in Linux OS which is one of the best operating system.
In truth, copying files in Linux works exactly like how you copy files in your Windows OS. The only difference in Linux, you can easily use commands to perform the same task, and without even opening the file or the folder, you can copy it from its previous location to the new one. 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 Proceed Further
As a Linux user, you need to keep in mind that all the commands only work in the command line, which is the terminal of the Linux operating system. 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 other method to launch the terminal is by pressing CTRL+ALT+F2 or CTRL+ALT+T together.
What Is The Difference Between The 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 directories in the file stores the data in them. A file could be of any type, and 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 folders, which are also referred to as directories, are used to keep your files organized in your storage.
A directory store 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 to gigabytes. Lastly, directories are present in a single format, you can change their name, but the format will remain the same.
Copying Files Using cp Command In Linux
We are starting with the main command for copying files in Linux, which 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 create a copy of my_testfile.txt and rename 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 to make the copy in the same directory. You can use a method that you like to highlight the difference in the name of the files.
For example, you can add mytestfile_new.text to the new file 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 will receive a warning message when you are changing the file or overwriting it, so be careful about the changes you make and for a better understanding of the cp command, know about the different options 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 and copy and store the file to another location.
- -v verbose: This option can be used when you are copying multiple files of considerable 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 precisely 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 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 new only, then the file mentioned in the command will be copied.
Copying File To Another Directory Using 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 file’s name as there is no other file with the same name that is present in the target directory. Moreover, if the file is located in some other folder or path, 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, you can even create a new directory with the cp command in which you are copying the file. The cp command will make a new directory if the directory with the same 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 cp Command
The cp command allows users to copy multiple files in a single command. To do this, you need to specify the name of the source files and their destination directories which will be written at the very 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 which are present in the 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.
Also check out – 10 Best Linux Distros for Gaming
Copying Directories Using 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 coping 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 screenshot_directory in the root folder of the operating system. After that, it will copy all the files and the subdirectories present in the screenshot_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 contents of the source directory but won’t copy the target directory. You can use the same option with directories, but with the -T option, you need to use the -R option to copy directories.cp -RT screenshots_directory screenshot_backup
Using 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; what it does is 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 areas. There are tons of options that can help a user to control the behaviour 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 to help us copy the files recursively, block devices, and preserve all the symbolic links present in the file, modification times, group ownership, and other permissions. If 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 must be cautious when using rsync because the command threatens the source directories, ending 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 whole source directory as an entire to the new destination directory. Thus, you will end up with a source directory folder in the destination. As a result, it is recommended to use trailing slash / at the end of both source directory and destination directory when using the rsync command to copy the directories.
These were the methods to copy files and folders/directories from one source to another in Linux. If we are honest, you should stick with the cp command for copying both files and directories. The rsync command can be used for copying files too, but it was not intended to perform copying in the first place. But it’s up to you to make your decision, choose whichever command suits your need and make it easier for you to copy files.