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

getting the x most recent entries from a directory

Status
Not open for further replies.

KarveR

MIS
Dec 14, 1999
2,065
GB
without a lot of typing (i'm real lazy about that).

Normally,
ls -ltA /somedir |head -x

what if you could just type
lht /somedir 5

well, you can - save the following to your /usr/local/bin as lht with exeute perms (written on redhat ES unsure of outcome for others).
Code:
#!/bin/sh
#clear
if [ "$#" -lt "2" ] ; then
        nonums="$(echo $1 | sed 's/[0-9]//g')"
        if [ ! -z "$nonums" ] ; then
                ls -ltA $1 --color=always|head
        else
                num=`expr $1 + 1`
                if [ $num -eq 1 ] ; then
                        num=6
                fi
                ls -ltA --color=always|head -$num
        fi
else
        nonums="$(echo $1 | sed 's/[0-9]//g')"
        if [ ! -z "$nonums" ] ; then
                num=`expr $2 + 1`
                if [ $num -eq 1 ] ; then
                        num=6
                fi
                ls -ltA $1 --color=always|head -$num
        else
                num=`expr $1 + 1`
                if [ $num -eq 1 ] ; then
                        num=6
                fi
                ls -ltA $2 --color=always|head -$num
        fi

fi

open to suggestions, improvements and portability options, but hopefully it will be helpful to someone else.

PS, its not fussy which way round you specify, number of entries 1st or 2nd, if you give no directory, it takes the current, and if you give no number you get 5. neither need be specified.

lht 5
lht /somedir
lht 20 /somedir
lht /somedir/sub-dir/whatever 12
lht



______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top