I am looking for a way to quickly access the latest file/directory on the command line, preferably in ZSH.
ls -d *(om[1])
Gives me just that, and if I want to use to with a command, e.g. less *(om[1])
This works as well.
However, it is tedious to type all the brackets, and I use this a lot - hence I am looking for a way to create a shortcut for this string.
I've created a function in the .zshrc-file
lf(){
ls -d *(om[1])
}
, which I can use like this:
less <$(lf)
less <`lf`
, but I find this still less than ideal.
less |lf
does not work.
Is there a way to quickly access the latest file without the use of "hard to type characters"? Ideally, it would just be something along the lines of
less LATEST
Any ideas?
Finding Files Modified on a Specific Date in Linux: You can use the ls command to list files including their modification date by adding the -lt flag as shown in the example below. The flag -l is used to format the output as a log. The flag -t is used to list last modified files, newer first.
sort -n -r |head -1 | cut -f2 - date orders the directory and outputs the entire name of the most recently modified (even if containing some space as cut default delimiter tab)
Running ls -t /path/to/source | head -1 will return the newest file in the directory /path/to/source so cp "$(ls -t /path/to/source | head -1)" /path/to/target will copy the newest file from source to target . The quotes around the expression are needed in order to deal with file names that contain spaces.
You could use the _most_recent_file (^Xm)
.
_most_recent_file (^Xm)
Complete the name of the most recently modified file matching the pattern on the command line (which may be blank). If given a numeric argument N, complete the Nth most recently modified file. Note the completion, if any, is always unique.
--
zshcompsys(1) BINDABLE COMMANDS
So, we can get the most recent file with typing CTRL-x-m. For example:
% less ;# typing `CTRL-X m` here, we could get:
% less newest-file-or-directory
And we could specify some patterns here, so for example:
% less *.log ;# when I want the newest *.log:
% less newest.log
It is necessary to have some autoload -Uz compinit; compinit
in the ~/.zshrc
though.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With