I have been using almost compulsively
ls -lrt
for a long time now. As per the
ls man page, this command lists the files of the current directory, with the latest files at the end, so that they are the ones that show up just above your next command-line. This is very convenient to work with, hmmm, not-so-well-organized directories, because it just shows what you're working on, and you can safely ignore the rest. Typical example is a big Downloads directory, for instance, but I use it everywhere. I quickly used
alias lrt="ls -lrt"
to make it easier, but... I though I might have as well a reliable way to
directly use what I saw. So I came up with the following shell function (
zsh, but probably works with most Bourne-like shells):
lrt() {
ls -lrt "$@"
lrt="$(ls -rt "$@" | tail -n1)"
}
This small function runs
ls -lrt
as usual, but also sets the
$lrt
shell variable to the latest file, so you can use it in your next commands ! Especially useful for complex file names. Demonstration:
22:05 vincent@ashitaka ~/Downloads lrt
[...]
-rw-r--r-- 1 vincent vincent 1490027 Apr 2 15:44 k.zip
-rw-r--r-- 1 vincent vincent 668566 Apr 3 22:05 1-s2.0-S0013468617305947-main.pdf
22:06 vincent@ashitaka ~/Downloads cp -v $lrt ~/nice-paper.pdf
'1-s2.0-S0013468617305947-main.pdf' -> '/home/vincent/nice-paper.pdf'
This saves typing the name of the
1-s2.0-S0013468617305947-main.pdf
: in this case, automatic completion doesn't help much, since many files in my
Downloads
directory start with the same orefix... I hope this helps !