move files into folders and rename existing files.

Hello everyone.


A few years ago i got some help here with moving and sorting files with great success.

I have modified the script a bit for a new situation and now i'm stuck.


The script moves files into specific folders depending on the filename and if there are any existing files rename them but i cant get the rename to work. I tried with with the --backup command:

mv -v -b \"$f1\" \"$jpg\" 

But all i get is illegal option, is there any other way to append existing files?


Also i'm sure there must be a better way to loop through the files instead of using two "while do" after each other, though it seems to work just fine.


Example filenames and folder structure:

15512-1.tif
15512-1.jpg

180313-1.tif
180313-1.jpg

2002005-1.tif
2002005-1.jpg


OBJECTS/2018/1803/180313/JPEG/180313-1.jpg
OBJECTS/2018/1803/180313/JPEG/180313-2.jpg
OBJECTS/2018/1803/180313/JPEG/old1_180313-1.jpg (exampl of renamed existing file)
OBJECTS/2018/1803/180313/JPEG/old2_180313-1.jpg



do shell script "/bin/bash -s <<'EOF'
SRC1=/volume1/Archive/IMAGES/UPLOAD/JPEG/
SRC2=/volume1/Archive/IMAGES/UPLOAD/TIFF/
DST=/volume1/Archive/IMAGES/OBJECTS/
{
export LC_CTYPE=UTF-8
while IFS= read -r -d $'\\0' f1
do
  n=${f1##*/}
  [[ $n =~ ^(([0-9]{2})([0-9]{1,2})[0-9]{2,3})[^0-9] ]] || continue	 #5,6 & 7-digits (yymkk) (yymmkk) (yymmkkk)
  p=${BASH_REMATCH[1]}
  y=${BASH_REMATCH[2]}
  m=${BASH_REMATCH[3]}
  m='0'$m
  m=${m: -2}
	
	jpg=${DST}/'20'${y}/${y}${m}/${p}/JPEG
	[[ -d $jpg ]] || mkdir -p \"$jpg\" || continue
   
	mv -v -b \"$f1\" \"$jpg\"
	
done < <(find \"$SRC1\" -type f -iname '*.jpg' -print0)

# TIFF files
while IFS= read -r -d $'\\0' f2
do
  n=${f2##*/}
  [[ $n =~ ^(([0-9]{2})([0-9]{1,2})[0-9]{2,3})[^0-9] ]] || continue	 #5,6 & 7-digits (yymkk) (yymmkk) (yymmkkk)
  p=${BASH_REMATCH[1]}
  y=${BASH_REMATCH[2]}
  m=${BASH_REMATCH[3]}
  m='0'$m
  m=${m: -2}
	
	tif=${DST}/'20'${y}/${y}${m}/${p}/TIFF
	[[ -d $tif ]] || mkdir -p \"$tif\" || continue
   
	mv -v -b \"$f2\" \"$tif\"
	
done < <(find \"$SRC2\" -type f -iname '*.tif' -print0)

} 2>&1
exit 0
EOF"
--END OF APPLESCRIPT

MacBook Pro 15", macOS 10.15

Posted on Mar 18, 2020 8:31 AM

Reply
Question marked as Top-ranking reply

Posted on Mar 18, 2020 9:21 AM

The GNU mv command on Linux supports the -b or --backup= syntax, but the BSD mv command found on macOS does not have this option — hence your illegal option message, and the peril of using examples from the Internet where most are based on GNU utilities, and not on macOS examples.


So, with BSD mv, there is no backup model built into the mv command. You could introduce a test if the target file exists, and then copy to a backup extension name.jpg.bak just before your mv command. This would only provide for one backup level. Introducing incremental numeric backup formats (e.g. name001.jpg) would be more work.






Similar questions

1 reply
Question marked as Top-ranking reply

Mar 18, 2020 9:21 AM in response to PJ.Henning

The GNU mv command on Linux supports the -b or --backup= syntax, but the BSD mv command found on macOS does not have this option — hence your illegal option message, and the peril of using examples from the Internet where most are based on GNU utilities, and not on macOS examples.


So, with BSD mv, there is no backup model built into the mv command. You could introduce a test if the target file exists, and then copy to a backup extension name.jpg.bak just before your mv command. This would only provide for one backup level. Introducing incremental numeric backup formats (e.g. name001.jpg) would be more work.






This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

move files into folders and rename existing files.

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.