mv error in shell script "are the same file"

12,920

Solution 1

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.

Solution 2

First, don't debug your scripts using sudo, ever!

The expression "$dir"*.* does not mean what you think. Try:

echo "$dir"*.*

To produce a list of files in $dir, use find (see man find):

find "$dir" -type f -print

which can be piped into your script, or split into parameters with xargs (man xargs).

find and xargs (and sort and a few others) can handle filenames with spaces (A file name.txt) by using NUL (0x00) delimiters. Sadly, there is a mixture of commane line switches, (-0, -z, --null), to achieve this, see the man pages.

Share:
12,920

Related videos on Youtube

cgram
Author by

cgram

Updated on September 18, 2022

Comments

  • cgram
    cgram almost 2 years

    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.

    • Byte Commander
      Byte Commander over 7 years
      *.* is Windows, on Linux just use *.
    • cgram
      cgram over 7 years
      My bad...just a typo in the question. The actual code had the $. Question is fixed.
  • cgram
    cgram over 7 years
    I'm not sure I follow. If I comment everything else out in the loop, and add:
  • cgram
    cgram over 7 years
    echo "$src"., I get a list of files in that directory. Seems it's working correctly. What am I missing?
  • cgram
    cgram over 7 years
    Also note that the script is not erring on EVERY file. just one or two, depending on what's populated in the source dir. So, there's something causing the error to trigger intermittently.
  • cgram
    cgram over 7 years
    Another caveat....if I reboot the box, then rerun the script (without changing anything in the source or dest directories), it suddenly works. Could there be something cached that's causing this?
  • cgram
    cgram over 7 years
    Also, one more piece of info....if I remove the spaces in the new file "newfile="$(date -r "$file" +"%Y-%m-%d-%H-%M-%S").MOV"", it works just fine.
  • Kyle Bridenstine
    Kyle Bridenstine about 6 years
    You can also run sudo su; sync; echo 3 > /proc/sys/vm/drop_caches as a quick fix.