Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mv: cannot overwrite directory with non-directory

Is it possible to get around this problem?

I have a situation where I need to move some files to 1 directory below.

/a/b/c/d/e/f/g

problem is that the filename inside g/ directory is the same as the directory name

and I receive the following error:

mv: cannot overwrite directory `../297534' with non-directory

Example: /home/user/data/doc/version/3766/297534 is a directory, inside there is a also a file named 297534

so I need to move this file to be inside /home/user/data/doc/version/3766

Command This is what I am running: (in a for loop)

cd /home/user/data/doc/version/3766/297534
mv * ../
like image 327
anarchist Avatar asked Dec 20 '13 14:12

anarchist


2 Answers

You can't force mv to overwrite a directory with a file with the same name. You'll need to remove that file before you use your mv command.

like image 63
lreeder Avatar answered Nov 03 '22 05:11

lreeder


Add one more layer in your loop.

Replace mv * ../ with

for f in `ls`; do rm -rf ../$f; mv $f ..; done

This will ensure that any conflict will be deleted first, assuming that you don't care about the directory you're overwriting.

Note that this will blow up if you happen to have a file inside the current directory which matches the current directory's name. For example, if you're in /home/user/data/doc/version/3766/297534 and you're trying to move a directory called 297534 up. One workaround to this is to add a long suffix to every file, so there's little chance of a match

for f in `ls`; do mv $f ../${f}_abcdefg; done
like image 26
merlin2011 Avatar answered Nov 03 '22 04:11

merlin2011