Using DTerm and the shell to rename files

I download files from my bank that have some really long name to them and at the end of the name, is the date.

The date is not formatted, of course, and I wanted to rename the files easily, without manually parsing out the string.

Now on my computer, I have DTerm and TextMate at my disposal. So, I created a shell script that would take two parameters: the path to the file and the file name to parse:

#!/usr/bin/env bash

echo "Starting script with parameters $1 $2"

full_path=$1
file_name=$2
year=${file_name:(-12):4}
month=${file_name:(-8):2}
day=${file_name:(-6):2}

cd $full_path
mv $file_name "${year}-${month}-${day}_statement.PDF"

Now that I saved the script in my Documents folder, I can go to where my file is downloaded and type:
/Users/jaimerios/Documents/rename_file.sh "$PWD" mysuperlongfilename20101014.PDF

To make things easier, I use TextExpander to type out the command for me in DTerm, then hit “Command+Shift+V” for DTerm to type out the name of the currently selected file.

Then Voila! The file has been renamed!

References:
http://tldp.org/LDP/abs/html/string-manipulation.html

Leave a Reply

Your email address will not be published. Required fields are marked *