Skip navigation

Tag Archives: linux shell script rename files

Ok, brief introduction on this.  We are moving, and in the process of selling a home among other things.  So in the process of staging our home we’ve packed up our desktops and servers here.  Not a problem, we have 3 laptops, 1 is my personal, 1 is for my work, and the other is a shared one for the family.  Recently while testing some stuff with hyper-v installed on the shared laptop I had imaged the machine then saved it to a portable drive. Well what does that have to do with anything? My wife wanted to use the shared laptop as a stand in until her desktop could come back out, and I’d since deleted the image and packed all my windows 7 discs etc. Ruh roh.

So being the FOSS guy that I am (outside of work), I decided I’d use this opportunity to throw a very user friendly Linux distribution onto the laptop and see what my wife thought of it.

The distribution:

Linux Mint 13 with KDE

amanda-desk1

Now this was a fairly easy setup to make for my wife.  She is what I would consider an average user with some advanced usage areas.  Primarily with video editing and photo editing (some graphic design stuff).  However for her day to day she needed web, office suite, and email.  Since I’m going to be on the road a lot, I needed her to have Skype as well as other chat programs.

I installed from a USB key using the live iso image.  From windows this is easy to achieve, download Windows 32 Disk Imager and the desired ISO file, rename it to a .img then apply it to your flash device and viola.

After a very light crash course on navigating the system and the passwords I setup for her she was off and rocking.  Our first hurdle however was the native mail client with KDE, Kmail.  It’s functional, but she was having some issues with the large volume of emails she has on one of her accounts so I opted to install one of my favorite FOSS mail clients on their instead, Thunderbird

We both are avid Chrome users, and have our accounts and profiles linked for easy setup across platforms, so that was a breeze for her.  I imported her windows profile data to her home directory which got her back up and running with the data she needed as well.

For her Office needs we went with (and she’s adjusted quite easily to) using LibreOffice which has usurped OpenOffice as the FOSS Office Suite of choice with most Linux distros these days.

For her photo editing there is of course Gimp which has been an amazing free image manipulation application for over a decade.  For her video editing needs there is Avidemux and OpenShot, neither of which she has made use of yet.  Her video editing needs are slim at this time anyway.

Now, here is the mind blowing part for some of you Linux timids or FOSS nay sayers.  Up until this point, I’d done everything from install of the OS to now without once going into the terminal.  I had made it a point not to.  If I was going to give this to my wife to use, regardless of my feelings about the console, she shouldn’t have to use it.  All the application installs were performed from default package stores as well using only the Linux Mint Software Manager.  Being a long time Linux user (since 97~98) I was personally blown away by this.  I normally run to the console out of habit, but found that it was at a point where I genuinely didn’t have to.  However there was a problem for usability for my wife that I felt needed to be addressed, and it would require me to script something for her.

In Windows, I have a PowerShell script my wife uses to pull file system object information for her pictures and looks to the creation date then renames the files for her.  So this was something she of course wanted, and even though Dolphin and Konqueror have a really cool built in rename feature, this date method wasn’t possible for her out of the box.  I’m not going to lie, I was personally elated with this task, but finding the best method was tricky so I went with a simple iteration script she could drop in the folder and double click to run.  If she chooses to use Linux longer I’ll work on a possibly building her something a bit more robust, but here’s the existing code:

#!/bin/bash
#Script to rename jpg by create date and number
x=1
for i in *.jpg *.JPG
do
y=$(stat -c %y "$i" | sed 's/^\([0-9\-]*\).*/\1/')
  file=$y"("$x").jpg"
        mv "$i" $file
                x=$[$x+1]
done

 

So there is a bit of regex usage here, but the end result is each jpg file in the existing folder has it’s date information pulled from the file using stat, then piped through sed to chop it down to a preferable string.  That string is then concatenated into a file name with an incremental integer and extension then the existing file has it’s name changed to that newly created string.

So what did my wife gain from this interim transition?  A new appreciation for another operating system and the flexibility and eye candy that comes with Linux environments?  I don’t know, but it’s allowed us to retrofit some old hardware with her level of desired functionality at 0 additional cost.  All in all, I’d say that’s pretty cool, and I’m still stoked my wife is rocking out with Tux.