case
case

Reputation: 305

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!

Upvotes: 4

Views: 1951

Answers (2)

user2404501
user2404501

Reputation:

In zsh: less dir/*(.om[1])

dir/* is a regular glob.

The . qualifier restricts to regular files.

om means order by modification time, newest first.

[1] means just expand the first filename.

It's probably better without the [1] - just pass all the filenames to less in the om order. If the first one satisfies you, you can hit q and be done with it. If not, the next one is just a :n away, or you can search them all with /*something. If there are too many, om[1,10] will get you 10 newest files.

Upvotes: 1

fedorqui
fedorqui

Reputation: 290065

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

Upvotes: 6

Related Questions