开发者

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

开发者 https://www.devze.com 2023-04-13 04:41 出处:网络
I would like to find the most recently changed file in a directory, excluding hidden files (the ones that start with .) and also excluding directories.

I would like to find the most recently changed file in a directory, excluding hidden files (the ones that start with .) and also excluding directories.

This question is headed in the right direction, but not e开发者_StackOverflowxactly what I need:

Linux: Most recent file in a directory

The key here is to exclude directories...


Like the answer there except without -A

ls -rt | tail -n 1

Look at man ls for more info.

To make it exclude directories, we use the -F option to add a "/" to each directory, and then filter for those that don't have the "/":

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


This does what you want, excluding directories:

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


same one, not very clean but: ls -c1 + tail if you want => 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 listed.


probably the same as the answer in the other post but with a small difference (excluding directories) -

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

精彩评论

暂无评论...
验证码 换一张
取 消