Friday, 23 August 2013

Rename Multiple Files in Linux

How-to rename multiple files in Linux


Sometimes you need to rename hundreds or thousands of files at once after one and the same scheme. Here are some examples how to rename the files in the linux terminal.

Rename all .htm files in one directory to .html


This will rename all *.htm files in the current directory and its subdirectories to *.html files. See the Linux manual pages "man bash", "man find", "man mv", "man sed" and "man pcrepattern".
All filename results from find are piped line by line to read and then processed by mv, editing the new filename with sed. This ensures that all paths with whitespace characters are processed correctly.
find . -type f -name '*.htm' | while read filename; do mv -v "${filename}" "`echo "${filename}" | sed -e 's/\.htm$/\.html/'`"; done

Rename all .ogg files in one directory to .oga


This will rename all *.ogg files in the current directory and its subdirectories to *.oga files. See the Linux manual pages "man bash", "man find", "man mv", "man sed" and "man pcrepattern".
All filename results from find are piped line by line to read and then processed by mv, editing the new filename with sed. This ensures that all paths with whitespace characters are processed correctly.
find . -type f -name '*.ogg' | while read filename; do mv -v "${filename}" "`echo "${filename}" | sed -e 's/\.ogg$/\.oga/'`"; done

Add additional file extension to all files


This will add an .old file extension to all files in the current directory, including all subdirectories. See the Linux manual pages "man bash", "man find", "man xargs" and "man mv".
find . -type f -name "*" | xargs -t -i mv {} {}.old
This will add date and time as a file extension to all files in the current directory, including all subdirectories. Very useful for backups. See the Linux manual pages "man bash", "man find", "man xargs", "man mv" and "man date".
find . -type f -name "*" | xargs -t -i mv {} {}.`date +%F-%H:%M:%S`

0 comments:

Post a Comment