How to Move Files and Directories in Linux?

Moving files and directories can help you keep your file system organized by placing related files and directories in the same location. This will also free up space in the original file system, which can be useful if you run low on storage space.

This is one of the most important aspects that every Linux user should be aware of, and that’s why we will teach you how to move files and directories in Linux.

Let’s start with the mv command.

How to Use the mv Command in Linux

The first thing that a user should do while learning about any command is to understand its basic syntax you can do this by typing:

$ mv --help

This is a generic command, so its syntax will remain the same for all different Linux distributions such as Ubuntu, Fedora, or CentOS.

In this section, we will guide you on moving a single file or directory to a different location.

How to Move a Single File and Directory to Another Directory (CLI)?

The first thing you need to understand is that you should know the complete path where you want to move any file that you can do by typing:

$ pwd

You can see that after running the command, it shows the path where we are currently present. So, the next thing is learning how to move files in any directory. For that, we will be creating a few files for better understanding:

$ touch file1.txt file2.txt

To show the list of all the directories available on the home directory along with these two newly created text files; you need to run the following command:

$ ls

Suppose you want to move the ‘file1.txt’ in the ‘Documents’ directory then as stated earlier you need to know the complete path of any directory where you want to move the file. So, in this case, the complete path of the document’s directory would ‘/home/itslinux/Documents’ would be:

$ mv file1.txt /home/itslinux/Documents

You will get the same results in a simpler way as well by typing the below command:

$ mv file1.txt ~/Documents

 Where ‘~/’ before the documents shows that you are currently in a home directory.

Now when you run the ‘ls’ command, you will notice that the ‘file1.txt’ is no longer available in the home directory and has moved to the document’s directory:

$ ls /home/itslinux/Documents
$ ls ~/Dcouments

Pro Tip: Suppose you want to move a file in any directory, and you are not aware of its path so another way of doing this is to open that directory, right-click on it and then select ‘open in terminal’

When you do this then it will directly open the terminal by accessing that directory path and all you need to do is to run the pwd command again as shown:

$ pwd

So, the path mentioned above can be used to move any file in the documents directory

Moving a Directory: The same thing could be applied to directories as well with the similar syntax as shown:

$ mv testdirectory ~/Documents

How to Move Multiple Files and Directories to a Different Directory (CLI)?

If you want to move multiple files in the directory, you need to type the name of all those files you want to move, followed by the destination directory.

$ mv file1.txt file2.txt /home/itslinux/Documents
$ mv file1.txt file2.txt ~/Documents

The same thing can be applied while moving multiple directories to another directory as well just by typing:

$ mv Dir1 Dir2 /home/itslinux/Documents
$ mv Dir1 Dir2 ~/Documents

How to Move All Files with the Same Format in the Directory?

Suppose if you want to move all the files into the document’s directory with ‘.txt’ format so you can do that by typing:

$ mv *.txt ~/Documents

Similarly, if you want to move multiple files having different formats, then you can use the same above command while adding those formats as well:

$ mv *.txt *.pdf ~/Documents

 How to Move Files Without Overwriting?

One of the drawbacks of the move command is that it will overwrite the file if it has the same name. So, to avoid that, you can use the ‘-i’ flag, which will prompt you whether you want to overwrite the file or not by typing:

$ mv -i file1.txt ~/Documents

The similar thing could be implemented for directories as well by following the same syntax as shown:

$ mv -i file1.txt ~/Documents

How to Move Files and Directories via Graphical User Interface (GUI)?

You can also move files and directories via GUI, and for that, you need to select the specific files or directories or their combination, right-click on them, and select the ‘move to’ option as shown.

This will open a new dialogue window where you must select your desired folder and click on the ‘select’ icon on the top right, as shown.

This will automatically move your selected files and folder to the desired folder. That’s how you can move files and directories in Linux.

Conclusion

Moving files and directories can help you organize your data, free up space, avoid duplication and make a backup. You can move files and directories in Linux either by using the ‘mv’ command using a terminal or by following the GUI method mentioned in this article.