September 25, 20187 yr I've written a user script to rename incoming photos. I want to rename like that: IMG_20180925_110601267_HDR.jpg --> 20180925 110601.jpg The following commands do most of the work: rename IMG_ '' IMG_* rename '_HDR' '' *_HDR* rename _ ' ' *_* What's missing is to cut the resulting basename to 15 characters exactly. In the example above the "267" should be removed too. I found a solution but the rename command of that solution seems to work different. The rename that comes with Unraid uses two parameters to search and replace a pattern. The rename example I found seems to accept one parameter to issue a regex search/replace string: rename -n 's/(.{15}).*(\.jpg)$/$1$2/' *.jpg How can I cut the basename after 15 characters with the buildin rename? Any help is highly appreciated.
September 25, 20187 yr I've actually never seen the rename command, but the last example you have using a sed-ish command won't work since rename doesn't support it. you can do this instead: this will extract 8 digits and 6 digits in the IMG_XXXXXXXX_YYYYYY*.jpg pattern and return it as XXXXXXXX\ YYYYY.jpg (the space in between is escaped to tell the shell that the space is part of the filename) for f in IMG_*.jpg; do echo mv $f $(echo $f | sed -e 's/IMG_\([0-9]\{8\}\)_\([0-9]\{6\}\).*\.jpg/\1\\ \2.jpg/'); done this will show you the command that will be attempted to rename everything that matches the pattern you mentioned. if you remove the echo in front of mv and run the command it will execute the commands you just saw. Edited September 25, 20187 yr by ken-ji changed to 6 digits as per OP
September 25, 20187 yr Author Thanks for the help. It didn't work immediately but with your idea and another Google search I could get it to work. It drops lots of errors, but it does what it should. No clue how it works, I'm just the ape hitting the keyboard here: # # Rename "IMG_yyyymmdd_hhmmssxxx_HDR.jpg" to "yyyymmdd hhmmss.jpg" # cd /mnt/user/NewPhotos/ rename IMG_ '' IMG_* rename '_HDR' '' *_HDR* rename _ ' ' *_* for filename in *.jpg; do newFilename=$(sed -E 's#([0-9]{8}) ([0-9]{6})(.*)$#\1 \2\.jpg#' <<< "$filename") mv "$filename" "$newFilename" done Thanks. Edited September 25, 20187 yr by hawihoney
Archived
This topic is now archived and is closed to further replies.