Skip to content
View in the app

A better way to browse. Learn more.

Unraid

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Need help with Linux rename command

Featured Replies

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.

 

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 by ken-ji
changed to 6 digits as per OP

  • 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 by hawihoney

Archived

This topic is now archived and is closed to further replies.

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.