1

I have many images of varying extensions (jpeg,jpg,png) contained in many different directories, many of which my be contained in other folders for organizing.

Such as:

Folder 1/
Folder A/
     Image 01.jpg
     Image 02.png
Folder 2/
     Folder B/
          Folder C/
               Image 03.jpeg
               Image 04.png

The hierarchy and folders change depending on what I am working on. I usually need to append the images parent folder as the preffix, and give the pages a number suffix of Page 001,002 etc.

I have been normally going into each folder and batch naming all images in the folder with the program Pyrenamer, but this takes significant time. 50 jobs for 50 folders for example.

I know a script could be made (preferably a nautilus script) that I can use on a single folder containing the many folders to accomplish the job.

For example, "Image 01.jpg" would get renamed to "Folder A - Page 001.jpg", while "Image 04.png" would be renamed to "Folder C - Page 004.png". The images are in a particular order, and need to renamed and numbered in the SAME order as before, just with a proper prefix and suffix.

Pyrenamer can accomplish the suffix, but only allows renaming using the current directory (not parent), making it useless the needed prefix.

My terminal knowledge is limited, as is my programming knowledge. Any help would be greatly appreciated and would greatly speed up my workflow.

  • Doh, that was a type error. Post has been corrected. – KhaosDvorak Apr 26 '15 at 20:58
1

This bash script should do: the path to manipulate has to be passed to it as an argument (this is intentional), e.g.:

./script.sh <directory>
#!/bin/bash

while read filepath
do
    newpath="$(<<< "$filepath" sed 's/^\(.*\)\/\(.*\)\/[^ ]*\(.*\)$/\1\/\2\/Folder \2 - Page 0\3/')"
    mv "$filepath" "$newpath"
done <<< "`find $1 -type f \( -iname '*.png' -o -iname '*.jpeg' -o -iname '*.jpg' \)`"

Before:

<directory>
├── f1
│   ├── Image 01.png
│   ├── Image 02.png
│   └── Image 03.jpeg
├── f2
│   └── f2.1
│       ├── Image 04.jpeg
│       ├── Image 05.jpg
│       └── Image 06.jpg
└── f3
    └── f3.1
        └── f3.1.1
            ├── Image 07.png
            ├── Image 08.png
            └── Image 09.jpeg

After:

<directory>
├── f1
│   ├── Folder f1 - Page  001.png
│   ├── Folder f1 - Page  002.png
│   └── Folder f1 - Page  003.jpeg
├── f2
│   └── f2.1
│       ├── Folder f2.1 - Page  004.jpeg
│       ├── Folder f2.1 - Page  005.jpg
│       └── Folder f2.1 - Page  006.jpg
└── f3
    └── f3.1
        └── f3.1.1
            ├── Folder f3.1.1 - Page  007.png
            ├── Folder f3.1.1 - Page  008.png
            └── Folder f3.1.1 - Page  009.jpeg

  • This did just what I needed. And since I need use Nemo as my default browser, I just click "Open Terminal Here" on my toolbar when I am in the folder I want to start from, then invoke the script and bam, images are renamed. This GREATLY reduces the time I have to spend on tasks and allows me to me stuff done! – KhaosDvorak Apr 26 '15 at 23:46
1

Assuming the folder contain only the files you want renamed, and that you are using bash:

$ shopt -s globstar
$ rename -n 's;/([^/]*)/([^/]*) (\d+)\.(\w+)$;sprintf("/%s/%s - Page %03d.%s", $1, $1, $3, $4);e' ./Folder*/**/*
./Folder 2/Folder B/Folder C/Image 03.jpeg renamed as ./Folder 2/Folder B/Folder C/Folder C - Page 003.jpeg
./Folder 2/Folder B/Folder C/Image 04.png renamed as ./Folder 2/Folder B/Folder C/Folder C - Page 004.png
./Folder A/Image 01.jpg renamed as ./Folder A/Folder A - Page 001.jpg
./Folder A/Image 02.png renamed as ./Folder A/Folder A - Page 002.png

rename is a Perl script that's very useful for renaming files. In this case, I use a regex that:

  • /([^/]*)/ - matches a directory component, bordered by slashes.
  • ([^/]*) - matches a prefix of the filename, succeeded by a space
  • (\d+) - matches the digits in the filename
  • \.(\w+) - matches the extension
  • $ all anchored at the end of the filename.

Then I use sprintf to print the matched values in the desired fashion. Vary the 3 in %03d to get more padding.

The ** in ./Folder*/**/* is a recursive glob, which is why I need to enable globstar.

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.