Skip to main content
Ryan Price and the Media
Commanding Chaos for Coworking, Open Source and Creative Communities

Main navigation

  • Blog
  • About
  • Contact
  • Subscribe
  • Drupal Easy
  • Florida Creatives
  • Liberatr
  • Lifestream
  • ThinkUp
  • Home
User account menu
  • Log in

Breadcrumb

  1. Home
  2. Awk Magic: Renaming Batch Files

awk magic: renaming batch files

By rprice , 13 August 2008
Categories
Tech
Tags
Drupal
Programming
Teaching

Michael Grant (not verified)

18 years ago

It's past time to move away

It's past time to move away from AWK.

Don't get me wrong, I've used AWK. Hell, between friends of mine, we've lived AWK.

But Perl (and even BASH shell scripting) -- you don't need awk.

> for FILE in `ls *`
do
mv $FILE $FILE.jpg
done

> for FILE in `ls *.jpg*.jpg`
do
mv $FILE ${FILE%*.jpg}
done

Hmm, somehow, files that should be jpegs have the extension ".null"... ugly!

ls -1 | \
awk -F\. '/.null/ { print "mv " $0, $1".jpg" }' \
| bash

Let's say you have a bunch of files named "image.jpg.jpg"... ugh!

rprice@server$ ls -1 | \
awk -F\. '/.jpg.jpg/ { print "mv " $0, $1"." $2 }' \
| bash

Now they are renamed to just "image.jpg", that's pretty simple

Next challenge: you have several images which are named "image(1).jpg"... what do you do?

rprice@server$ ls -1 *\(1\)* | \
awk -F '\(1\)' '{ print "mv "$0 "(" $1 "" $2 "" $3 "" $4}' | \
awk -F\( '{ print $1 "\\(" $2 ")" $3 "" $4 "" $5}' | \
awk -F\) '{ print $1 "\\)" $2 " " $3 "" $4 "" $5}' \
| bash

Now they are also renamed to just "image.jpg" - you are a winner!

...and if you had anything named "image.jpg(1).jpg", you will have to run both of these scripts. Lucky you!

For more awk tutorials, check out Drupal Easy.

P.S. if you also have a database that has the same nasty file names like this in it, example:
34,135887,image with spaces,image_with_spaces.jpg(1).jpg,image/jpeg,1
export the thing as a CSV and then apply the following script to the file before you drop all the rows and put them back in:

awk -F, 'BEGIN { OFS = "," } {
sub(/\([0-9]\)/, "", $4)
sub(/\.jpg\.jpg/, ".jpg", $4)
split($4, arr, "/")
print $1, $2, arr[2], "files/legacy/" $4, $5, $6
}' files.csv

Twitter / liberatrRyan Price | Facebook Flickr: Photos from Liberatr.netInstagramYouTube :: Videos by liberatrLinkedIn: Ryan Price

I am nobody's puppet.

Disclaimer: My blogs are not a reflection of my employer's opinions, but are my own, and often unfiltered. Also, some links on this site may be part of an Amazon Associates program, so by clicking on them you help me pay for my independence.

Drupal Association - Individual Member Acquia Certified Developer Acquia Certified Developer Miro Video Player | Download torrents and podcasts, play any video, and do it with style. Gimp - a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. Processing is an electronic sketchbook for developing ideas. Build a custom arts management solution. LibreOffice is the free power-packed personal productivity suite that gives you six feature-rich applications for all your document production and data processing needs

  • Thanks again, Steve. I hope
    14 years 1 month ago
  • Hi - bit late responding here
    14 years 1 month ago
  • Should have added...
    14 years 2 months ago
  • Hi Ryan,
    14 years 2 months ago
  • Ryan,
    14 years 2 months ago
  • It is happening and you are
    14 years 5 months ago
  • In close future I want to
    14 years 8 months ago
  • the article has been
    14 years 9 months ago
  • I think it's a great coup for
    14 years 9 months ago
  • My hardware box acts as an
    14 years 10 months ago

  • Blogging Fringe
  • Bringing Art and Technology Together
  • Drupal Easy Podcast
  • Florida Creatives Podcast
  • Invisible Frisbee
  • Liberatr.net Facebook
  • Liberatr.net
  • Orlando Podcasters
  • Orlando Scene TV
  • Orlando Video
  • Our Yellow House
  • Pulpp.net Orlando Blogs
  • Pulpp.org Real-Time Collaboration
  • Shrink Your Ecological Footprint
  • The Student of the Game

  • Alex Rudloff
  • Apartment E
  • Brian Feldman
  • Chad Miller
  • Charles Krueger
  • Chris Messina
  • Chris Scott
  • Chris Wojda
  • Daniel H. Pink
  • Daniel Smith
  • Darren McDaniel
  • David Terrar
  • Destination Journal
  • Dr. XNLB
  • Elizabeth Maupin
  • Eric Marden
  • Hugh McLeod
  • Jana Waring
  • Jeremy Harrington
  • John Rife
  • Josh Hallet
  • Katie Ball
  • Katharine Miller
  • Lloyd Davis
  • Loren Feldman
  • marc. with a c.
  • Marc Rapp
  • Mario Andy Lurig
  • Mark Baratelli
  • Mike G
  • Noel Hidalgo
  • PSFK
  • Rebekah Lane
  • Stephen Matheny
  • Thomas Thorspecken
  • Tim Welch

  • Florida Drupal Group
  • Izon Orlando
  • Orlando Coworking
  • Orlando Fringe
  • Orlando Improv Festival
  • Orlando PHP
  • Orlando Puppet Festival
  • Page 15
  • The Daily City
  • Urban ReThink

Powered by Drupal

<none>

  • RSS Feed