简体   繁体   中英

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!

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM