The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Need help renaming files bbbngowc UNIX for Dummies Questions & Answers 5 04-23-2008 11:08 AM
Renaming the files redbeard_06 Shell Programming and Scripting 12 02-27-2007 07:10 AM
renaming and number padding using directory as guide, help TiredOrangeCat Shell Programming and Scripting 3 02-12-2007 08:41 PM
renaming files in a directory to lowercase vhariprasad Shell Programming and Scripting 1 03-26-2006 09:26 AM
Help with renaming files mc_cool_e UNIX for Dummies Questions & Answers 4 01-13-2006 12:18 PM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-22-2008
Registered User
 

Join Date: Feb 2008
Location: חיפה
Posts: 14
Renaming files as per directory

I have many files with duplicate names spread out over several tens of directories. I would like to mv them to the parent directory, but to avoid conflicting filenames I'd like to prefix each filename with the name of the directory it was in.

For example, if this is my directory structure:
/pictures/dotan/1.jpg
/pictures/dotan/2.jpg
/pictures/dotan/3.jpg
/pictures/ety/1.jpg
/pictures/ety/2.jpg
/pictures/ety/3.jpg

Then I'd like to end up with this:

/pictures/dotan-1.jpg
/pictures/dotan-2.jpg
/pictures/dotan-3.jpg
/pictures/ety-1.jpg
/pictures/ety-2.jpg
/pictures/ety-3.jpg

Assuming that pwd is /pictures, I figure that I'll need two expressions, one to rename and another to move. However, I don't know where to start. I've googled the cp (for renaming) and file (for going through all the directories recursively) however I don't know how to access the name of the directories. As for recusively moving the files afterwards, I will probably use file again.
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 03-22-2008
Moderator
 

Join Date: Feb 2007
Posts: 1,665
A possibility for the given directory srtucture:

Code:
find /pictures -name "*.jpg" |
awk  -F/ '{print "mv "$0,FS$2FS$3"-"$4}' | sh
Try it first without the last pipe command to sh (in blue) to be sure it gives you the proper commands.


Regards
Reply With Quote
  #3 (permalink)  
Old 03-22-2008
Registered User
 

Join Date: Feb 2008
Location: חיפה
Posts: 14
Thank you, that got me close enough to turn it into this:
Code:
find * -name "*.jpg" | awk  -F/ '{print "mv "$0,$1"-"$2}' | sh
However, now I've added yet another tool to my box! I've been avoiding awk for the time being, until I get a handle on more basiv stuff. Although I now have working code, I would still like this to be a positive learning experience. Is it possible to do this with regular bash tools: find, mv, grep, and so forth? I would really like to know how to do this with the commands that I'm currently learning, without getting into awk or perl.
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 01:57 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0