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)?
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.
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.
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.