This guide will focus on how you can easily move files and directories in Linux with the use of the mv
command.
To use the mv
command we must understand its syntax:
mv flags source destination
The most common mv
command options are:
-i
– Interactive mode. Prompts a message before overwriting.-b
– Creates a backup.-f
– Force, no prompts.-n
– Do not overwrite existing files.-v
– Verbose mode. It shows what its doing.
Move Files and Directories in Linux
Now that we already know the syntax of the mv
command, we can start moving our files and directories. It is important to note that your user should have write permissions on the source and its destination.
Files
Single File overwrite
To move a single file with the interactive mode. On our example, we will be moving a file with the same name of a file found on our test folder:
mv -i file directory
example
root@server:~# mv -i test.txt test/
mv: overwrite 'test/test.txt'? Y
root@server:~#
In the case that you’d like to force overwrite, you can use the -f
flag. This will simply force the overwrite with no prompts.
Multiple Files
To move multiple files, you can list them right next to each other:
mv flags file1 file2 file3 directory
example
mv -i family.jpg pets.jgp friends.jpg photos/
Directories
Single Directory
To move a directory:
mv flags directory_source directory_destination
Multiple Directories
Similarly to files, you can list all the directories you’d like to move right after each other but before the destination location:
mv flags directory_1 directory_2 destination
Summary
This short guide talked about how you can move files and directories in Linux with the use of the mv
command.