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
1. Move a File
The basic syntax for moving a file is
mv source destination.
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.
To rename a file named
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.
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.
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.
To rename a directory named
newdir, you would use:
mv dir newdir
6. Interactive Mode
If you want to be prompted before overwriting files, you can use the
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
mv -n file.txt /path/to/directory/
Renaming files in Linux:
rename allow you to rename files, but they operate in different ways and have different uses.
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.
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 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.
If you want to rename all .txt files to .bak files in a directory, you could do so with one
rename 's/\.txt$/.bak/' *.txt
This command replaces the .txt extension with .bak for all text files in the current directory.
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:
|File System Operations||
|Search and Text Processing||
|System Information and Management||
|User and Session Management||