LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 09-28-2005, 09:57 AM   #1
ceg4048
LQ Newbie
 
Registered: Oct 2004
Distribution: Fedora
Posts: 23

Rep: Reputation: 1
How to recursively rename files using their directory name pattern


Hello,
I have a project in which I need to convert some .tif files and assemble them into a .pdf book. The conversion is not a problem as I can use Adobe in windows however, there are a large number of files (50,000) distributed over several hundred directories . The pattern of the directory names and files is similar to the following examples:

/book1/subject1/chapter1/page01.tif
/book2/subject2/chapter2/page01.tif

My goal is to search through all the target directories and extract all to a single directory, however since they are all similarly named (page01.tif, page02.tif etc.) I need to rename them and keep track of where they came from such that each file combines it's orignal directory name with it's original filename appended, similary to the following pattern:

home/book1_subject1_chapter1_page01.tif
home/book1_subject1_chapter1_page02.tif
home/book2_subject2_chapter2_page01.tif
home/book2_subject2_chapter2_page02.tif


I'm having difficulty coming up with a robust string of commands that can do this renaming non-interactively. Could anyone offer a command string to accomplish this?

Cheers,
 
Old 09-28-2005, 12:37 PM   #2
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
This might get you started. Make the script executable and run it like this example....
./test "/mnt/tech/achome/Music/mp3 files"

mkdir /home/images

Code:
#!/bin/bash

usage()
{
        echo "Usage: $0 [directory]"
        exit 1;
}

test -d "$1" || usage

dir="$1"
name=`echo "$dir" | cut -d/ -f2- | tr '/' '_'`
ls "$dir" | grep -e "[:alnum:]" | \
while read i; do
   j=`echo "$i" | sed 's/\(.*\)/'"$name"'_\1/'`
   cp -v "$dir/$i" "/home/images/$j"
done

Edit: Maybe you better not re-create /home/images after you have already put files into it.

Last edited by homey; 09-28-2005 at 12:43 PM.
 
Old 09-28-2005, 01:16 PM   #3
ioerror
Member
 
Registered: Sep 2005
Location: Old Blighty
Distribution: Slackware, NetBSD
Posts: 536

Rep: Reputation: 34
A simple script incorporting the following should suffice:

There are any number of ways to suck up the filenames, e.g.

filelist=(find ~/whatever)

With zsh, you can do recursive globbing, usnig a **.

filelist=(~/whatever/**)

but don't try that in bash!

Once you have your list of filenames, they just need
a little massaging,

Take a filename:

foo=/some/path/a/b/c/d.e

With zsh, you can nest parameter expansions, but bash can't,
as far as I know, so I'll seperate them:

First, strip off the leading path name, using ${...#...}

foo=${foo#*/some/path/}

(don't forget the trailing slash). This will leave you with "a/b/c/d.e"

Now replace each / with an _, using a ${...//...} expansion (rtfm

foo=${foo//\//_}

foo is now, of course, "a_b_c_d.e". Voila!

Now just mv/cp/ln the original filename to $foo.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
rename all files in a directory merlin23 Linux - Newbie 4 03-24-2005 11:02 AM
Changing owner of a directory recursively? fturcic Linux - General 2 02-01-2005 07:13 AM
rename file with a pattern sujith_deva Programming 7 12-16-2004 07:39 PM
rename all files in my directory BabaKali Linux - Newbie 2 11-10-2004 04:27 PM
Find string pattern in directory of text files magnum818 Linux - Newbie 2 10-15-2003 08:19 PM

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

All times are GMT -5. The time now is 01:25 AM.

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