Home / Article / Linux Move File

Linux Move File

One of the most basic tasks you’ll encounter is moving files and directories.

Today, we’ll teach you how to do it. Using the “mv” command, you’ll move files and directories in no time.

Whether you want to copy directory in Linux or move them, this article is for you!

How to use the “mv” command

Let’s take a quick refresher recap of what the “mv” command can do before we get started.

If you’re familiar with the “cp” command, you won’t have a hard time learning the “mv” command. They’re more or less similar.

But, there’s one vital difference between the two. Using the “mv” command allows you to move the file in the destination to another physically.

On the other hand, the “cp” command function duplicates your files.

The “mv” command can move files and directories from one destination file to another.

The command can even move multiple files and directories in one operation.

It’s a convenient way of doing things.

How to move a file

The syntax when you want to use the “mv” command is:

mv [OPTIONS] SOURCE DESTINATION

Let’s dissect the script so that you can understand it better.

“SOURCE” can pertain to files and directories.

“DESTINATION,” on the other hand, only pertain to one file or directory.

Important points to remember

  • Whenever you identify multiple files or directories as your “SOURCE,” remember that the “DESTINATION” should always be a directory. Doing so transfers the “SOURCE” files to the directory you’ve indicated.
  • If you identify only a single file as your “SOURCE” and the “DESTINATION” is an already existing directory, it is moved there.
  • If you identify only a single file as “SOURCE” and “DESTINATION,” you’re only renaming it.
  • If the “SOURCE” refers to a directory and the “DESTINATION” doesn’t exist, you’ll only be renaming “SOURCE” as “DESTINATION.”

When moving a file, there are two things you need to consider. First, the name of the file you want to move, and second, the files’ new name.

Let’s say the file is “ABC.txt” and you’ll rename it as “DEF.txt.” The script will look like this:

ls
ABC.txt
mv ABC.txt DEF.txt
ls 
DEF.txt

How to move a file in a directory

“mv” lets you move a file in a directory of your choice.

Let’s say you want the file “ABC.txt” to move in the directory “CDE.” The script should be:

tree -F .
.
├── CDE/
└── ABC.txt
mv ABC.txt CDE
tree -F 
.
└── CDE/
    └── ABC.txt

You can also move multiple files and directories in one action. Identify the files and the destination you plan to move it in.

For example, you want the files “ABC1” and “ABC2” to be moved to “DIRECTORY.” The script would be:

mv file1 file2 dir1

You can also use pattern matching with the “mv” command.

If you want to move all “txt” files, for example, from its current directory to a new destination like “DIRECTORY,” the script would be:

mv *.txt ~/DIRECTORY

How to move a Directory

You could also move a directory in the same way as to how you would move a file.

Let’s use the same example above. The current directory is “ABC,” and you want to move it to “DEF.” The script will look like this:

ls -F 
ABC/
mv ABC DEF
ls -F
DEF/

Other “mv” command options

“mv” allows you several options that affect the default command behavior.

We’ll go through each option one by one.

Overwriting an existing file 

Generally, using “mv” overwrites your existing file. For example,

ls 
ABC.txt DEF.txt
mv ABC.txt DEF.txt
ls
DEF.txt

As can be seen from the example, “DEF” was overwritten by “ABC.” In one way or another, you probably didn’t mean to overwrite the file at all.

The “-f” or force command overrides the interactive moves and modes you make, without any prompt. It’s not an ideal command to use unless you’re familiar with it.

If you want to receive a prompt before overwriting your files, you can use the “-i” or interactive command.

The command prompts you when an existing file will overwrite the file you chose.

For example:

ls 
ABC.txt DEF.txt
mv -i ABC.txt DEF.txt
mv: overwrite 'DEF.txt'? n

Don’t worry about losing your files because you’ll be prompted with a message before proceeding with an action.

Don’t overwrite existing files

If you want to be sure that you don’t overwrite your existing files, you can use the “-n” option.

mv -n file1 /tmp

Backing up files

If there’s already an existing destination file, you can create a backup using the “-b” option.

mv -b file1 /tmp

The backed-up file will have the same file name as the original. But, the backup file will have a “~” or tilde appended before the file name.

If you want to verify that a backup was created, you can use the “Is” command.

ls /tmp/ABC*
Output
/tmp/ABC  /tmp/ABC~

Verbose Output

Another useful option you can use is the “-v” or verbose option. It shows you a list of the files you’ve moved.

mv -i ABC /tmp
Output
renamed 'ABC' -> '/tmp/ABC'

Move files newer than the destination

If you have an existing file that’s newer than your destination, use the “-u” option to move the file.

The “-u” option only works on a file that’s not newer than the directory. Otherwise, this option won’t work, and your file won’t be moved.

For example:

tree -F .
.
├── DEF/
│   ├── DEF.txt
│   └── ABC.txt
├── DEF.txt
└── ABC.txt

Let’s take at the timestamps of the example above.

ls -l
total 4
drwxr-xr-x 2 rdj users 4096 Oct 11 20:18 DEF
-rw-r--r-- 1 rdj users    0 Oct 11 20:18 DEF.txt
-rw-r--r-- 1 rdj users    0 Oct 11 20:20 ABC.txt
ls -l DEF
total 0
-rw-r--r-- 1 rdj users 0 Oct 11 20:18 DEF.txt
-rw-r--r-- 1 rdj users 0 Oct 11 20:23 ABC.txt

In the above example, it reveals that the “ABC” file is older than the “ABC” file located in the “DEF” directory.

When you use “-u,” only the new file will be moved.

mv -u ABC.txt DEF.txt DEF
ls 
DEF ABC.txt

Conclusion

You can quickly move files using the “mv” option. As long as you master how to use it, you shouldn’t encounter any problem!

You can do so much with the “mv” command option. It can do different tasks when you use it with other commands.

There might be a lot of going on in your head when you use a Linux. “How to copy a directory” or “Easy ways to renaming files in Linux.”

We hope we shed light on some of your questions and you learned from this tutorial.

If you enjoyed this article, feel free to share this with your friends and family. Who knows, you might help them!

You can also leave us a comment below or questions you might want to get answers on.

Until the next tutorial!