views:

32

answers:

2

It's been a while since i used bash:/

I wish to find all .docx files, and append them a string. This is my current code, witch has a little bug:find -name '*.docx -execdir mv {} {}$string \; Files are renamed, but string is added like this filename.docx_string and not like that filename_string.docx.

Any ideas? Thank you:)

A: 

This will do the trick and descend into the subdirectories.

find ./ -name "*.docx" -print | while read i; do mv "$i" `echo "$i" | sed -e 's/\.docx/_stringhere\.docx/'`; done

The sed -e portion will perform a regex substitution for .docx.

Buggabill
for is not an option, since it doesn't check recursive. My files will be in sub folders. Will check what sed -e ... does tho! Thank you for your replay.
krizajB
Edited to accommodate what you are looking for.
Buggabill
It might be a litter better if you anchor the extension to the end of the string: `'s/\.docx$/_stringhere\.docx/'`. Or you can use this variation on **ghostdog74's** [answer](http://stackoverflow.com/questions/3468949/find-all-docx-files-add-sufix-with-bash/3469819#3469819): `mv "$i" "${i/%.docx/${string}.docx}"`
Dennis Williamson
A: 

bash 4

shopt -s globstar
for file in **/*.docx
do
  echo mv "$file" "${file%.docx}${string}.docx"
done
ghostdog74