Move Files and Directories in Linux
As a Linux administrator or even an inexperienced user, you need to know how to move files in Linux to manage them well. In this article, you will learn two different methods to move files and directories in Linux. Regardless of using Terminal or GUI, this content would be useful for you. Moving files and directories in Linux is not complex or difficult.
Just follow below practical examples to do your Linux tasks better. In the end, you can move your target files using GUI and on the command line in Linux.
Prerequisites to Move Files and Directories in Linux
To let this tutorial works correctly, provide the options below and move on.
- Buy Linux VPS to provide a system running Linux distribution.
- A non-root user with
sudo
privileges. - Access to the terminal window/command line
Move Files and Directories Using mv Command [9 Techniques]
In everyday Linux tasks, you will copy, paste, and cut files. Previously, you learned how to search files & directories using find command. Stay with us on this guide to be an expert on all moving operations in the Linux terminal using mv command. As a Linux system tool, the mv
command allows you to move files and directories from one location to another. Using the mv command, allows you to move files on Linux BSD, Illumos, Solaris, and macOS.
Its basic syntax is as follows:
mv [options] [source location] [destination place]
Let’s go through this part and review the most common and easy examples of using mv command to move files and directories in Linux. /Home is assumed as the destination directory in this guide.
Before starting, it’s better to know that you can create directory in linux using mkdir command.
1. Move a file from one Directory to another
To move a file from your current directory to the target directory, run:
mv sample /Home
In this way, you can move the ‘’sample’’ file to the ‘’home’’ directory.
Also, you can run the following command to relocate a file from one specific directory to another specific directory.
mv sample1 sample2
Relocating directories and relocating files both use the same syntax. If the dir2 directory is present like, in the example below, the command will transfer dir1 within dir2. dir1 will be renamed to dir2 if dir2 doesn’t already exist:
mv dir1 dir2
2. Move Multiple Files and Directories
Using the mv command to move files and directories in Linux, also allows you to move multiple directories. The basic syntax is as follows:
mv [source 1] [source 2] … [source n] [destination]
So, when you need to move the Sample 1, Sample2, and Sample3 directories to the home directory, you will run:
mv Sample1 Sample2 Sample3 home
Since the mv command enables you to use pattern matching, you can run something like the below to move all pdf files from your current directory to the ~/Home directory:
mv *.pdf ~/Home
To rename files and directories, the mv command is still used. Run:
mv [old filename] [new filename]
In this way, you can rename the Sample file into Opera by running the following command:
mv Sample Opera
3. Move a File to its parent directory
You can use the command below (as the basic syntax) to move a file from your current directory to its parent directory when you are currently in a directory.
mv file ..
Instead of specifying the complete path to the parent directory, you must substitute ..
.
4. Move Directories
Using the mv command helps you to move directories. To do this, run:
mv dir target_directory
The same method can be used to move multiple directories. Note that the last argument must be a directory to not face ‘’ target is not a directory error’’.
5. Move a file Without Overwriting
By default, the destination file will be overwritten with the source file when relocating a file in a directory where a file with the same name already exists. As you see below, to prompt for confirmation, use the -i
option:
mv -i sample1 /Home
Overwriting is confirmed or rejected by typing Y or N and hitting Enter, respectively.
6. Create a Backup while Moving a File
When you Move Files and Directories in Linux, you might need to make a backup. The mv command enables you to make a backup of a file you are moving if it already exists. -b
option helps you to create a backup of the existing file.
mv -b sample.txt Home
In this way, you can use the mv command to create a backup of the existing sample.txt file in the Home directory before overwriting. With a tilde (~) appended, the backup file has the same name as the original file.
In the previous articles, you got familiar with the ls command. Here, you can use this command to verify whether the backup was created or not.
ls Sample
Another backup option in Linux is --backup
which accepts an argument specifying the name of the backup file. By the combination of the --backup
and -S
options, you can change the default suffix for backup files (tilde). So, the new suffix is passed as an argument after the -S option and before specifying the source and destination files or directories:
mv --backup -S 01 sample.txt Home
7. Move Multiple Files but Not Overwrite Files
If multiple files already exist, you can move them and not overwrite them. If a file sample.txt already exists, the command below will do nothing:
mv -n ~/sample.txt ~/Home
So, if multiple files already exist, the command ignores those files and moves the ones that do not exist in the destination directory.
8. Move and Rename a File Simultaneously
Move Files and Directories in Linux is also possible at the same time. A file can also be renamed using the mv command. To use it, you only need to input the individual filename rather than a directory as the destination.
Let’s move a file named opera.pdf.txt from ~/Documents to ~/Home and rename it to pdf.txt:
mv ~/ Documents / opera.pdf.txt ~/Home/pdf.txt
Also, you can rename a file in your current directory. It is useful for typo occasions.
mv opers.txt opera.txt
9. Move a File only if it’s Newer than the Destination File
The mv command has a few unique options. Updating with the -u option is one of them. This will only overwrite the destination file if the one being moved is more recent than it.
Let’s assume that there is a file named opera.txt in ~/Documents which was created today, and a file named opera.txt in ~/Home which was created yesterday. So, run:
mv -u ~/Documents/opera.txt ~/Home
In this way, the command will move and replace the older file. Obviously, the above command would do nothing if the file in ~/Home was newer than the file in ~/Documents. In the event that there isn’t a file named opera.txt in /Home, this command will move the file.
mv Command Options
Let’s see what options the mv command use.
-b | Works like --backup but does not accept arguments. |
--backup | Creates a backup of the destination file. Allows you to pass additional arguments to change how the command behaves. |
-f | Automatically overwrite destination files. |
-i | Enables interactive mode, with a prompt asking for input before overwriting files. |
-n | Disables overwriting destination files. |
-S | Provide a new suffix for the backup files. |
--strip-trailing-slashes | Removes trailing slashes ("/") from source file or directory names. |
-t | Move all provided sources into a destination directory. |
-T | Treat the provided destination as a file instead of a directory. |
-u | Update destination files. |
-v | Show command output in verbose format. |
-Z | Set the destination file security context to default. |
To get more information, you can run:
man mv
Or, have a look at the man page of the mv command.
Move Files and Directories Using GUI [3 Methods]
So far, you learned the first method to move files and directories in Linux which was using mv command. In this part, you will learn to move files and directories using GUI.
As a new Linux user, you might prefer the GUI (Graphical User Interface) file manager to move your files and directories, not the command line. Since the GNOME desktop environment is the default for many popular Linux distributions, we use it in this tutorial. If the directories are not created, run the command below to create them:
$ mkdir <directory name>
There are some different ways to move files and directories in the Linux GUI. Let’s review some of them.
Method 1. Move via Copy and Paste
In this method, you need to press Ctrl+X after choosing the directory you want to relocate first. The directory can also be selected by right-clicking it and choosing Cut from the drop-down menu.
Secondly, to relocate the directory, navigate to the new location, press Ctrl+V, or right-click an empty area and choose Paste from the menu that appears.
Method 2. Move Files via Move To… option
Nautilus is typically the default file manager for Linux distributions that use GNOME as their desktop environment. Users of Nautilus can relocate their files and folders by right-clicking and choosing a new location.
To use this method, after navigating to the source directory, choose your considered file to move and select Move to… from the drop-down menu by right-clicking the directory.
Next, navigate to the destination in the Select Move Destination window, then click Select to move the directory.
In this way, the target directory will receive the file automatically.
Method 3. Move Files via Drag and Drop
One of the most intuitive acts on the desktop is undoubtedly moving a file rather than copying it when dragging and dropping it from one window to another in the GNOME desktop. Using this method, you can move a file via drag and drop from the source directory to the target directory.
So, launch the file manager first. Then, open both target and source directories. While holding down the left mouse button, select the moving file, and drag it to the target directory. Finally, release, or drop the considered file to see that it is successfully moved.
You’re all done. At this point, you are an expert in moving files and directories in Linux using Terminal and Graphical User Interface.
FAQ
What if we copy a file to a directory while there is a file with the same name on it
The destination file with the one you are moving will be replaced by the mv command.
How to rename multiple files using mv command
It is not possible to rename multiple files directly with mv command. Other commands like ‘’find’’ must be used.
What will happen if we move multiple files at once?
The destination will be treated by mv as the last directory specified.
Conclusion
In this article, you learned how to move files and directories in Linux using mv command and GUI. Now, you can use mv command as the default program more intelligently than ever before now that you are aware of your possibilities. As to be expected, the command-line approach provides us with a little bit more control over the process, but both approaches work just as well. You can pick whichever of these two suits your needs the most.
There are other methods of moving data than the mv command. Share with us your experience of using any other solutions to move files and directories in Linux.