Looking for a command that will return the single most recent file in a directory.
Not seeing a limit parameter to ls...
Join Stack Overflow to learn, share knowledge, and build your career.
|
Looking for a command that will return the single most recent file in a directory. Not seeing a limit parameter to ls... |
|||
Not very elegant, but it works. |
|||||||||||||||||||||
|
This command actually gives the latest modified file in the current working directory. |
|||||||||||||||||||||
|
|
This is a recursive version (i.e. it finds the most recently updated file in a certain directory or any of its subdirectory)
Edit: use |
|||||||||||||||||
|
|
I use:
It gives me just the file name, excluding folders. |
|||||
|
|
The other solutions do not include files that start with This command will also include
|
|||||||||
|
|
I like |
|||||||||||||||||
|
|
Shorted variant based on dmckee's answer:
|
|||
|
|
|
I personally prefer to use as few not built-in
or to get the name of the oldest file
(Mind the space between the two '<' marks!) If the hidden files are also needed -A arg could be added. I hope this could help. |
|||||||||||||
|
|
The find / sort solution works great until the number of files gets really large (like an entire file system). Use awk instead to just keep track of the most recent file:
|
||||
|
|
Will show the last modified item in the folder. Pair with
|
|||||
|
|
try this simple command
If you want file name - last modified, path = /ab/cd/*.log If you want directory name - last modified, path = /ab/cd/*/ |
||||
|
|
|
Recursively:
|
|||
|
|
|
All those ls/tail solutions work perfectly fine for files in a directory - ignoring subdirectories. In order to include all files in your search (recursively), find can be used. gioele suggested sorting the formatted find output. But be careful with whitespaces (his suggestion doesn't work with whitespaces). This should work with all file names:
This sorts by mtime, see man find:
So just replace |
|||||
|
|
Finding the most current file in every directory according to a pattern, e.g. the sub directories of the working directory that have name ending with "tmp" (case insensitive):
|
|||
|
|
|
A note about reliability: Since the newline character is as valid as any in a file name, any solution that relies on lines like the With GNU
(add If you want to limit to regular files (disregard directories, fifos, devices, symlinks, sockets...), you'd need to resort to GNU With bash 4.4 or newer (for
Or recursively:
Best here would be to use Newest regular file in the current directory:
Including hidden ones:
Second newest:
Check file age after symlink resolution:
Recursively:
Also, with the completion system ( So: vi Ctrl+Xm Would make you edit the newest file (you also get a chance to see which it before you press Return). vi Alt+2Ctrl+Xm For the second-newest file. vi *.cCtrl+Xm for the newest vi *(.)Ctrl+Xm for the newest regular file (not directory, nor fifo/device...), and so on. |
|||
|
|
|
I needed to do it too, and I found these commands. these work for me: If you want last file by its date of creation in folder(access time) :
And if you want last file that has changes in its content (modify time) :
|
||||
|
|
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
watch -n1 'ls -Art | tail -n 1'- shows the very last files – YumYumYum Jul 5 '12 at 19:52