Home > Point2 - Technical > How to change all suffixes on Unix (or Linux, or Mac, or Bash)

How to change all suffixes on Unix (or Linux, or Mac, or Bash)

June 2, 2009
ls -1 in action

ls -1 in action

One trend I’ve noticed more and more over the last five years is how little people know about Unix when they come out of their computer-related education.  Most know the bare bones, but very few know more than that and the title of the post is meant to reflect that (most people have different names for the Unix command line).

In that vein, here’s a quick shell script recipe to show how to rename all the files that have the same suffix to some other suffix.  The most common case I have is renaming CAPS suffixes from the Windows world down to lowercase.  As with any Unix script, there’s more than one way to do it.  Here’s how I do it.

\ls -1 *.JPG | awk {'print "mv",$1,$1'} | sed 's/JPG$/jpg/' | sh

A quick explanation of the components:

\ls -1 *.JPG |

The -1 flag to ‘ls’ makes it list every item on a separate line.  The backslash ‘\’ in front of the ls is to remove any aliasing that your might have.  Many people have ls aliased to include their favourite flags, and on many systems that is the default.  The final pipe ‘|’ pipes the output of that command into the next command.

awk {'print "mv",$1,$1'} |

‘awk’ is a text processing program that comes pre-installed on most Unix flavours.  In this case, everything in {‘ ‘} is an awk script, which says “Print the literal text ‘mv’ and then print the first argument piped into you twice.

sed 's/JPG$/jpg/' |

‘sed’ is another text processing program, which can run regular expression substitutions on arguments piped into it.  In this case, the script in between the single quotes says “Substitute the string ‘JPG’ anchored to the end of the line (denoted by $) with the string ‘jpg’.


Finally, the last command takes the output of the first three and runs each line as a shell script.

This is the first piped set of commands I learned on Unix and when I dissected it I ended up learning a lot of different little tricks.  I suggest you do the same.  Try running the ls command by itself, then add the awk, then the sed and see what happens.  Nothing will be renamed until you pipe it all back into the shell.

This little recipe can be extended to do a lot more than just rename all of your poorly suffixed files.  I’ve used it as a building block for changing naming schemes on files, killing running processes (using ps instead of ls, and ‘kill’ instead ‘mv’) and probably lots of others.

For more reading, check out the man pages for sed and awk.  Note that different command lines sometimes want the awk script to be delimited differently: I’ve seen ‘{}’ as well as the {”} notation that I’ve got above.

Hope this helps!

By: Aidan

%d bloggers like this: