File management is a core part of any operating system. Creating copies of files is one of the most basic but important actions that exist in file management. In Linux-based OS, there exist several methods through which a copy of any file can be created to a different directory. This process is carried out to make copies of the important files that need backup.
This article will demonstrate the possible ways to achieve this task in Linux.
Method 1: Using the “cp” Command to Duplicate Files From One Folder to a Different Folder in Linux
This section will elaborate on all the different ways to copy a file to a different location in a Linux system. The “cp” command is the primary player in duplicating the files from one folder to the same or different. To explain the syntax, “cp” is the keyword used which is followed by the source path of the file that needs to be copied. Finally, enter the destination path where you want the copy of the file to be created as demonstrated below:
$ cp [source] [destination]
Example 1: Copy a Single File Using “cp”
In this example, the “cp” command is carried out to copy a single file from one location to the other.
Check out the example shown below that copies the file named “sample” from the “Desktop” directory to another directory located at “Desktop”:
$ cp /home/itslinuxfoss/Desktop/sample /home/itslinuxfoss/Desktop/Destination
The successful execution of the command states that the file has been copied successfully. Let’s navigate to the “Destination” directory to verify the copy:
As shown here, the “sample” file is copied from the directory and the duplicate is created inside the “Destination” directory.
Example 2: Copy a Single File and Duplicate it With a Separate Name
The “cp” command can also be utilized to make a copy of the file and save it with a different name. The following command makes a copy of the file named “sample” and saves it with a file named “newName”:
$ cp /home/itslinuxfoss/Desktop/sample /home/itslinuxfoss/Desktop/Destination/newName
It is observed that the “newName” file can be located in the directory where it was copied.
Example 3: Copy Multiple Files
The “cp” command enables the user to perform is to make duplicates of multiple files using just a single command. Firstly, we should go to the directory where these files are present. In our case we are moving to the “/home/itslinux/Desktop” directory as follows:
$ cd /home/itslinuxfoss/Desktop
In the next step, utilize the “cp” command along with the names of all the files that you want to copy. After the names, insert the path to the folder where you want the copies of the files:
$ cp sample1 sample2 sample3 /home/itslinuxfoss/Desktop/Destination
The copies of the files can be observed at the specified path as shown below:
Example 4: Copy All the Files
The “cp” command makes use of the “ * ” operator to select all files in a targetted directory and copies them. This operator is used to refer to all the files present in a certain directory. Check out the example shown below for how this operator is implemented:
$ cp /home/itslinuxfoss/Desktop/* /home/itslinuxfoss/Downloads/Destination
As the snippet above shows, all the files contained within the “Desktop” directory have been copied and duplicates created inside the “Destination” directory.
Method 5: Using the “rsync” Command to Duplicate any File
The “rsync” command line tool can be utilized while attempting to make copies of files in the Linux system. It usually comes pre-installed in the system, but if that is not the case, it can be installed using the following command:
$ sudo apt install rsync
Once installed, the “rsync” tool is used in the following format, which is very similar to the “cp” command as shown below:
$ rsync [source] [destination]
The “rsync” command is utilized with the source path followed by the destination path for the copied files. Let’s make use of the “rsync” tool to make a copy of the file from one directory to another:
$ rsync /home/itslinuxfoss/Desktop/sample1 /home/itslinuxfoss/Downloads/Destination
The example above shows the working of the “rsync” tool in a Linux system.
A file can be copied to a different directory in Linux using either the “cp” command or the “rsync” tool. Using these commands, single and multiple, as well as all the files inside a directory, can be copied from one directory to another. The user can also choose between keeping the same name of the duplicated file or changing it. This post has demonstrated the possible methods and examples to copy files from one directory to another in Linux.