4

I have a folder structure like that: 'Pictures/2015/2015-01-02/random_name.jpg' and I want to rename the files to '2015-01-02 001.jpg', '2015-01-02 002.jpg' etc.

I'm not really familiar with bash programming and perl expressions, so any help is greatly appreciated! Thanks in advance

2 Answers 2

5
cd target_dir
dcomp="$(basename "$(pwd)")"
count=1
for file in *; do
    ext="${file##*.}"
    mv -v "$file" "$dcomp $(printf '%03d' "$count").$ext"
    (( count++ ))
done

This assumes you really want a space in the result filenames, which isn't a wonderful idea. You can, of course, change the pattern inside the loop to whatever you want. To do this to multiple directories, put the whole thing in a loop over them.

2

This is what you need ;)

 cd Pictures/2015/2015-01-02
 c=0 curdir=${PWD##*/} rename -n '
     s/.*/sprintf("%s %.03d.jpg", $ENV{curdir}, $ENV{c}++)/e' *jpg

Obviously, this is Perl's rename.

Remove -n switch after your tests (dry-run).

2

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.