Getting the error ‘cannot move – to a subdirectory of itself’?

This error message always bothered me because it makes no sense when I get it.

Here’s my scenario… I have a directory that is used as the base for all new accounts I sell on my servers. The path is; /root/cpanel3-skel. I always put WordPress in that directory and I want to keep the latest version of WP in that directory

[root@wiredtree ~]# rm -r wordpress latest.*
rm: cannot remove `wordpress’: No such file or directory
rm: cannot remove `latest.*’: No such file or directory
[root@wiredtree ~]# wget -q http://wordpress.org/latest.zip
[root@wiredtree ~]# unzip -q latest.zip
[root@wiredtree ~]# mv -f wordpress/* /root/cpanel3-skel/public_html/
mv: cannot move `wordpress/wp-admin’ to a subdirectory of itself, `/root/cpanel3-skel/public_html/wp-admin’
mv: cannot move `wordpress/wp-content’ to a subdirectory of itself, `/root/cpanel3-skel/public_html/wp-content’
mv: cannot move `wordpress/wp-includes’ to a subdirectory of itself, `/root/cpanel3-skel/public_html/wp-includes’

WHAAAAT? Obviously it is not a subdirectory of itself…

Something strange is going on… We get a little more information if we try to do a move from one device to another. Take a look at this error message:

[root@wiredtree tmp]# mv -f wordpress/* /root/cpanel3-skel/public_html/
mv: inter-device move failed: `wordpress/wp-admin’ to `/root/cpanel3-skel/public_html/wp-admin’; unable to remove target: Is a directory
mv: inter-device move failed: `wordpress/wp-content’ to `/root/cpanel3-skel/public_html/wp-content’; unable to remove target: Is a directory
mv: inter-device move failed: `wordpress/wp-includes’ to `/root/cpanel3-skel/public_html/wp-includes’; unable to remove target: Is a directory

This reveals the true source of the error message. In “mv –help”, the explanation of “-f” is too simplified and says only:

-f, –force do not prompt before overwriting

In “cp –h” we closer,but not exact, explanation of the real process, and one that better matches the inter-device error message:

-f, –force if an existing destination file cannot be opened, remove it and try again

The final bit of information is in an Ubunto bug 71174 “Misleading error message with mv and existing directories” where they change the error message to be “mv: cannot move `a’ to `b/a’: Directory not empty”.

So there you have it. You get that error message because the -f command tries first to remove the directory and can’t because it contains one or more files. You don’t get this message if the subdirectories are empty. The remove works fine on an empty directory but fails if there are files. The Linux core can’t correctly handle this exception and throws up what is probably the last error message in a switch/case statement “cannot move – to a subdirectory of itself”.

So what do you do about it? Well you can either empty the destination directory first, or you can copy the files and then delete the source directory. I chose the latter option and run this from the root folder:

rm -r wordpress latest.*
wget -q http://wordpress.org/latest.zip
unzip -q latest.zip
cp -rf wordpress/* cpanel3-skel/public_html/
rm -r wordpress latest.*

Cool? Hope that helps someone…

2 Comments

Add a Comment

Your email address will not be published. Required fields are marked *