Alex Rivera | Logout

How can I manipulate file names using bash and sed?

Asked 2011-08-30T12:02:05.167
14

I am trying to loop through all the files in a directory.

I want to do some stuff on each file (convert it to xml, not included in example), then write the file to a new directory structure.

for file in `find /home/devel/stuff/static/ -iname "*.pdf"`;
do
  echo $file;
  sed -e 's/static/changethis/' $file > newfile +".xml";
  echo $newfile;
done

I want the results to be:

$file => /home/devel/stuff/static/2002/hello.txt

$newfile => /home/devel/stuff/changethis/2002/hello.txt.xml

How do I have to change my sed line?

Edit
Report

1 Answer

4

Are you trying to change the filename? Then

for file in /home/devel/stuff/static/*/*.txt
do
    echo "Moving $file"
    mv "$file" "${file/static/changethis}.xml"
done

Please make sure /home/devel/stuff/static/*/*.txt is what you want before using the script.

answered 2011-08-30T12:20:05.317

Your Answer