Batch Rename Multiple Files with Linux

Here is a list of ways how to rename multiple files with Linux. The easies and quickest way I found was to use ‘rename’.

Prompt me, Bash! Pronto!

While searching for a promt command for bash to trimm long pwds (current working directory) I stumbled uppon “Blog.ubrious“. After a little hacking I adopted it to work for linux bash. It also cuts off leading characters when pwd is longer than 20 characters with build-in commands only. Plus I opted to use a ‘@’ as dilimeter between user name and host name so to rather have ssh style. Here is what I came up with:

PROMPT_COMMAND='PS1="\[33[0;33m\][\!]\`if [[ \$? = "0" ]]; then echo "\\[\33[32m\\]"; else echo "\\[\33[31m\\]"; fi\`[\u'@'\h: \`if [[ ${#PWD} > 20 ]]; then echo ${PWD:$((${#PWD}-20))}; else echo "\\w"; fi\`]\$\[33[0m\] "; echo -ne "33]0;`hostname -s`:`pwd`07"'

If wordpress does mess up the code (which I hope does not happen) please visit the original blogger’s post since he was able to upload a text file (I’m not, unfortunatelly).Here is a small list on what it can do for you and a little demo:

  1. Changes to red if the last command didn’t have a return code of 0
  2. Includes your bash history # for easy history command repeating. Follow the link to find out how to use !!, !$ and !number.
  3. Makes sure path length doesn’t exceed 30 regardless of what the base name is (bit after last fwd slash).

Bash prompt trimming

The only small hassle about it is even though it uses \w it wouldn’t abreveate my home directory as ‘~’ for me.

Note 2008/05/14: If you want to use this promt command with screen, too, make sure to test $TERM for ‘screen’. For example mine reads:

case "$TERM" in
xterm*|rxvt*|screen*)
     PROMPT_COMMAND=...

Resizing Digital Pictures Made Easy

One might think it’s not that much of a task to resize a bunch of files in one directory so a certain dimention. Say you want to have all jpg images in your directory have width 1024 with aspect ratio maintained. For one image using GIMP actually this is easy. Just open it, go to image > scale image and type “1024″ for width. That’s it. Knowing of GIMP’s batch mode feature I though: Well, now let’s just do this very same thing in batch mode for multiple files. Googling I found this passage Adrian Likins GIMP Batch How-To:

Batch mode is slow. Its not really a practical replacement for tools like ImageMagick or NetPBM when it comes to large scale image conversions or similar. At least not without writing some very clever scripts.
The problem with this approach is that gimp/script-fu has no built in procedures to iiterate though a list of images. So you cant easily tell gimp to load up *.jpg and run predator.scm on them, at least not without it taking a _long_ time.

There’s the solution, I thought. So I installed ImageMagick for Windows and made myself ready to run a Windows batch for loop on the files. After double checking against ImageMagick’s delivered help for convert I knew the syntax:

convert  -resize 1024

So far so good. But now comes the Windows batch bit. Even though fairly powerfull even compared with bash or other mighty script languages on Linux I couldn’t figure out how to run more than one command per iteration step. This is what I came around to, which does what intended, but without a pretty notice to the user that could have been done with adding something like ; @echo %i

for /f %i in ('dir /b *.jpg') do convert %i -resize 50% %i

Note that I chose to halve the pics rather than to hard-set the width so I don’t risk upright pictures to become bigger in file size than the original. There you go!

Update: I finally came around to do it with Linux. Here is the Bash equivalent to the above (plus an additional nice notification which is easy with Bash):

for i in *.jpg; do echo "converting \"$i\"..."; convert "$i" -resize 50% "$i"; done

Follow

Get every new post delivered to your Inbox.