How to fix “Directory not empty” when using mv in Linux

mv (or move) is a Unix/Linux command that moves one or many files or directories from one place to another.

mv is usually used for two purposes:

  • It rename a file or folder.
  • It moves group of files to different directory.

mv syntax and usage should be straightforward. However, when you’re learning to use Linux for the first time or when you’ve come to Linux with a solid background in another operating system, you may run into some things that it doesn’t allow.

If you’ve ever received a Directory not empty when trying to move files or directories, then this guide can help you.

Why does “Directory not empty” happens?

This is one of the most common errors with mv. Usually, Directory not empty occured when you’re moving directories, and the target directory already contains files.

$ mv network_files/ tax_network mv: cannot move <code>network_files/' to </code>tax_network/network_files': Directory not empty
Code language: HTML, XML (xml)

You can try --force trying to overwrite files and directories without explicit permission like this, but it won’t work either.

$ mv --force network_files/ tax_network mv: cannot move <code>network_files/' to </code>tax_network/network_files': Directory not empty
Code language: HTML, XML (xml)

mv, as its name suggests, do one thing and one thing only : move files and directories (also rename them, which is another way of saying move).

This is mv‘s built-in protective mechanism to prevent you from accidentally use mv for purposes it isn’t built for. It refuses to work in this case because you’re merging contents of one directory to another.

How to move files/directories to a non-empty directory

Having known the cause of the error, we have several ways to do what we intended to do.

Method 1 : Using rsync

One of the best way is using rsync instead. rsync is built for copying files from source to destination, and have the ability to retain the attributes of many files and folders. Overall, rsync is better than cp because it only copies the files that is not present in the target directory instead of all files.

rsync is available in all major Linux distributions, including all recent versions of Ubuntu. If you don’t know which Ubuntu version is running on your machine, check out our guide on find Ubuntu version number.

Installing rsync on Ubuntu is easy and straightforward, just type in this command in the terminal :

sudo apt-get install rsync -y
Code language: JavaScript (javascript)

Enter this command to your terminal to sync contents from source to target :

rsync -a source/ target/

After that, removes the source directory by :

rm -rf source/*

Method 2 : Using built-in commands

Instead of using rsync, you also can do this classical command which earns you more geek points. It’s quick and dirty, if you know what you are doing:

(cd backup && tar c .) | (cd backupArchives && tar xf -)


Why does “Directory not empty” occurs?

Usually the message means that the target directory already contains files and writing to it could cause confusion.

How to move files safely in Linux without accidentally overwrite files?

Using rsync, you could use “rsync -a source/ target/”, which synchronize the contents of 2 directory.

Fastest way to move directories (or folders) in Linux?

Without installing any other program, you can use this command :
(cd backup && tar c .) | (cd backupArchives && tar xf -)
But be careful, you could be overwriting files without knowing it. Using rsync to do the job is safer and easier.

1 thought on “How to fix “Directory not empty” when using mv in Linux”

  1. Hey Dann,

    I just wanted to say thank you so much for the detailed guide here; you have saved and changed my life. I will now continue to utilize rysnc when and where needed. Thank you so much!


Leave a Comment