Append string to all the files inside a directory excluding subdirectories and .zip files

Hii,

Could someone help me to append string to the starting of all the filenames inside a directory but it should exclude .zip files and subdirectories.

Eg.
file1: test1.log
file2: test2.log
file3 test.zip
After running the script
file1: string_test1.log
file2: string_test2.log
file3: test.zip

Thanks in advance,

Please use code tags as required by forum rules!

Any attempts from your side?

---------- Post updated at 13:06 ---------- Previous update was at 13:00 ----------

Anyhow, with a recent bash - unfortunately you don't mention the system nor the shell you use - you could try:

for FN in !(*.zip) ; do [ -d "$FN" ] && continue; echo mv $FN "String_$FN"; done

after setting shopt -s extglob .
If that's not available, try

for FN in *; do [ -d "$FN" ] || [ ${FN#*.} == "zip" ] && continue; echo mv $FN "String_$FN"; done

In any case, remove echo when happy with what you see...

1 Like

Thank you very much Rudic...this worked great

---------- Post updated 04-23-15 at 12:16 PM ---------- Previous update was 04-22-15 at 04:45 PM ----------

Hey, this worked fine when I run through command line,
command:

for FN in !(*.zip)  ; do [ -d "$FN" ] && continue; mv $FN "appIn1a_$FN"; done

but it gives
syntax error near unexpected token `('

when I use this same in bash script.

Please suggest the changes

Try putting:

shopt -s extglob

At the beginning of your script.

1 Like

Thank you very muchh...it worked:)