How to Move Files and Directories in Linux?
Moving files and directories in Linux is an important task that allows you to reorganize your file structure, create backups, free up space, share files with others, and access files from different locations. It is a useful and necessary task to help you manage your data and improve productivity. This post will instruct you on how to move files and directories in Linux. The practical implementation of the steps is carried out on Linux Mint 21.
How to Move Files and Directories in Linux Using CLI?The mv command is used to move files and directories in Linux. The basic syntax for the mv command is as follows:
Syntax of the mv Command
While the ‘source’ and ‘destination’ represents the current location and the location to which the file will be moved.
$ mv [options] source destination
Examples of Moving Files in LinuxThe following examples demonstrate how files can be moved in Linux.
Example 1: Moving a File to a Different DirectoryThe simple move is shown in the following command, where the file named “genie1.txt” will be moved to the directory “/home/linuxgenie/data”.
$ mv genie1.txt /home/linuxgenie/data
Example 2: Moving File With a Space in the NameNow, if there is a space in the file name, it can be moved by putting the file name in the double quotations as practiced in the following command.
$ mv "genie file.txt" ~/data
Example 3: Move a File Having Special Character in Its NameIf there is a character in the file name, you need to put the backslash before the character. The file named “[email protected]” will be moved using “genie\@”, as seen below.
$ mv "genie\@.txt" ~/data
Example 4: Moving Multiple FilesThe multiple files can be moved using the file names in a sequence and then putting the destination directory’s address. The following command moves 3 files to the target directory.
$ mv genie1.txt genie2.txt genie3.txt ~/data
Example 5: Move and Rename File SimultaneouslyThe files can be moved and placed with a different name. As an example, the command written below will move the file named genie.txt and will move it to a different directory and will name it “GenieTest.txt”.
$ mv genie.txt GenieTest.txt ~/data
Example 6: Move Files of Specific CategoryUsing the wild card character, the file’s category can be mentioned; thus, only selected files will be moved to the destination directory. The wild card can be mentioned with the extension name, and that file will be moved only. An example of such a scenario is seen below, where all the files with a .txt extension will be moved to the destination directory.
$ mv *.txt ~/data
Examples of Moving Directories in LinuxNow, let’s practice the examples of moving directories in Linux.
Example 1: Moving a Directory to a Different DirectoryThe command below will move the directory named linuxgenie to the destination directory /home/linuxgenie/data.
$ mv linuxgenie ~/data
Example 2: Moving Multiple DirectoriesTo move multiple directories with all their content, use the recursive flag with all the directory’s names as follows.
The recursive move feature will move the sub-directories and all the files inside those directories.
$ mv genie1 genie2 genie3 ~/data
Example 3: Move All Directories/Files From the Current DirectoryYou can use the wild card character (*) to move everything from the current directory to the destination directory, as demonstrated in the command written below.
$ mv * ~/data
Example 4: Files/Directories Starting or Ending With a Specific Character/WordThe wild card is the primary catalyst for selecting multiple files/directories. For instance, the command provided below will select and move all the file directories started with the name gen.
$ mv gen* ~/data