I need to keep only the 4 most recent file in a directory. I used the following command:
rm -f `ls -t $SOMEDIR/archive/* | awk 'NR>4'`
That's working. I'd like to use a variable NUM_TO_KEEP instead of the '4', so I tried the following:
NUM_TO_KEEP=4
rm -f `ls -t $SOMEDIR/archive/* | awk -v num=$NUM_TO_KEEP 'NR>$num'`
And I get the following error: awk: syntax error near line 1 awk: bailing out near line 1
I also tried a simple ls | awk -v var="foo" 'NR>4'
And I get the same error. I am trying this on linux and solaris and get the same error for both OS. Any idea on what I'm doing wrong? My question is regarding the error I get while using the '-v' option, as soon as I set a variable next to the -v option I get an error. (ls | awk -v 'NR>4' works, ls | awk -v="foo" 'NR>4' gives an error)