How to get only files created after a date with ls?

  • 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 ?

    `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).

    So how did the date with ls go? Did you two hit it off?

    With `zsh`: `ls -1 -- *(.m-1)`

  • dogbane

    dogbane Correct answer

    10 years ago

    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.

    The very thing I would have said. There's no reason to limit yourself to `ls` here, Luc.

    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).

    Note the minus sign: `find . -mmin -5`

    and -maxdepth 1 can be increased to any n value to search files under subdirectory level too

    This is great, except it lists the current directory `.` also. I had to run this through `tail` to strip that. Is there a way within `find` to chop one?

    Found my answer: Use `-mindepth 1` to ensure that the current directory `.` does not get listed.

    for me it works like this: find . -type f -newermt '1/15/2012 18:09:00'

    `-newermt` was not available on the last server I was working on. `-newer` does accept files/directories and will only list files newer than that file/directory, though.

    That's a lot of work to calculate how long ago an arbitrary date was

License under CC-BY-SA with attribution


Content dated before 6/26/2020 9:53 AM