0

After using command:

find path1/ -iname 'file*[1234]' -exec mv -t /home/user1/dir1/path1/path2 {} \;

I got an error that those files are the same. The command finished successfully. The parent directory from where the search started is path1/ and then as destination directory I put whole absolut path where I want the files moved. Why did this happen?

1

What mv is saying is that it makes no sense to move the particular file as moving it to the destination would write it back to itself.

You'll get the same thing if you try

mv file file

for any file called file.

As to why this happens:

You run find on the path path1. I'm assuming that this is the same path1 as in the absolute path /home/user1/dir1/path1/path2.

If you have files in /home/user1/dir1/path1/path2 whose names matches the pattern that you use (these may have been moved there by an earlier invocation of your find command, or even by the same find due to the order of directory traversals that find makes), then these would be found by find (since path2 is a subdirectory of path1). This would prompt find to try to execute an mv command that would move files from path2 into path2. This is what triggers the message (I'm hesitating to call it an actual "error").

To avoid looking into path2 with find, you may do this:

find path1 \( -type d -path 'path1/path2' -prune \) -o -iname 'file*[1234]' -exec mv -t /home/user1/dir1/path1/path2 {} +

If the thing found is a directory (-type d) and if it's the pathname path1/path2, then prune it from the directory search tree that find builds while traversing the directories. Otherwise, continue as before (almost, I changed the final \; to + to make mv take as many pathnames as possible at once).

  • I think the problem here is that there is a file matching the pattern in .../path2/ because another such file was moved to that target directory during the find command and the target directory is traversed after the directory the file originated from. If you have Afolder/file but no Bfolder/file and you do find -name file -exec mv -t Bfolder {} \;, you get the error, but if you have a Bfolder/file and no Afolder/file, then you don't. – Stefan Hamcke Jan 24 at 21:22
  • @StefanHamcke The error indicates that you are moving a file to itself. Not just that you are moving a file to another with the same name. The actual inode is the same when this message is outputted. Also, I think you got your two examples mixed up. – Kusalananda Jan 24 at 21:36
  • I know. I'm referring to your sentence "If you have files in /home/user1/dir1/path1/path2 ...". I think the OP did not originally have such files in the target dir, but they were moved there due to the command. Let me explain my example: Say you are in a dir with only Afolder/ and Bfolder/ and also Afolder/file (regular file), but nothing else. Then you do not even need a Bfolder/file for the message to appear when you execute find -name file -exec mv -t Bfolder {} \;. – Stefan Hamcke Jan 24 at 21:52
  • @StefanHamcke Ah, yes. It may depend on the order of traversal as well. If the destination directory is late in that order, files may already have started to accumulate there. – Kusalananda Jan 24 at 21:55
  • The above command will first descent into Afolder/, find the file, and move it to Bfolder/. After that, it will look into Bfolder/ and stumble upon the file it did just move there. – Stefan Hamcke Jan 24 at 21:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.