Mine the Harvest

lslt Bash Function

by on Jan.26, 2013, under Linux

I’m always typing: ls -lhatr | tail

I decided this was getting old. ‘Alias’ was not going to really do what I wanted, so I made a little bash function instead, which can be put in your .bashrc, etc. Simple – but handy. And (of course) maintains colors.

The full script version :

#!/bin/bash
# List the last N items in specified directory in
# reverse time order (newest on bottom).
#
# Note however, the 'alias' commanad alone will not correctly handle args.

if [ $# -lt 1 ]
then
        echo "Usage: $0 /foo N (optional)"
        echo "Will perform ls -lhatr | tail -N"
        exit 1  
fi

dir=$1
echo "Listing $dir . . . "
ls -lhatr --color=always $dir | tail -$2

But you can achieve the same thing with a bash function, perhaps in your ~/.bashrc e.g.:

# lslt () { command ls -lhatr --color=always "$1" | tail -"$2"; }
#
# Or even fancier . . .

lslt () {
if [ $# -lt 1 ]
then
        #type lslt //Works, but a nicer way is . . .
        echo "Usage: lslt /foo N (optional)"
        echo "Will perform ls -lhatr | tail -N"
else
        command ls -lhatr --color=always "$1" | tail -"$2";
fi
}
No comments for this entry yet...

Leave a Reply

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit our friends!

A few highly recommended friends...