This works pretty well:
But I admit I don't get the $1=$1.
tcsh, etc:
foreach file (`find , -name "* *"`)
mv "$file" "`echo $file | awk 'BEGIN {OFS="-"} $1=$1'`"
end
bash, etc:
find . -name "* *"|while read file
do
mv "$file" "`echo "$file"| awk 'BEGIN {OFS="-"} $1=$1'`"
done
Subscribe to:
Post Comments (Atom)
I would suggest modifying the bash example to use $( ) instead of backticks (``), as it's much more readable. Also, the use of $( ) is not only more readable for humans as we don't have to worry about mixing up the back-ticks and single-quotes (''), but it is "safer" in bash scripting and allows for multiple levels of nesting.
ReplyDeleteThe $1=$1 part is necessary due to the structure of the awk scripting language. It's pretty common in one-liners.
I've also seen this done (probably done it myself, I would think) with sed or with tr instead of awk. Personally, I think I would use tr, as it's much lighter weight for this kind of thing and can reduce the complexity (i.e. number of commands executed and sub-shells spawned) for each trip through the loop. Of course, that would work with both the csh/tcsh and sh/ksh/bash/zsh/etc. shells.