Linux: The most recent file in a directory, excluding directories and. files

I would like to find the last modified file in a directory, excluding hidden files (those starting with.), And also excluding directories.

This question is going in the right direction, but not quite what I need:

Linux: last file in the directory

The key here is to exclude directories ...

+4
source share
4 answers

Like the answer, except without -A

ls -rt | tail -n 1 

See man ls for more information.

To exclude directories, we use the -F option to add "/" to each directory and then filter for those who do not have "/":

 ls -Frt | grep "[^/]$" | tail -n 1 
+8
source

This does what you want, with the exception of directories:

 stat --printf='%F %Y %n\n' * | sort | grep -v ^directory | head -n 1 
+2
source

same, not very clean, but: ls -c1 + tail, if you want => ls -c1 | tail -1 ls -c1 | tail -1

 $ touch a .b $ ls -c1 a $ ls -c1a a .b $ touch d $ ls -c1 d a $ ls -c1a . d a .b .. $ touch .b $ ls -c1a .b . d a .. 

As you can see, without a arg, only visible files are displayed.

0
source

probably matches the answer in another post, but with a slight difference (excluding directories) -

 ls --group-directories-first -rt | tail -n 1 
0
source

All Articles