How to Move a File to Another Directory in Linux?

In Linux, the basic administrative task consists of creating, removing, and moving a file commonly used by the users. Several methods are used to move files to another directory, like using the “mv” command or with the help of GUI (easy for windows users).

This article will explain all the possible methods to move a file to another directory by following this timeline:

Method 1: Move File to Another Directory Using CLI

In this section, we will discuss moving the file to another directory with the command line. Files can be moved from one directory to another using the “mv” command. Let’s check its syntax:

$ mv [options] source-file destination-directory

The components of the “mv” command are as follows:

  • mv: This represents the “move” command to transfer files.
  • options: Replace it with mv command available options.
  • source-file: Replace with the source file’s name, which will be moved.
  • destination-directory: Replace it with your destination directory name.

Example 1: Move a File to Another Directory

The “mv” command allows the user to move a file to another directory. For instance, to move the “testFile.txt” to the desired directory “~/Desktop”, execute this command:

$ mv testFile.txt ~/Desktop

The “testFile.txt” is moved to the “Desktop” directory, which can be verified using the “ls” command as shown in the above output.

If you want to check the status of every moving file, the “v” verbose option is utilized. For example, to move the “testFile2.txt” to the “Documents” directory to show its status, this command is used:

$ mv -v testFile2.txt ~/Documents

Example 2: Move Multiple Files to Another Directory

The “mv” command allows the user to move the multiple files to another directory with a single command. To move several files (for example, testFile3.txt and testFile4.txt) to another directory, use the below-mentioned command:

$ mv -v testFile3.txt testFile4.txt ~/Desktop

Both the files are moved to the destination folder “Desktop” as seen in the output.

Example 3: Move Specific Type of Files to Another Directory

The “mv” can be used to move a specific type of files to a new directory. For instance, to transfer all the “pdf” files to the “Documents” directory, the below command is utilized:

$ mv -v *.pdf ~/Documents

The output verifies the three “pdf” files moved to the “Documents” directory.

Example 4: Move the Unique Files to Another Directory

Sometimes, the file is already present in that directory, and try to move the file again, which duplicates the file in that directory. To avoid the duplication of the files, the “n” option is used, which ignores the already existing files. For instance, to move the “testFile1.txt” to the “Desktop” directory by avoiding the file if already present, the following command is used:

$ mv -n testFile1.txt ~/Desktop

The file “testFile1.txt” is not moved to another directory as the file already exists in the system.

Method 2: Move File to Another Directory Using GUI

A single or multiple files can be moved to another directory using the GUI approach. The files can be moved to another directory by following these steps:

Navigate to the desired folder and right-click on the specific file (which you want to move), then click on the “Move to…” option from the dropdown menu:

Click on the destination directory (in this case, Documents) and press the “Select” button:

The selected file is moved to the destination (Documents) directory.

Navigate to the destination directory and verify the file has been moved to that directory.

Similarly, multiple files can be moved to another directory by selecting those files, choosing the “Move to… ” option, and clicking on the “Select” button after selecting the destination directory.

Note: Following the GUI method, you can use the shortcut key “CTRL+X” to cut and “CTRL+S” to save the file at the destination.

Conclusion

In Linux, the “mv” command is used to move a file to another directory or choose the “Move to…” option after selecting the file from GUI. A single file or specific files can be moved using the “mv” command, which provides options like moving with confirmation and backup. This guide has explained all possible methods of moving a file to another directory using different options.