![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Rules & FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| 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 |
|
|
LinkBack | Thread Tools | Display Modes |
|
|||
|
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. |
| Forum Sponsor | ||
|
|
|
|||
|
A possibility for the given directory srtucture:
Code:
find /pictures -name "*.jpg" |
awk -F/ '{print "mv "$0,FS$2FS$3"-"$4}' | sh
Regards |
|
|||
|
Thank you, that got me close enough to turn it into this:
Code:
find * -name "*.jpg" | awk -F/ '{print "mv "$0,$1"-"$2}' | sh
|
|||
| Google UNIX.COM |
| Thread Tools | |
| Display Modes | |
|
|