LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 02-19-2011, 09:20 PM   #1
aai
LQ Newbie
 
Registered: Feb 2011
Posts: 3

Rep: Reputation: 0
add path to flenames


Hello,
I have thousands files in thousands subfolders. All subfolders are named differently, but all files named like:

AAA_000_001.tif
AAA_000_002.tif
and so on (AAA_000_999.tif)

I have to replace "AAA_000" with the path of the file.

For example if I have a file AAA_000_001.tif in Foled/sub1/sub2, I have to rename AAA_000_001.tif to Foled/sub1/sub2_001.tif (or Folder_sub1_sub2_001.tif)

I have searched several forums, and so far I have this script:

for a in `find ./ -name "AAA_000*"`
do
mv $a `echo "$a" | sed 's/AAA_000/BBB/g'`
printf $PWD $a
done

it can replace all AAA of all files in all subfolders with BBB, but I did not find a way to replace it with the path (

I will really appreciate your help
Thanks
 
Old 02-19-2011, 10:04 PM   #2
fordeck
Member
 
Registered: Oct 2006
Location: Utah
Posts: 520

Rep: Reputation: 61
You might want to look at the following command:

dirname

Or

basename

Regards,

Fordeck
 
Old 02-19-2011, 10:50 PM   #3
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,445

Rep: Reputation: 829Reputation: 829Reputation: 829Reputation: 829Reputation: 829Reputation: 829Reputation: 829
Code:
find * -name 'AAA_000*' | while read name ; do
    # remove echo to actually do rename
    echo mv $name $(echo $name | sed 'y@/@_@;s/AAA_000_//')
done
 
Old 02-20-2011, 08:57 AM   #4
aai
LQ Newbie
 
Registered: Feb 2011
Posts: 3

Original Poster
Rep: Reputation: 0
Thank you for suggestions!
The script really does what I want! It perfectly renames all files in all subfolders. But, it moves files from their original locations to the main folder. Is it possible to keep them where they are?

Thanks
 
Old 02-20-2011, 10:07 AM   #5
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,445

Rep: Reputation: 829Reputation: 829Reputation: 829Reputation: 829Reputation: 829Reputation: 829Reputation: 829
Yup
Code:
find * -name 'AAA_000*' | while read name ; do
    # remove echo to actually do rename
    echo mv $name $(dirname $name)/$(echo $name | sed 'y@/@_@;s/AAA_000_//')
done
 
Old 02-20-2011, 10:22 AM   #6
aai
LQ Newbie
 
Registered: Feb 2011
Posts: 3

Original Poster
Rep: Reputation: 0
Thumbs up

Yeaaahhh!!
Thank you very much!!!
 
Old 02-20-2011, 06:55 PM   #7
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,492

Rep: Reputation: 1892Reputation: 1892Reputation: 1892Reputation: 1892Reputation: 1892Reputation: 1892Reputation: 1892Reputation: 1892Reputation: 1892Reputation: 1892Reputation: 1892
I know its already solved, but thought I would add 2 cents
Code:
while read -r name file path
do
    echo mv $name $path/${path//\//_}${file#AAA_000}
done< <(find * -name 'AAA_000*' -printf '%p %f %h\n')
 
  


Reply

Tags
shell


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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Add path to $PATH variable alaios Suse/Novell 2 04-19-2009 08:41 AM
add to path FrankBlourtango Linux - Newbie 2 08-06-2007 01:31 PM
Do you add to the path line or make a new path in /etc/profile? M$ISBS Linux - Newbie 2 12-13-2006 02:14 PM
add to path towel401 Linux - Newbie 3 07-23-2006 10:38 PM
How do I add something to my $PATH? iammeuno Linux - Software 1 12-18-2003 04:02 PM


All times are GMT -5. The time now is 05:52 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration