'Bash: move and rename files to parent directory

I have the following working code:

for f in */*/*.jpg; do rename 's#/picture#p#' "$f"; done &&
for f in */*.jpg; do rename 's#/subfolder#s#' "$f"; done

However, this does not work with the following verison of rename: https://linux.die.net/man/1/rename

The following folder structure:

folder1/subfolder1/picture1.png
folder1/subfolder2/picture1.png
folder1/subfolder2/picture2.png
folder2/subfolder1/picture1.png

should be converted into the following structure

f1s1p1.png
f1s2p1.png
f1s2p2.png
f2s1p1.png


Solution 1:[1]

Check out the man page for rename that you linked yourself: the command takes two strings "from" and "to" (plus a list of files).

Synopsis

rename from to file...

You could just try the following:

for f in */*.jpg */*/*.jpg; do mv "$f" "$(echo $f | sed 's/\([a-zA-Z]\)[^0-9]*\([0-9][0-9]*\)/\1\2/g' | tr -d /)"; done

This will, however, leave the original folders in place which you will then have to remove manually.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1