Welcome to the most active Linux Forum on the web.
Go Back > Forums > Linux Forums > Linux - General
User Name
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.


  Search this Thread
Old 09-28-2005, 09:57 AM   #1
LQ Newbie
Registered: Oct 2004
Distribution: Fedora
Posts: 23

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

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:


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:


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?

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

Rep: Reputation: 59
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


        echo "Usage: $0 [directory]"
        exit 1;

test -d "$1" || usage

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"

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
Registered: Sep 2005
Location: Old Blighty
Distribution: Slackware, NetBSD
Posts: 536

Rep: Reputation: 30
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 **.


but don't try that in bash!

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

Take a filename:


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 ${...#...}


(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 is now, of course, "a_b_c_d.e". Voila!

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


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

All times are GMT -5. The time now is 02:04 AM.

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