In this blog post, we look at another everyday Linux command line tool, the mv command. The mv command in Linux is used for moving files and directories from one location to another. It is part of GNU coreutils so not only will you find it on Linux, but you will also find it on other Unix-based systems. In this post, we will touch on command syntax, flags, and examples.
⚠ Note: Be aware that when manipulating data, one mistake is all it takes. In the case of the mv command, unless you overwrite existing files or directories, typically the worst mistake that can be made is that you end up with files renamed or moved to the wrong place. That said when working with critical data it’s recommended to take precautions and make sure you have a backup.
Syntax
Typically your command is going to follow this basic syntax.
mv [OPTIONS] SOURCE DIRECTORY
There are exceptions to this but in general, they will follow this layout.
Common Flags
- -i: Interactive mode
- -f: Force move
- -v: Verbose output
- -n: No overwrite
- -b: Backup before overwrite
- -u: Move only when the source is newer than the destination
- –exchange: exchange source and destination
These are not all the flags but the ones I think you will find most useful, for a full list refer to the man page.
Practical Examples
Below are some practical examples of commands and different ways to use the mv command in Linux.
Rename A File
mv old_filename.txt new_filename.txt
Explanation: This command renames the file name with the source file name of old_filename.txt to the destination file of new_filename.txt.
Move A File
mv file.txt /path/to/destination/file.txt
Explanation: This command moves file.txt from its current location to the specified path, renaming it in the process.
Move More Than One File To The Same Path
mv file1.txt file2.txt file3.txt target_directory
Explanation: This command allows you to move multiple files (file1.txt, file2.txt, and file3.txt) to a single destination directory in one command.
Rename file Or A Directory
mv old_directory new_directory
Explanation: Similar to renaming, this command renames/moves one entire directory, from old_directory to new_directory.
Move Preserving Filename
mv file.txt /path/to/destination/
Explanation: When moving an existing file without specifying a new filename in the destination, the original filename is preserved. In this case, file.txt retains its name after being transferred to the new directory.
Move All Files Ending In A Specific Extension
mv *.jpg /path/to/destination/
Explanation: This command lets you move all existing files ending with .jpg from the current directory to the specified target directory. The * is a wildcard that matches any filename before the .jpg extension.
Conclusion
No matter if you want to move a target file to a different location, move a directory, rename files, or rename directories look no further than the the Linux mv command command line tool. The mv command is used for moving files and directories from one location to another. We covered command syntax, flags, and examples.
Looking For More
Done reading and looking for more, why not check these out?
Embracing a lifelong passion for technology since childhood, CJ delved into the intricate workings of systems, captivated by the desire to understand the unknown. This innate curiosity led to his discovery of Linux, a revelation that resonated deeply. With more than 7 years of on the job experience, he’s honed his technical skills as a Geek and Senior Linux Systems Administrator.
Leave a Reply