removing files with certain number in the first row

Hello!

I have 32000 files in a directory and want to remove those with first row beging with 0.00; file names are in numbers from 1 through 32000; I have coded the following but it gives me error:

while ( i <= 32000 )
if (head -1 $i ==0.00) rm $i
end

Well, i am sure even if this works it takes a long time. Any idea on modifying this or other methods (like find command)?

Thank you

something like?

for file in $(ls -1 *)
do
found=$(head -1 $file | grep '^0\.00')
if [ "$found" ]; then
rm -f $file
fi
done

Thanks! but it gives me an error, i.e Variable syntax?!

Don't do that!

If it gives you an error, show the error!!!

Illegal variable name!! This is the only message shows up...

I think this would work: -

grep ^0.00 * |  cut -d":" -f1 | xargs -i rm -f {}

---------- Post updated at 09:58 PM ---------- Previous update was at 09:43 PM ----------

Actually with 32000 files this would probably be safer: -

rm -f $(ls | xargs -i grep -l ^0.00 {})

Dear Steadyonabix;

error message shows up for the second code, Illegal variable name!!
The first code seemingly removes the first field, but i want the file o be deleted.

thanks,

What shell are you using? I don't get these errors (with ksh/bash).


ls | while read file
do
[ "$(sed -n "/^0.00/p;q" $file 2>/dev/null)" ] && echo rm -f $file
done

(remove the echo after testing)

Anything which expands the file list as arguments to the program (like grep ... *; rm $(...) is potentially problematic if there are too many files. The -i option of xargs is not POSIX standard.

I tried

set A = (ls | xargs -i grep -l ^0.00 {})
rm -f $A

but it didn't work:(

---------- Post updated at 04:27 PM ---------- Previous update was at 04:24 PM ----------

I am using C shell!

Why are you using C Shell?

99.9% of the answers you get here will be bash or ksh based.

Can't you just add

#!/bin/bash

or

#!/bin/ksh

to the first line of your script?!

Would make life simpler for everyone!

It helps if you scrape the screen output so I can see the actual error message.

Keep in mind this was written in the korn shell, if you use a different shell you may want to break it down and check the syntax.

I should mention somewhat belatedly that this will delete all files containing any line beginning with 0.00, not just the first. So you will need to modify it if you want to narrow the scope of the files deleted.

With regard to breaking down the code: -

rm -f $(ls | xargs -i grep -l ^0.00 {})

rm -f expects a filename as an argument so we generate the list of file names in the sub shell: -

ls | xargs -i grep -l ^0.00 {}

The ls command is piped into xargs which feeds the file names into grep one at a time in the position marked by {}. The -l option for grep tells it to just output file names; as this is all done in the subshell the names of the files containing the pattern are fed back to the rm statement.

Experiment and try it a chunk at a time till you get it working, then modify it to head each file instead.

Good luck

Scottn, your code perfectly finds the file but doesn't remove it

Thanks steady for your comments

Did you remove the echo? It will echo the rm command only. If you're happy with the result, remove the echo.

This will go through the list of files and delete the appropriate ones..

ls | awk '{getline line < $0; close($0); if(line ~ /^0\.00/){ print "removing " $0;system("rm " $0)} }'

Please post the exact script, exactly what you typed to invoke the script and exactly what error messages you see.

To reiterate the first post. What Operating System and version, and Shell are you using? To echo other posts. The C shell is not an option.

BTW. Your earlier post will not work because there are space characters either side of the equals sign ... as well as syntax errors.