Click to See Complete Forum and Search --> : Need a script to remplace files (Was: script)


meh1587
09-25-2003, 04:26 PM
Help! I need a script that will replace all spaces with _ for all files in a directory. It would be nice to have an explanation of the script too so maybe I could learn a little something. :)

micio
09-26-2003, 03:21 AM
for file in *; do { sed "y/ /_/" $file > $file.new; } done;

explanation:
sed -> man sed :-)
"for file in ..." is a bash script quite self explanatory

You'd better to learn bash scripting and tools for the next time ;-), it's not difficult, try with "Teach yourself shell programming in 24 hours" and then "advanced bash scripting guide" (keep it as a reference). You won't learn everything but it is worth, now I'm using scripting everywere (once I used to write C programs) even when I'm on a M$ platform (with cygwin): the line of code above was tested on cygwin running on w2k!!