How to rename multiple files at once How to rename multiple files at once shell shell

How to rename multiple files at once


You can do it with find:

find -name "*poster.jpg" -exec sh -c 'mv "$0" "${0%/*}/folder.jpg"' '{}' \;

Explanation

Here, for each filename matched, executes:

sh -c 'mv "$0" "${0%/*}/folder.jpg"' '{}'

Where '{}' is the filename passed as an argument to the command_string:

mv "$0" "${0%/*}/folder.jpg"

So, at the end, $0 will have the filename.

Finally, ${0%/*}/folder.jpg expands to the path of the old filename and adds /folder.jpg.

Example

Notice I'm replacing mv with echo

$ find -name "*poster.jpg" -exec sh -c 'echo "$0" "${0%/*}/folder.jpg"' '{}' \;./anotherpath/my-poster.jpg ./anotherpath/folder.jpg./path/to/file/test-poster.jpg ./path/to/file/folder.jpg./tuxisthebest/ohyes/path/exm/bold-poster.jpg ./tuxisthebest/ohyes/path/exm/folder.jpg


Try this script, it should rename all the files as required.

for i in $(find . -name "*-poster.jpg") ; do folder=`echo $i | awk -F"-poster.jpg" {'print $1'}`; mv -iv $i $folder.folder.jpg; done

You can replace . to the directory where these files are placed in the command find . -name "*-poster.jpg" in the script. Let me know if it is working fine for you.


you can try it like

find  -name '*poster*' -type f -exec sh -c 'mv "{}"  "$(dirname "{}")"/folder.jpg' \;

find all files containing poster == find -name '*poster*' -type f

copy the directory path of the file and store it in a temporary variable and afterwards affix "folder.jpg" to directory path == -exec sh -c 'mv "{}" "$(dirname "{}")"/folder.jpg' \;