mv error in shell script "are the same file"
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.
Related videos on Youtube
cgram
Updated on September 18, 2022Comments
-
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 over 7 years
*.*
is Windows, on Linux just use*
. -
cgram over 7 yearsMy bad...just a typo in the question. The actual code had the $. Question is fixed.
-
-
cgram over 7 yearsI'm not sure I follow. If I comment everything else out in the loop, and add:
-
cgram over 7 yearsecho "$src"., I get a list of files in that directory. Seems it's working correctly. What am I missing?
-
cgram over 7 yearsAlso 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 over 7 yearsAnother 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 over 7 yearsAlso, 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 about 6 yearsYou can also run
sudo su; sync; echo 3 > /proc/sys/vm/drop_caches
as a quick fix.