LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 04-28-2005, 09:09 PM   #1
Imyrryr
Member
 
Registered: Aug 2003
Location: 3rd electron on the left getting mugged by the nutrinos at Quarks
Distribution: Pop_OS
Posts: 140

Rep: Reputation: 17
Question Complex file renaming


I have spent quite a bit of time trying to understand how to do this but I have to admit I am a scripting newbie. I have read about sed, tr, rename and gawk. I'm a bit overwhelmed with all the info. This is what I am trying to accomplish. I have a list of files:

background_jpg_jpg.jpg
background01_jpg_jpg.jpg
backdrop_jpg_jpg.jpg
scenery_jpg_jpg_jpg.jpg


The end result that I am trying to get to is this:

background.jpg
background01.jpg
backdrop.jpg
scenery.jpg


The information that I have found 'Googling' is about renaming extensions or putting files in sequence or is very convoluted. Any constructive help is greatly appreciated, not to mention I write stuff like this down in my "Cheatsheet Notebook" for future reference.

EDIT: Did I mention that it's about 400 files?

Last edited by Imyrryr; 04-28-2005 at 09:13 PM.
 
Old 04-28-2005, 11:16 PM   #2
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
Here's an example
Code:
#!/bin/bash

dir="/home/images"

for i in `ls $dir` ; do
   j=`echo "$i" | awk -F"_" '{print$1}'`
   mv "$dir/$i" "$dir/$j.jpg"
done
 
Old 04-28-2005, 11:19 PM   #3
Harmaa Kettu
Member
 
Registered: Apr 2005
Location: Finland
Posts: 196

Rep: Reputation: 30
Code:
for file in *_jpg*; do newname=`echo $file | perl -pe 's/_jpg//g'`; mv $file $newname; done
Sed could be used, but I'm more familiar with perl.
 
Old 04-29-2005, 08:27 AM   #4
Imyrryr
Member
 
Registered: Aug 2003
Location: 3rd electron on the left getting mugged by the nutrinos at Quarks
Distribution: Pop_OS
Posts: 140

Original Poster
Rep: Reputation: 17
Thank you so much for this info, I would of never figured that out on my own, at least not in a timely manner. I think it's time I stop procrastinating and start reading on the ABS book.

You guys rock!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
file renaming with shell script or ? XJNick Linux - General 5 07-29-2005 02:43 PM
bash help, renaming file extensions trey85stang Linux - General 8 07-21-2005 04:51 PM
renaming a file?? U-Toast Linux - Newbie 2 08-29-2004 01:07 AM
Bulk File Renaming nevereverend Linux - Newbie 11 07-03-2004 05:28 PM
renaming or converting a text file to a dat file... tangaz Linux - Software 1 10-24-2003 06:57 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 05:59 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration