How to Move Files and Directories in Linux
Move Files and Directories in Linux involves using the mv command to relocate or rename files and directories efficiently. This operation updates the file location without duplicating data, optimizing file system organization.
🤖AI Overview:
Moving files and directories in Linux is primarily accomplished using the mv command, which shifts items from a source to a destination path within the system. This command supports multiple file operations, renaming during the move, and options for interactive confirmations, backups, and conditional transfers. Graphical user interfaces also provide visual methods such as drag-and-drop and cut-paste functionalities for file management.
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.
Conclusion
Mastering how to move files and directories in Linux is indispensable for developers managing projects, deploying applications, or overseeing system administration.
The mv command provides robust and flexible options for moving single or multiple files and directories, renaming files during transfers, and controlling overwriting behavior with options tailored for safety and clarity.
By adhering to best practices such as cautious use of wildcards, employing interactive and verbose options, and verifying permissions, developers can efficiently maintain their Linux environments with confidence and precision.
This knowledge streamlines workflows and prevents data loss, contributing to smoother development and production operations.
FAQ
2. How can I move multiple files or directories at once in Linux using the mv command?
You can move multiple files or directories by listing all sources followed by the destination directory:
mv [source1] [source2] ... [destination]
Wildcards like *.pdf can also be used to select multiple files.
3. How do I rename a file or directory while moving it in Linux?
To rename during the move, specify the new name as the destination:
mv [oldname] [newname]
For example, "mv oldfile.txt newfile.txt" renames and moves the file.
4. How can I prevent overwriting existing files when moving files in Linux?
Use the "-i" option with "mv" to prompt before overwriting (mv -i), or use the "-n" option to skip overwriting without prompt (mv -n).
5. Is it possible to create backups automatically when moving files that already exist?
Yes, use the "-b" option with "mv" to create a backup of the existing file before overwriting. The backup file will have a tilde (~) suffix by default.
6. How can I move files only if they are newer than the destination files?
Use the "-u" option with "mv" to move files only when the source is newer than the destination or if the destination does not exist.
7. What are common GUI methods to move files and directories in Linux?
Popular GUI methods include cut and paste (Ctrl+X and Ctrl+V), using the Move To... option in file managers like Nautilus, and drag-and-drop file movement.
8. How do I move a file to its parent directory using the command line?
You can move a file to its parent directory by specifying ".." as the destination:
mv filename ..
9. What happens if I try to move multiple directories but the destination is not a directory?
The "mv" command requires the last argument to be an existing directory when moving multiple directories. Otherwise, it returns an error "target is not a directory."
10. What distinguishes the mv command from copy commands like cp or rsync when managing files in Linux?
The "mv" command relocates files and directories by changing their path without duplicating data, whereas "cp" and "rsync" create copies. Move operations generally are faster and do not increase disk usage.