views:

56

answers:

2

Here is my snippet:

#!/bin/sh

for fname in *
do
      if [! -d "$fname"]
      then
          WHAT CAN I DO
      fi
done

All new named files has to have this format O.fname - as you see preceeded by O. Can you please help? I know i have to use mv somewhere.

+2  A: 

Files are renamed using mv, so just do

mv "$fname" "O.$fname"

in the loop.

sepp2k
can u not just use rename instead of mv ?
thegeek
@thegeek: You can use rename if a) you know it is installed and b) you know which one is installed. There's one tool named rename which accepts a perl regex and a list of file and another which accepts two strings and a list of files. Neither of them is installed on all systems.
sepp2k
+2  A: 

Just use the mv inside the loop. Also, in the if statement, there is a space between the [ and the !

for fname in *
do
    if [ ! -d "$fname" ]
    then
        mv "$fname" "O.$fname"
    fi
done
Ed
sepp2k's answer is better: `mv "$fname" "O.$fname"` protects against spaces in file names.
Bolo
that's right. I always forget that.
Ed
...and a space before the `]` (as you've shown).
Dennis Williamson