cwd
cwd

Reputation: 54796

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

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 exactly what I need:

Linux: Most recent file in a directory

The key here is to exclude directories...

Upvotes: 6

Views: 4691

Answers (4)

ankith13
ankith13

Reputation: 391

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

Upvotes: 0

Diego Torres Milano
Diego Torres Milano

Reputation: 69228

This does what you want, excluding directories:

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

Upvotes: 3

oadams
oadams

Reputation: 3087

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

Upvotes: 11

Aif
Aif

Reputation: 11220

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.

Upvotes: 0

Related Questions