Remove the files that have less than certain lines

Hi all,

I'm a newbie and I'm sorry if my question is too simple.
I'm having problem to delete the files that have less than certain lines, say 16.

#!/bin/tcsh
set filen = `sh -c 'ls *csv 2> /dev/null'`
foreach fil (${filen})
      if [[ `wc -l ${filen}` < 16 ]]; then
         rm -f ${filen}
      fi
end
exit

here's the error message:

16: No such file or directory.

Thanks in advance for your help.

Hi.

Seems you've mixed up some sh and csh ( if [[ `wc -l ${filen}` < 16 ]] is quite Bourne shell :slight_smile: (except the < part)).

#!/bin/csh
set filen = `sh -c 'ls T 2> /dev/null'`
foreach fil (${filen})
      if ( `wc -l < ${fil}` < 16 ) then
         rm -f ${fil}
      endif
end

Standard fare: See Csh Programming Considered Harmful and Top Ten Reasons not to use the C shell.

1 Like

ohh! I just searched for the if statement and didn't pay attention to the shell!
Thanks very much scotten for sorting this out and also the useful links.