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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

delete directory by ls head 2

Status
Not open for further replies.

GROKING

Programmer
Mar 8, 2005
26
0
0
US

Hello,

I have two directories and want to delete the top in a shell script.
2010_01_18
2010_01_20

I do an ls ls | head -n 1 which displays 2010_01_18, this is the directory I want to delete.

I try rm -rf < ls | head -n 1
I try ls | head -n 1 | rm -rf

neither deletes the directoy

I cant delete by date -mtime because the top dir gets modified to a later date that the second dir.

any help is appreciated!!
thx
 
As long as you're sure you want to delete the very first file/directory:

rm -rf `ls | head -n 1`
 
If any other directory gets created in that directory with a name that sorts higher, you might end up deleting the wrong directory. Maybe add some pattern matching and do it this way (assuming your directory name matches the format YYYY_MM_DD).
Code:
rm -rf `ls -1 20[0-9][0-9]_[01][0-9]_[0-3][0-9] | head -1`

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top