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 ?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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.
Method 2
find . -type f -newermt '1/30/2017 0:00:00'
This will find all files modified after a specific date.
Method 3
ls -ltr | grep "`date | awk '{print $2" "$3}'`"
Method 4
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}'
Method 5
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
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0