How to Copy and Rename Files in Linux?

Copying and renaming files are generally part of the daily routine of Linux users. This task can be performed either using the Command Line Interface or the user-friendly Graphical User Interface. Both methods are easy and effective to utilize. This post explains the process of copying and renaming files in Linux.

The outline of this post is listed below

Let’s get started with the copy process.

How to Copy Files in Linux Using Terminal?

The “cp” command is the key ingredient to copy files in Linux using the terminal. Its working depends on the syntax provided below:

Syntax:

$ cp [source] [destination]

The above syntax contains the main keyword “cp”, and the square brackets show the “source” files that need to be copied to the specified “destination”.

The “cp” command also provides a list of supported options that can be obtained by executing the below “help” command:

$ cp --help

Scroll down the page to see more options.

Example 1: Copying Files

To copy files, the “cp” command can be used with specified files and the target directory in the terminal. In this example, the “test2” directory contains three files, as shown in the screenshot:

$ ls -l

Type the “cp” command with the files’ names and the target directory “Sample” as written below:

$ cp FileA.txt FileB.txt FileC.txt ~/Sample

All the files of the “test2” directory have been copied to the “Sample” directory. Run the below “ls -l” command to verify the results:

$ ls -l Sample

Example 2: Create Backup Before Copy Files (Recommended)

The “cp” command offers the “-b” flag to create the backup of files that are going to be copied. Run the below “cp” command with the “-b” argument to create the backup of “F11.txt” and the “F12.txt” files placed in the “New” directory:

$ cp -b F11.txt F12.txt

Run the “ls -l” command to check the created backup files:

$ ls -l

The “tilda (~)” after “F11.txt” and “F12.txt” identifies the backup files.

Example 3: Ask for Confirmation to Copied Files

The “-i” argument of the “cp” command allows the user to ask for confirmation before copying files into a directory. It will overwrite the target file if it is already available in the directory.

Execute the respective command in the console to allow the “cp” command to copy the mentioned files:

$ cp -i NewFile.txt Heloo.txt

 Type “y(yes)” to overwrite the “Heloo.txt ”. Hence, the “Heloo.txt” has been overwritten by the “NewFile.txt”.

Method 2: How to Copy Files Linux Using GUI?

Apart from Command Line Interface, the “GUI” Graphical User Interface is the more convenient way for copying the files from the source to the target place. Follow the step-by-step procedure that is specified below to copy files in Linux:

Step 1: Select the Files

Select all the desired files by using the mouse or keyboard. Right-click on any of the selected files. It will generate a menu list. Press on the “copy” option from the list. In our case, the desired files are placed in the “notes” directory.

Tip: The user can also use the shortcut key (Ctrl+C) to copy the selected files.

Step 2: Copy the Files at the Target Place

Open the destination directory and right-click with the mouse inside it. It generates a drop-down list. Navigate the mouse pointer to the “paste” option from the list:

Click the “paste” option, and in the result, the selected files from the “notes” directory will be moved into the “Pictures” directory:

All the selected files have been copied from the “notes” to the “Pictures” directory.

How to Rename Files in Linux Using Terminal?

Linux offers the  “mv” command that is useful for moving files from the source place to the destination place. Besides this, the “mv” command is also beneficial for renaming the files in Linux. It changes the current file name and replaces it with the new specified filename. The working of the “mv” command depends upon its syntax.

Syntax to Rename Files

The general syntax of the “mv” command to copy files in Linux is written below:

$ mv [old_filename] [new_filename]

In the syntax, the “mv” denotes the “move” command, and the square brackets show the “old” filename that needs to be replaced by the “new” filename.

Like the “cp” command, the “mv” command also offers numerous arguments. These arguments can be availed by running its “help” command:

$ mv --help

Rename File Using mv Command

The “mv” command helps the users in renaming. For example, the “ExtraDir” directory contains the file “FirstFile.txt” as shown in the screenshot:

$ ls -l

Now execute the “mv” command to rename the “FirstFile” by the “NewFile”:

$ mv FirstFile.txt NewFile.txt

The “ls -l” command confirms that the “FirstFile” has been renamed to “NewFile”.

Note: While renaming the files, one thing must be kept in mind, which is “the new name of the file should be unique”. If the file with the new name is already present, then the destination file will be overwritten by the source file instead of renaming it.

Method 2: How to Rename Files in Linux Using GUI?

Same to copying files, the user can also use the Graphical User Interface to rename the files in Linux. This method carried out some essential steps to do this task quickly and easily:

Step 1: Choose the Files

Choose the required files and right-click on them as it will pop up a menu having a number of options. Move to the “Rename” option in the list. In this example, the selected files are shown below:

Step 2: Rename the Files

A new window will open after pressing the “Rename” option from the list:

Modify the existing name of the Files into the highlighted text field. Hit the “+Add” button and add select the number series. This is because the series file must have a unique name.

After that, the first series “1,2,3,4” is selected. Now press the “Rename” button to save the new name of all files:

It is verified in the below screenshot that all the selected files have been renamed.

That’s all from copying and renaming files.

Conclusion

To copy and rename files in Linux, use the “cp(copy)” and the “mv(move)” commands. The main objective of the “cp” command is to copy the file from the source to the destination place while the “mv” command renames the current file name to the new filename. Furthermore, the GUI method is also provided in this guide to perform these tasks. This post has briefly explained both CLI and GUI methods to copy and rename files in Linux.