0

Hi,

Can someone create a short code that will rename a files extension in a sub directory using a command line option -r?

So if i have a file a.txt in folder called loc. I enter this "change -r txt doc" and the file will become a.doc in the loc sub directory (as in loc/a.doc).

I need to add this to a code of mine. Also the reason why I want to use a case statement is because I will be adding more options in the future.

3
Contributors
4
Replies
5
Views
6 Years
Discussion Span
Last Post by JonSmith
0

Using bash: This worked fine for my baby test. I can't see any need for the case statement. Of course you will need to poke it into shape to do your particular task.

change() {
  mv $1 ${1%$2}$3
}
for f in $(find $toplocation -iname '*.txt' -print) ; do
  change $f txt doc
done

Edited by griswolf: n/a

0

Using bash: This worked fine for my baby test.


But it will fail in several places if any filenames contain whitespace.

0

find ... -print0 | xargs -0 somecommand works fine with whitespace in paths. However you do have to muck about with somecommand if you want it to have parameters other than the name of the one "found" file at a time. One option is to use a sub-shell to pass parameters as environment variables: ( cmdarg1=1;cmdarg2='TWO'; find ... -print0 | xargs -0 command-uses-2-params ) Note the parentheses around the entire line

Edited by griswolf: n/a

0

Thanks for the help guys. I should be able to getting going with this.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.