Bash – Fix mv Error ‘Are the Same File’ in Shell Script

bashmvscripts

I'm trying to get a script that will rename and move videos. Here is what I have:

#!/bin/bash

src="/mnt/Files_Apps/temp/"
dest="/mnt/Files_Apps/TFTP root/"

for file in "$src"*.*; do
newfile="${dest}$(date -r "$file" +"%Y-%m-%d %H %M %S").MOV"
mv "$file" "$newfile" 
done

When I debug the output, I see this:

$ sudo bash -v videorename.sh 

date -r "$file" +"%Y-%m-%d %H %M %S"
mv: '/mnt/Files_Apps/temp/IMG_7662.MOV' and '/mnt/Files_Apps/TFTP root/2016-      05-08 11 57 58.MOV' are the same file
date -r "$file" +"%Y-%m-%d %H %M %S"
mv: '/mnt/Files_Apps/temp/IMG_7687.MOV' and '/mnt/Files_Apps/TFTP root/2016-    05-09 16 03 39.MOV' are the same file
date -r "$file" +"%Y-%m-%d %H %M %S"
date -r "$file" +"%Y-%m-%d %H %M %S"
date -r "$file" +"%Y-%m-%d %H %M %S"
date -r "$file" +"%Y-%m-%d %H %M %S"

Details of the src directory:

ls -lia
total 148402
1443129 drwxrwxrwx  3 chris linuxadmin        0 Oct  9 18:12 .
26870564 drwxrwxrwx 15 chris linuxadmin        0 Oct  5 15:51 ..
1441900 -rwxrwxrwx  1 chris linuxadmin 75031725 May  8 11:57 IMG_7662.MOV
1443124 -rwxrwxrwx  1 chris linuxadmin 76930641 May  9 16:03 IMG_7687.MOV

I've changed the contents of the source directory several times. Some files it chokes on, others work fine. I can't figure out why it's seeing certain files (all in MOV format imported form my iphone) as duplicates, especially since I'm moving files to a new directory. Any help would be greatly appreciated.

Best Answer

It turns out, the issue revolved around how the CIFS share was mounted. It appears that the inode was possibly being cached, thereby rendering most files duplicates as the script tried to write the new file name. To resolve this, I added 'cache=none' to fstab and remounted the share. I've since run through the script several times with no issues.

While I know WHAT the issue was, I'm still not entirely sure WHY it was in issue. If anyone has insight to how this affects the CIFS mount, I'm all ears.

Related Question