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 `network_files/' to `tax_network/network_files': Directory not empty

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 `network_files/' to `tax_network/network_files': Directory not empty

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.

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 -)

How to install rclone on Ubuntu 18.04

Install Rclone on Ubutu 18.04

In this article, we are going to learn about a powerful command-line tool to sync files and directories to and from different cloud storage providers.

Dubbed “rsync for cloud storage”the open-source rclone enables you to send data back and forth to the likes of Google Drive, OneDrive, Amazon S3, Yandex Disk, etc.

rclone comes with a few useful features:

  • Always checks for file integrity using MD5/SHA-1 hashes
  • Timestamps preserved on files uploaded
  • Partial syncs supported on a whole file basis
  • Built-in Copy mode to just copy new/changed files
  • Sync (one way) mode to make a directory identical
  • Check mode to check for file hash equality
  • Can sync to and from network, e.g. syncing two different cloud accounts
  • Optional encryption (Crypt), optional cache (Cache), optional FUSE mount (rclone mount)
  • Multi-threaded downloads to local storage
  • Supports HTTP/WebDav/FTP/SFTP/DLNA

How to install rclone on Ubuntu

Because rclone is written in Go programing language, it comes in a single binary file.

There are several ways to install it on Ubuntu. The easiest approach would be to use official package for apt or the snap package manager. Alternatively, you can install rclone using a bash install script provided by its development team.

We will walk you through all of them in this article.

Install rclone the official package from Ubuntu repositories (for Ubuntu 18.04 onwards).

Since Ubuntu 18.04 Bionic Beaver, rclone has become mature enough to become a part of the official repositories. To most users, this is the best way as you will have the most stable, well-tested version of rclone on your system.
If the package manager of your choice is apt, simply run these commands in your terminal. We’ll first make sure that the system is fully updated.

sudo apt update && sudo apt upgrade sudo apt install rclone -y

Unfortunately, if you prefer snap app store, I have bad news for you : rclone development team has abandoned support for the snap package because it’s too hard to make it work properly (and peacefully) with the security confinement of snap.

Install rclone using the official bash script

If you have any problem with the package provided by Ubuntu (which is quite rare by the way), you can always install rclone using an official bash script from the development team. Open up your terminal and run the following commands :

You have to make sure curl is installed on your system :

curl --version

If you see something like this, then you’re good to go:

curl 7.58.0 (x86_64-pc-linux-gnu) libcurl/7.58.0 OpenSSL/1.1.0g zlib/1.2.11 libidn2/2.0.4 libpsl/0.19.1 (+libidn2/2.0.4) nghttp2/1.30.0 librtmp/2.3 Release-Date: 2018-01-24 Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtmp rtsp smb smbs smtp smtps telnet tftp Features: AsynchDNS IDN IPv6 Largefile GSS-API Kerberos SPNEGO NTLM NTLM_WB SSL libz TLS-SRP HTTP2 UnixSockets HTTPS-proxy PSL

If you see something like this, you will need to install curl first :

Command 'curl' not found, did you mean: command 'curl' from deb curl Try: sudo apt install

Get curl to your system :

sudo apt install curl -y

Download the bash script and let it does the install :

curl | sudo bash

If you have any difficulties in installing rclone using any of the methods above, please dont hesitate, let me know in the comments below.