On Linux, moving a directory is a common activity that every user will have to do at some point. This can be done using any installed desktop environment or the mv command from the command line.
While this is a rather straightforward function, there are a few things to bear in mind. We’ll go over various examples of relocating a directory on Linux in this article. To learn more about the mv command and GUI process, you can look up this article.
This article will teach you how to:
How to move a directory using a Graphical User Interface (GUI)
How to move a directory using the Command Line Method
Method 1: Move a Directory Using a Graphical User Interface (GUI)
The method for moving a directory on Linux via the graphical interface varies slightly based on the distribution and desktop environment you’ve installed. The only big difference you’ll notice is that some of the menus have changed. We’ll use the GNOME desktop environment in the steps below, which is the default for many popular distributions like Ubuntu. If the directories are not created, you can create them using the mkdir command:
$ mkdir <directory name>
Select “cut” from the context menu when you right-click on the folder/directory you want to relocate. You can also select the folder/directory and cut it by pressing Ctrl + X on your computer.
Navigate to the location where you want the folder to be moved. “Paste” can be done by right-clicking in an empty space and selecting “paste.” Alternatively, go to the destination and paste the folder by pressing Ctrl + V on your keyboard.
You may also right-click a directory and choose “Move to” in various desktop environments.
Then, to finish the move, browse to the new location, highlight it, and click “select.”
It’s as simple as that. It’s important to note that this will relocate the entire folder, including subdirectories. The command-line approach will be covered next.
Method 2: Move a Directory Using the Command Line Method
On Linux, the mv command is used to relocate directories (and files). Simply specifying a source and destination location in your command is the simplest version of the command. To access directories, you can use absolute or relative paths.
$ mv <directory1> <directory2>
You can see that the above command will move the contents of /d1 to /d2.
But wait, if /d2 doesn’t exist, then /d1 would just be renamed to /dir2 in such a scenario. In other words, when we use the mv command, we can give our relocated directory a new name. Simple relocation of the directory into an existing place will avoid having to supply a new name.
You can also get more information about the relocation by using the -v (verbose) option. It’s worth noting that mv states it’s renaming the directory. Because paths are simply connections to files and folders on the hard disc, this is essentially the same as “moving” them.
$ mv -v dir1 dir2
You can also transfer multiple directories at once. The destination directory for the rest of your command will be the final directory in your command.
$ mv d1 d2 d3
By running this command, you can see that both d1 and d2 will be relocated to d3 in this case.
In this article, we learned how to move directories on a Linux system using the GUI and the command line. This is a standard task that all users should be able to complete. The command-line method, as expected, gives us a little more control over the operation, but both methods are equally effective. You can choose the one among these two that is most convenient for you.