92

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 ?

  • 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). – Gilles Mar 24 '11 at 20:46
  • 3
    So how did the date with ls go? Did you two hit it off? – Garrett Albright Dec 4 '13 at 0:17
  • 3
    With zsh: ls -1 -- *(.m-1) – don_crissti Aug 8 '15 at 15:52
119

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.

  • 3
    The very thing I would have said. There's no reason to limit yourself to ls here, Luc. – Warren Young Mar 24 '11 at 16:04
  • 12
    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). – Gilles Mar 24 '11 at 20:44
  • 3
    Note the minus sign: find . -mmin -5 – user7543 May 17 '11 at 8:32
  • and -maxdepth 1 can be increased to any n value to search files under subdirectory level too – anshuman Sep 12 '12 at 5:42
  • 2
    for me it works like this: find . -type f -newermt '1/15/2012 18:09:00' – woohoo Jan 5 '13 at 23:16
8
find . -type f -newermt '1/30/2017 0:00:00'

This will find all files modified after a specific date.

1
ls -ltr | grep "`date | awk '{print $2" "$3}'`"
  • 5
    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 '15 at 15:58
  • 1
    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) – G-Man Jun 4 '15 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. – G-Man Jun 4 '15 at 1:37
  • ls -ltr | grep "$(date | awk '{print $2,$3}')" – SergioAraujo May 22 '18 at 12:21
0

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}'
  • For many reason, it's not a good idea to parse ls output. And not everyone speaks English :-) – Luc M Apr 18 '18 at 20:49

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.