4

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.

  • 1
    *.* is Windows, on Linux just use *. – Byte Commander Oct 10 '16 at 18:09
  • Either is acceptable, and works as far as the loop goes. Just to verify, I changed it to just *, and I get the same error. – cgram Oct 10 '16 at 18:11
  • Yeah, that's not the problem, just a note. But you miss a dollar sign, which definitely breaks something, it must be newfile="${dest}.... – Byte Commander Oct 10 '16 at 18:12
  • My bad...just a typo in the question. The actual code had the $. Question is fixed. – cgram Oct 10 '16 at 18:14
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.

  • You can also run sudo su; sync; echo 3 > /proc/sys/vm/drop_caches as a quick fix. – Kyle Bridenstine Jun 13 '18 at 20:36
0

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.

  • I'm not sure I follow. If I comment everything else out in the loop, and add: – cgram Oct 11 '16 at 17:12
  • echo "$src"., I get a list of files in that directory. Seems it's working correctly. What am I missing? – cgram Oct 11 '16 at 17:14
  • 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 Oct 11 '16 at 17:15
  • 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 Oct 11 '16 at 18:24
  • 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. – cgram Oct 11 '16 at 19:07

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.