1

I was thinking of adding a convenience alias for myself. As of right now I have an alias called ll for ls -lha.

However, I noticed there were several occasions where I wanted to list the latest three files in a directory. This can be tedious in the console, especially when the directory contains hundreds of files.

Usually, one can do:

ll -1t | head -3

However, this is still a bit tedious, as it requires filtering. ll prints the total file size as top line. And then it followed either by . or .. or both, which would make sense to filter out as well. Is there a nice way to do that?

Is it possible to make dynamic aliases, say lll{0-n}, so, if I type lll7 it would list the 7 latest files in a directory, starting with the latest one on top?

3
  • 1
    Alias works by simply replacing text. More information here. Commented Mar 11, 2020 at 1:18
  • Alright, fair. That means I am limited to a hard coded limit, which is fine, I guess. But I am still unsure about the filtering issue.
    – n00b.exe
    Commented Mar 11, 2020 at 1:28
  • Hardcoded? No. One can create a shell function or a script that takes argument(s) and applies logic. My point is the alias is not the right tool. Commented Mar 11, 2020 at 5:18

1 Answer 1

0

In bash, you can create this function (put it in your ~/.bashrc, then source it):

function lll { ls -at1 | grep -v -e '^\.$' -e '^\.\.$' | head -$1 ; }

To display the most recent 7 files from newest to oldest, not including . nor .., type this at the bash prompt:

lll 7

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .