How to Move Linux Files With the Mv Command

While you likely already know how to move a file in Linux using the GUI file browser, you may be wondering if there’s a move command in the terminal that allows you to quickly move files to different directories. The mv command is the one you want, and it’s easy to use with its simple syntax and a few optional safety flags.

This basic terminal command works on most Linux distributions, including Ubuntu, Kali Linux, and Fedora.

Mv Command Syntax

The mv command is pretty flexible, but you do have to keep objects in this order when using it:

mv [option] 

Every mv command must have a source and a destination specified; if you include an option, it must come before the source and destination. We’ll explain what some of those options are below.

To try out the mv command without any options, create a quick file and issue a command like this:

mv ~/test.txt ~/Documents

That command will move the file test.txt from the home folder to the Documents directory.

To move multiple files, just list all your files, separated by spaces, before specifying the destination, and they will all be moved in one command.

Additionally, if you have several files you want moved to the same destination, and they all have something in common in their name (such as an extension), you can use an asterisk (*) in the source name as a wildcard.

You’ll notice that in none of these commands did mv ask to confirm your move or even report that anything happened. This is where the options for mv come in.

Mv Command Options

One option you can use is –verbose or -v, which will simply print a record of every operation.

One important note when using the mv command is that unless you specify, mv will automatically overwrite any files in the destination that have the same name as the source file.

You can avoid an accidental overwrite with interactive mode, using the -i option.

In interactive mode, mv will ask you to confirm the move in the event of a file conflict in the destination directory.

To automatically cancel an mv command if there’s a conflict, specify the -n option instead.

You can set mv to, in a conflict, always favor a file with a newer “last modification date” by setting the update option, -u.

This is handy if you have two files with the same name but you want to keep only the most recently updated file.

One more option for avoiding conflicts is the backup option. If you use –backup=numbered, mv will append the source file’s name with ~1~ in the case of a file name conflict. The moved file will then be hidden from normal view unless you reveal hidden files, like with the command ls -a.

Moving Files Seamlessly

We’ve learned some tips and tricks for using mv to move local files quickly and safely in the Linux terminal.

In some cases, you may want to move local files to another machine, and there are many ways of doing this on Linux as well.

Source: makeuseof.com

Related posts

Best Tablets for Kids in 2024

What Is a Snapchat Public Profile and How Do You Create One?

Connections #343: Today’s Answer and Clues (Sunday, May 19, 2024)