155

With the ls command, is it possible to show only the files created after a specific date, hour...?

I'm asking it because I have a directory with thousand of files.

I want so see all files that were created since yesterday.

I use ls -ltr but I have to wait to see all files...

There is an equivalent of DIRECTORY/SINCE=date from OpenVMS ?

2
  • 1
    ls lists files, but it doesn't offer much to select what files to list or the output format. Use globbing (shell wildcards) or find when you want to select files by name or metadata (e.g. date). Mar 24, 2011 at 20:46
  • 4
    With zsh: ls -1 -- *(.m-1) Aug 8, 2015 at 15:52

5 Answers 5

198

You can use the find command to find all files that have been modified after a certain number of days.

For example, to find all files in the current directory that have been modified since yesterday (24 hours ago) use:

find . -maxdepth 1 -mtime -1

Note that to find files modified before 24 hours ago, you have to use -mtime +1 instead of -mtime -1.

10
  • 3
    The very thing I would have said. There's no reason to limit yourself to ls here, Luc. Mar 24, 2011 at 16:04
  • 30
    With GNU find, there are other possibilities. -mmin 5 lists files modified in the last 5 minutes. -newermt "2011-02-27 13:42" lists files modified since the specified date. You can use -exec ls --color -ld {} + instead of -ls to get the usual color display (if you like colored ls output). Mar 24, 2011 at 20:44
  • 5
    Note the minus sign: find . -mmin -5
    – user7543
    May 17, 2011 at 8:32
  • 1
    Found my answer: Use -mindepth 1 to ensure that the current directory . does not get listed.
    – Geoff
    Dec 27, 2012 at 15:43
  • 4
    for me it works like this: find . -type f -newermt '1/15/2012 18:09:00' Jan 5, 2013 at 23:16
66
find . -type f -newermt '1/30/2017 0:00:00'

This will find all files modified after a specific date.

1
  • 14
    This has worked for me, except I decided to go for a less ambiguous date format, e.g. find . -type f -newermt '2020-04-01 00:00:00'
    – mehov
    Mar 4, 2021 at 7:23
3
ls -ltr | grep "`date | awk '{print $2" "$3}'`"
5
  • 7
    Hi Trant! While your solution is creative in attempting to respect the question's request to use ls, parsing the output of ls is seldom safe (e.g., what happens with file names including newlines here?), and we like it for answers to not be just one-liners, but rather to explain how they work in as much detail as is relevant.
    – dhag
    Apr 10, 2015 at 15:58
  • 3
    Issues: (1) On my system, date says Jun 03, but ls says Jun  3, so this doesn’t work.  (2) A week ago, the date was May 27.  ls -l | grep "May 27" would find files modified that day, but also files modified May 27 of any other year — and files with “May 27” in their name.  (And if you think that’s a totally bogus concern, look up “Dec 10”.)  (3) And, if you managed to get date to say Jun 2, grepping for that would find Jun 20 through Jun 29, but not Jun  2 (with two spaces).  … (Cont’d) Jun 4, 2015 at 1:33
  • 1
    (Cont’d) …  (4) The OP was using ls -ltr to get the most recently modified files at the end of the listing.  If you’re grepping for a date, there’s no need to do that (except to get the May 27, 2015 files at the end of the listing, after the May 27, 2014, May 27, 2013, etc., files).  (5) In awk, print $2, $3 is equivalent to print $2" "$3, and is much easier to read — especially when there are three other levels of quotes. Jun 4, 2015 at 1:37
  • ls -ltr | grep "$(date | awk '{print $2,$3}')" May 22, 2018 at 12:21
  • Best answer in my opinion. Works and you only need to use everyday commands.
    – daniel_hck
    Jan 21, 2021 at 0:39
2

Hope this works:

ls -ltr | awk '$6 == "May" && $7 >=01 && $7 <= 31 {print $9}'

Here:

$6 indicates position of month
$7 indicates day of the month

this above command prints all the file names which are created on or after May 1st to May 31st

if you want to print date as well on the console try this

ls -ltr | awk '$6 == "May" && $7 >=01 && $7 <= 31 {print $6"-"$7,$9}'

If you want to specify path of the directory you may try this out

ls -ltr <path>| awk '$6 == "May" && $7 >=01 && $7 <= 31 {print $6"-"$7,$9}'
1
  • 2
    For many reason, it's not a good idea to parse ls output. And not everyone speaks English :-)
    – Luc M
    Apr 18, 2018 at 20:49
0

I think these ls commands are far better than using find if the additional file metrics that find (which just returns filenames) does not provide are needed. That said, all the ls solutions seem really cumbersome piping to awk. Why not just use gnu date's built in formatting instead?

ls -ltr directory/ | grep "$(date +"%b %e")" Does the same thing and requires no awk print statements or conditionals. GNU date formatting is really helpful for outputting your date exactly how you need. In this case %b matches the 3 letter month and %e is a space-padded day value matching the format of ls instead of a 0-padded day value that the default date uses.

ls -ltr directory/ | grep "$(date +"%b %e")" Example output:

-rw-r----- 1 ocams ocams       987 Sep  2 01:45 ember_status_2021-245-01_30.log.gz.closed
-rw-r----- 1 ocams ocams      1202 Sep  2 01:45 realmvm_status_2021-245-00_04.log.gz.closed
-rw-r----- 1 ocams ocams      1085 Sep  2 01:45 realmvm_status_2021-245-01_04.log.gz.closed
-rw-r----- 1 ocams ocams    312590 Sep  2 01:45 3-21-244-234712.csv.gz.closed
-rw-r----- 1 ocams ocams    925880 Sep  2 01:45 1-21-245-010728.csv.gz.closed
-rw-r----- 1 ocams ocams    310556 Sep  2 01:45 3-21-245-010238.csv.gz.closed
-rw-r----- 1 ocams ocams      1041 Sep  2 01:45 ember_status_2021-245-00_45.log.gz.closed

If you wanted to more closely match find to get exactly 24 hours you can handle the two different dates (the current day, and the previous day) with an or condition in grep: ls -ltr directory/ | grep "$(date +"%b %e")\|$(date -d -1day +"%b %e")". This is returning 25-48 hours worth of data instead of <24.

ls -ltr directory/ | grep "$(date +"%b %e")\|$(date -d -1day +"%b %e")" Example output:

-rw-r----- 1 ocams ocams    314951 Sep  1 23:45 3-21-244-231707.csv.gz.closed
-rw-r----- 1 ocams ocams    899348 Sep  1 23:45 1-21-244-230205.csv.gz.closed
-rw-r----- 1 ocams ocams    915400 Sep  1 23:45 1-21-244-231708.csv.gz.closed
-rw-r----- 1 ocams ocams    671063 Sep  1 23:45 2-21-244-231708.csv.gz.closed
-rw-r----- 1 ocams ocams    666953 Sep  1 23:45 2-21-244-230205.csv.gz.closed
-rw-r----- 1 ocams ocams       987 Sep  2 01:45 ember_status_2021-245-01_30.log.gz.closed
-rw-r----- 1 ocams ocams      1202 Sep  2 01:45 realmvm_status_2021-245-00_04.log.gz.closed
-rw-r----- 1 ocams ocams      1085 Sep  2 01:45 realmvm_status_2021-245-01_04.log.gz.closed
1
  • 1
    Welcome to the site, and thank you for your contribution. A few comments, though: (1) parsing the output of ls is discouraged for a variety of reasons, locale-dependence being one important part. (2) Although find by default only prints the filename, the printf action of find allows output of almost any file attribute, you just need to specify it explicitly. (3) Your approach is very interesting but only works for "absolute" timespans, i.e. you can't say "the last 24h", but only "the last calender day" and the like.
    – AdminBee
    Sep 3, 2021 at 10:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.