4

I am using move command mv -f $file1 $file2 to move a file from source directory to a destination directory. I am getting a failure message as:

mv: cannot create regular file $file2:File exists 

Could you let me know on the reason for such failures from move command?

Are there any ways to solve this error?

ABCD
  • 7,914
  • 9
  • 54
  • 90
user1360733
  • 85
  • 4
  • 6
  • 1
    Guess what, the error message tells you what is wrong. – fdomig Jul 09 '12 at 16:27
  • 2
    fdomig: Normally, when moving files, `mv` automatically overwrites the destination, even without `-f`. – Jay Sullivan Jul 09 '12 at 16:34
  • Are you running the command in a script? If so, show some more code please. – Simon Forsberg Jul 09 '12 at 16:36
  • yes, you are right but I am not sure on the reasons for the above message. – user1360733 Jul 09 '12 at 16:36
  • I am running 'mv -f /var/tmp/test_file.txt /data/test_file.txt' command in my script. – user1360733 Jul 09 '12 at 16:38
  • Regarding OS: I m using Linux – user1360733 Jul 09 '12 at 16:39
  • @user1360733 Are you specifying the parameters directly or through variables? If using variables, show more code. – Simon Forsberg Jul 09 '12 at 16:42
  • 1
    @user1360733: Could you look at the chmod and owner of both source and destination file? If possible, give some commands for completely reproducing the situation, e.g., `touch /data/filea; chmod 700 /data/filea; touch /var/tmp/fileb; chmod 700 /var/tmp/fileb; mv /var/tmp/fileb /data/filea` – Jay Sullivan Jul 09 '12 at 17:24
  • Are you using a case-insensitive or case-preserving filesystem, perhaps? – Rob Davis Jan 16 '13 at 15:20
  • @fdomig I also had this issue recursively writing a directory to an empty location. There is no way the file already exists. The question is probably due to confusion over why the error occurs when the destination clearly doesn't exist. – Nielsvh Oct 25 '13 at 20:28

3 Answers3

2

This is caused by race condition. Your were running multiple mv in your scripts.

ABCD
  • 7,914
  • 9
  • 54
  • 90
  • See https://stackoverflow.com/questions/9463262/race-condition-while-moving-files-on-linux for details. – SergeyR Mar 23 '22 at 23:38
0

Does that file really exist? If it exists, and you are SURE that you want to overwrite it, add the -f flag, which will force the command to continue;

mv -f file1 file2
Alex W
  • 37,233
  • 13
  • 109
  • 109
ivica
  • 1,388
  • 1
  • 22
  • 38
  • 1
    sorry! I missed to add '-f' in the question. I am actually using '-f' in my mv command. – user1360733 Jul 09 '12 at 16:30
  • 3
    You shouldn't have to use `-f` here; `-f` means "do not prompt before overwriting". He's not being prompted, so this won't solve anything. – Jay Sullivan Jul 09 '12 at 17:26
0

This error can be caused by a privileges conflict and occasionally by using illegal characters in the file name. Make sure there are no unusual special characters in the file's name and verify that there is not already a file with the same name in the directory that the file is being moved to. You might need to use ls -l from above target directory to see if the privileges settings will allow you to read/write to the directory.

data88
  • 1
  • 1