Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need

Status
Not open for further replies.

tom781

Technical User
Jun 5, 2015
2
US
I'm a newbie. I have a question:
The following script is supposed to list the first(oldest) 40 archived directories and remove them one by one, but I do not think it's correct. Seems that it will remove all the directories, not just the 40 mentioned. Please advise.

setenv archiveDir <someArchivePath>
foreach x (`ls -t ${archiveDir}/ | awk 'BEGIN {k=40} {k--; if(k<0) {print $0}}'`)
rm -rf ${archiveDir}/${x}
echo ${archiveDir}/${x}
end
 
Hi

Actually it does the reverse : skips the first 40 lines and outputs all the following. To output the first 40 lines :
Code:
[gray]# the shortest[/gray]
[blue]master #[/blue] awk '[navy]NR[/navy][teal]<=[/teal][purple]40[/purple]'

[gray]# the fastest[/gray]
[blue]master #[/blue] awk '[purple]1[/purple][teal];[/teal][navy]NR[/navy][teal]==[/teal][purple]40[/purple][teal]{[/teal][b]exit[/b][teal]}[/teal]'


Feherke.
feherke.ga
 
OK. Now it makes sense. Thanks alot for the quick reply.
 
Hi

By the way, the above "fastest" solution is just the fastest Awk way. There is a dedicated tool for this task : [tt]head[/tt].

Feherke.
feherke.ga
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top