Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I ALIAS --> "less" the latest file in a directory?

Just wondering how could I less the latest log file in a directory in Linux? I'm after a oneliner, possibly considering an alias!

like image 849
case Avatar asked Sep 13 '25 15:09

case


1 Answers

Something like this?

ls -1dtr /your/dir/{*,.*} | tail -1 | xargs less

Note that for the first block of ls I am using an answer of Unix ls command: show full path when using options

As it requires a parameter, we create a function instead of an alias. Store the following in ~/.bashrc:

my_less_func ()
{
        ls -1dtr "$1"/{*,.*} | tail -1 | xargs less
}

Source it (it is enough doing . ~/.bashrc) and call it with:

my_less_func your/path
like image 122
fedorqui 'SO stop harming' Avatar answered Sep 16 '25 05:09

fedorqui 'SO stop harming'