2

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

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 from my iPhone) as duplicates.

Output of mount:

//GRAMNAS/Files_Apps on /mnt/Files_Apps type cifs     (rw,relatime,vers=1.0,sec=ntlm,cache=strict,username=xxxxx,uid=1208001104,forceuid,gid=1208001114,forcegid,addr=x.x.x.x,unix,posixpaths,serverino,mapposix,acl,rsize=1048576,wsize=1048576,actimeo=1)

ls -li on src before script run:

~/.scripts$ ls -li /mnt/Files_Apps/temp/
total 697384
13238340 -rwxrwxrwx 1 chris linuxadmin   8844047 Jul 19 21:39 IMG_5624.MOV
13238349 -rwxrwxrwx 1 chris linuxadmin  96701684 Apr 14 11:58 IMG_7355.MOV
13238366 -rwxrwxrwx 1 chris linuxadmin  98690685 Apr 17 15:06 IMG_7426.MOV
13238384 -rwxrwxrwx 1 chris linuxadmin 100289499 May  5 14:32 IMG_7568.MOV
13238579 -rwxrwxrwx 1 chris linuxadmin  75031725 May  8 11:57 IMG_7662.MOV
13239032 -rwxrwxrwx 1 chris linuxadmin  86233885 May  9 10:13 IMG_7672.MOV
13239098 -rwxrwxrwx 1 chris linuxadmin  76930641 May  9 16:03 IMG_7687.MOV
13239466 -rwxrwxrwx 1 chris linuxadmin  78796811 May 13 13:22 IMG_7806.MOV
13240809 -rwxrwxrwx 1 chris linuxadmin  92599209 May 14 12:39 IMG_7951.MOV

ls -li on dest before script run:

~/.scripts$ ls -li /mnt/Files_Apps/TFTP\ root/
total 0

Script output:

~/.scripts$ bash -v videorename.sh
date -r "$file" +"%Y-%m-%d %H %M %S"
mv: '/mnt/Files_Apps/temp/IMG_5624.MOV' and '/mnt/Files_Apps/TFTP root/2016-07-19 21 39 21.MOV' are the same file
date -r "$file" +"%Y-%m-%d %H %M %S"
mv: '/mnt/Files_Apps/temp/IMG_7355.MOV' and '/mnt/Files_Apps/TFTP root/2016-04-14 11 58 26.MOV' are the same file
date -r "$file" +"%Y-%m-%d %H %M %S"
mv: '/mnt/Files_Apps/temp/IMG_7426.MOV' and '/mnt/Files_Apps/TFTP root/2016-04-17 15 06 35.MOV' are the same file
date -r "$file" +"%Y-%m-%d %H %M %S"
.
.
.

ls -li on src after script run:

~/.scripts$ ls -li /mnt/Files_Apps/temp/
total 697384
13238340 -rwxrwxrwx 1 chris linuxadmin   8844047 Jul 19 21:39 IMG_5624.MOV
13238349 -rwxrwxrwx 1 chris linuxadmin  96701684 Apr 14 11:58 IMG_7355.MOV
13238366 -rwxrwxrwx 1 chris linuxadmin  98690685 Apr 17 15:06 IMG_7426.MOV
13238384 -rwxrwxrwx 1 chris linuxadmin 100289499 May  5 14:32 IMG_7568.MOV
13238579 -rwxrwxrwx 1 chris linuxadmin  75031725 May  8 11:57 IMG_7662.MOV
13239032 -rwxrwxrwx 1 chris linuxadmin  86233885 May  9 10:13 IMG_7672.MOV
13239098 -rwxrwxrwx 1 chris linuxadmin  76930641 May  9 16:03 IMG_7687.MOV
13239466 -rwxrwxrwx 1 chris linuxadmin  78796811 May 13 13:22 IMG_7806.MOV
13240809 -rwxrwxrwx 1 chris linuxadmin  92599209 May 14 12:39 IMG_7951.MOV

ls -li on dest after script run:

~/.scripts$ ls -li /mnt/Files_Apps/TFTP\ root/
total 0
5
  • This is irrational. Are you using RedHat or a related distribution? There seem to be known problems in this area, such as this, without a current solution. I'm on Ubuntu myself, and I've never seen this message, despite frequent use of mv. I can't easily test if you're not using a Debian-derived system. You could try moving to a different file system and back: I feel instinctively that this may be a work-round, but I can't test, and you won't know until you try.
    – AFH
    Oct 6, 2016 at 22:41
  • I'm on the latest Ubuntu
    – cgram
    Oct 8, 2016 at 14:50
  • I'm on Ubuntu 16.04.1, and I've never seen anything like this. The only possible explanation that I can think of is that there are links of some sort (symbolic or hard) between the files or directory locations.
    – AFH
    Oct 8, 2016 at 23:19
  • @cgram: (1) I edited your question.  I changed {dest} to ${dest} because there’s no way you could have gotten the results you report without the $. (P.S. You could use $dest instead.  You hardly ever need the curly braces; see this.) Please check that you don’t have any other typos in your question. (2) Can you do ls -li on the source and destination directories before and after running your script, and post the output?  Please do not respond in comments; edit your question to make it clearer and more complete. Oct 12, 2016 at 0:25
  • @Scott...thanks for the correction, but I had the $ in my actual script...the question was just a typo...apologies. I've done another round of tests, and updated the question with the results, along with ls -li before/after the script run. What's interesting is that if I take the spaces out of the newfile: "newfile="$(date -r "$file" +"%Y-%m-%d-%H-%M-%S").MOV"", it works just fine.
    – cgram
    Oct 12, 2016 at 14:39

3 Answers 3

2

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.

1

For mv complaining on "the same file", try to use mv -n (--no-clobber): Do not overwrite an existing file:

mv -n "$file" "$newfile"

However, "mv -n" might not work properly in older GNU versions:

$ mv -n ./my_file /current_dir/my_file
mv: './my_file' and '/current_dir/my_file' are the same file

To workaround this, check if the files have same device and inode numbers with -ef, before moving:

$ [[ ./my_file -ef /current_dir/my_file ]] || mv ./my_file /current_dir/my_file
0

Spaces are not that tedious to handle:

#!/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

Tell me if it solves your problem. At least it's simpler.

12
  • That is cleaner, thanks. However, I still get the same error: mv: '/mnt/Files_Apps/temp/IMG_8431.mov' and '/mnt/Files_Apps/TFTP root/2016-05-25 18 08 23.MOV' are the same file
    – cgram
    Oct 8, 2016 at 14:45
  • Did you try to do the mv manually? If that does not work, try to create the destination file empty, then do the mv?
    – yarl
    Oct 8, 2016 at 14:46
  • I just did...and it works. It has to be the way the loop is handling spaces, I just can't figure out what the issue is.
    – cgram
    Oct 8, 2016 at 14:49
  • you copy-pasted my code, right?
    – yarl
    Oct 8, 2016 at 14:56
  • I did copy/paste your code. What's odd is that I'm only getting this error for one file. If I clean out the destination dir, and only run the script on this one file, I still get the error. And for what it's worth...the same thing happens if I change 'mv' to 'cp'.
    – cgram
    Oct 8, 2016 at 16:01

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.