Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fill in the latest used file in the current directory

Tags:

zsh

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?

like image 958
Gerhard Avatar asked Feb 06 '15 15:02

Gerhard


People also ask

How do I find recent files in Linux?

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.

How do I find the current directory in Unix?

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)

How do I copy the latest file in Linux?

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.


1 Answers

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.

like image 198
hchbaw Avatar answered Sep 28 '22 07:09

hchbaw