How to Use mv in Linux
Get to know the mv command in Linux for relocating and renaming files.
The mv
command in Linux, which stands for “move,” is among the most frequently used commands within the Linux environment.
The principal use of the mv
command involves relocating files and directories from one location to another within the system’s structure.
An additional noteworthy feature of the mv
command is its ability to rename files and directories. Essentially, when you ‘move’ a file to a different filename within the same directory, you’re effectively renaming it.
Here are some ways to use the mv
command:
1. Move a File
The basic syntax for moving a file is mv source destination
.
Example:
To move a file named file.txt
from the current directory to another directory, you would use:
mv file.txt /path/to/directory/
2. Rename a File
You can rename a file by moving it to a new name in the same directory.
Example:
To rename a file named file.txt
to newfile.txt
, you would use:
mv file.txt newfile.txt
3. Move Multiple Files
You can move multiple files at once by specifying multiple source files.
Example:
To move file1.txt
, file2.txt
, and file3.txt
to a directory, you would use:
mv file1.txt file2.txt file3.txt /path/to/directory/
4. Move Directories
You can move directories in the same way as files.
Example:
To move a directory named dir
to another directory, you would use:
mv dir /path/to/directory/
5. Rename Directories
You can rename directories in the same way as files.
Example:
To rename a directory named dir
to newdir
, you would use:
mv dir newdir
6. Interactive Mode
If you want to be prompted before overwriting files, you can use the -i
(or --interactive
) option.
Example:
mv -i file.txt /path/to/directory/
7. Do Not Overwrite Existing Files
If you do not want to overwrite existing files in the destination, you can use the -n
(or --no-clobber
) option.
Example:
mv -n file.txt /path/to/directory/
Renaming files in Linux: mv
vs. rename
Both mv
and rename
allow you to rename files, but they operate in different ways and have different uses.
mv
mv
is primarily used to move files and directories from one place to another, but it can also be used to rename files and directories. It’s straightforward and easy to use. You simply type mv
, the current name of the file, and then the new name you want the file to have.
Example:
mv oldname.txt newname.txt
However, if you need to rename multiple files or need to use patterns for renaming, mv
can be quite limited and may not meet your needs and this is where rename
comes in.
rename
rename
is more sophisticated Linux command and is designed specifically for renaming files. It’s powerful because it can use Perl regular expressions to rename files in bulk.
Example:
If you want to rename all .txt files to .bak files in a directory, you could do so with one rename
command:
rename 's/\.txt$/.bak/' *.txt
This command replaces the .txt extension with .bak for all text files in the current directory.
In conclusion, mv
is simple and useful for renaming single files or moving files from one place to another, while rename
is more powerful and better for renaming multiple files at once, especially when patterns or regular expressions are involved.
More Linux commands:
Directory Operations | rmdir · cd · pwd · exa · ls |
File Operations | cat · cp · dd · less · touch · ln · rename · more · head |
File System Operations | chown · mkfs · locate |
Networking | ping · curl · wget · iptables · mtr |
Search and Text Processing | find · grep · sed · whatis · ripgrep · fd · tldr |
System Information and Management | env · history · top · who · htop · glances · lsof |
User and Session Management | screen · su · sudo · open |