Recursively files renaming on Linux cli

To change files name recursively you can combine the commands find and rename, as in these ways:

To add suffix (e.g. ‘_mysuffix’)

find . -name “*.jpg” -exec rename -v ‘s/\.jpg$/\_mysuffix.jpg/i’ {} \;

To remove suffix

find . -name “*.jpg” -exec rename -v ‘s/\_mysuffix.jpg$/\.jpg/i’ {} \;

To add prefix (e.g. ‘myprefix_’)

find . -name ‘*.jpg’ -printf “‘%p’ ‘%h/myprefix_%f’\n” | xargs -n2 -exec mv

To remove prefix

find . -name “*.jpg” -exec rename -v ‘s/my_prefix//’ ‘{}’ \;

To add file extension

find . -type f -exec mv ‘{}’ ‘{}’.jpg \;

To change file extension (e.g. from .jpg to .bmp)

find . -name “*.jpg” -exec rename -v ‘s/\.jpg$/\.bmp/i’ {} \;

In the examples above  only files with ‘.jpg’ extension are taken into account, but you can look for other types of file (e.g. “*.pdf”, “*.txt”) or for files that have particular string in name (e.g. “*myfile*”); use “*.*” for any file with any extension.

Leave a comment

Your email address will not be published. Required fields are marked *