Processing a number of filenames at once

Sometimes it is necessary to process one or more of the same changes to a number of filenames at once. This is a tidious task for a human, but, a perfect task for a computer.

Here are some examples of Unix oneliners, used on a FreeBSD operating system.

In this example, all filenames will have their spaces replaced with operating system and network friendly underscores.

for i in *.*; do mv "$i" "`echo $i| tr ' ' '_'`"; done

In this example, all filenames will be converted to operating system friendly lowercase letters.

for i in *.*; do mv "$i" "`echo $i| tr [A-Z] [a-z]`"; done

In this example, we want to make archive friendly filenames. The text “2009-10-13-” will be inserted before the existing filename for all filenames in the directory. This is also referred to as prefixing.

for i in *.*; do mv "$i" "2009-10-13-$i"; done

In this example, we also want to make archive friendly filenames, but, this time, the date of today will be inserted before the existing filename for all filenames in the directory.

for i in *.*; do mv "$i" "`date "+%Y-%m-%d"`-$i"; done

You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed.


Comments are closed.