Bash – bulk renaming files to their folders name

bashrename

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

Best Answer

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.

Related Question