LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   add path to flenames (http://www.linuxquestions.org/questions/programming-9/add-path-to-flenames-863817/)

aai 02-19-2011 10:20 PM

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

fordeck 02-19-2011 11:04 PM

You might want to look at the following command:

dirname

Or

basename

Regards,

Fordeck

ntubski 02-19-2011 11:50 PM

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


aai 02-20-2011 09:57 AM

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

ntubski 02-20-2011 11:07 AM

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


aai 02-20-2011 11:22 AM

Yeaaahhh!!
Thank you very much!!!

grail 02-20-2011 07:55 PM

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')



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