Register
It is currently Tue Nov 25, 2014 10:03 pm

smv - rename files with search/replace


All times are UTC - 6 hours


Post new topic Reply to topic  [ 3 posts ] 
Author Message
 PostPosted: Tue Apr 06, 2010 3:47 pm   

Joined: Wed Mar 10, 2010 8:05 pm
Posts: 25
there's more I want to do to this script, but it serves it's purpose.

smv renames a file based on a sed search and replace string. like so:
# ls
file
# smv 's/il/3333/g' file
# ls
fi3333e

combined with my 'yargs' script I posted on here, you can do large batches of files this way.

Code:
#!/bin/bash
#released under the GPL v2

if [ $# -lt 1 ]; then
  /bin/echo "Usage: smv sed_expression filename"
  exit 1
fi

if [ ! -e "$2" ]; then
        echo "file [""$2""] doesnt exist.";
        exit;
fi
SEDED=$(echo $2 | sed "$1");
if [ -e "$SEDED" ]; then
        echo "file [""$2""] already exists.";
        exit;
fi
cp "$2" "$SEDED"
if [ ! -e "$SEDED" ]; then
        echo "failed to copy.";
        exit;
fi
rm "$2"
if [ -e "$2" ]; then
        echo "failed to delete.";
        exit;
fi


Last edited by caibbor on Fri Apr 09, 2010 10:43 pm, edited 2 times in total.

Top
 Profile  
 PostPosted: Thu Apr 08, 2010 4:43 am   
Moderator
User avatar

Joined: Thu Oct 11, 2007 7:12 am
Posts: 229
Location: London - UK
This is quite nifty :)

I did want to say though that if you intend to have your code GPLd then the comment at the start of your script doesn't protect the script very well. I suggest you check out the GNU howto on the subject;
http://www.gnu.org/licenses/gpl-howto.html

For such a short script the preamble may seem a bit long but if you want your code protected by the GPL it's worth adding.


Top
 Profile  
 PostPosted: Fri Apr 09, 2010 10:41 pm   

Joined: Wed Mar 10, 2010 8:05 pm
Posts: 25
aye, I'm juust lazy. I figure who's gunna violate a gpl'd BASH scrpt, anyway. in a large C program I'd be more concerned. but thanks for the link :)

and it just occured to me to add a 'do' loop in the script for multiple files. I'd actually like to get more complex sed'ing in there first, but I'm not familiar enough with sed. and a mv command instead of a cp+rm.


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP