How do I rename 500 photos with a consistent naming scheme?

Quick Linux Tip #23:

Try: rename 's/IMG_(\d+)\.jpg/photo_$1.jpg/' *.jpg

Info: The Perl rename command uses regex to rename files in bulk. -n is a dry-run option that shows what would change without actually renaming files.

Examples:

  • $ rename -n 's/\.jpeg$/\.jpg/' *.jpeg  # Dry-run preview
  • $ rename 'y/A-Z/a-z/' *.TXT  # Lowercase extension/file
  • $ rename 's/(.+)/prefix_$1/' *.log  # Prepend prefix

Note: Ubuntu/Debian use Perl rename. Always test with -n first. RHEL/Fedora use a different rename implementation; install prename or perl-rename as appropriate.

Follow @LinuxTeck for more #LinuxTips




LinuxTeck.com
linuxteck@ubuntu:~$ ls | head -3
IMG_0001.jpg
IMG_0002.jpg
IMG_0003.jpg

linuxteck@ubuntu:~$ rename -n 's/IMG_(\d+)\.jpg/photo_$1.jpg/' *.jpg
rename(IMG_0001.jpg, photo_0001.jpg)
rename(IMG_0002.jpg, photo_0002.jpg)
rename(IMG_0003.jpg, photo_0003.jpg)

linuxteck@ubuntu:~$ rename 's/IMG_(\d+)\.jpg/photo_$1.jpg/' *.jpg

linuxteck@ubuntu:~$ ls | head -3
photo_0001.jpg
photo_0002.jpg
photo_0003.jpg
linuxteck@ubuntu:~$

PREVIOUS ARTICLE Quick Linux Tip #22: How do I prevent a runaway script from consuming all resources? NEXT ARTICLE Quick Linux Tip #24: My script is slow but I don't know why. How do I find the bottleneck?
About John Britto

John Britto Founder & Chief-Editor @LinuxTeck. A Computer Geek and Linux Intellectual having more than 20+ years of experience in Linux and Open Source technologies.

View all posts by John Britto →

Leave a Reply

Your email address will not be published.