Linux: find files created/modified after, before and between a timestamp
Find files created or modified after, before and between a timestamp!
in Linux modification timestamp is the creation timestamp if file has not been modified since creation, Linux find command allows us to perform search using file modification timestamp, to search using modification time we use the -newermt option.
Example: Find all files in a directory without caring about timestampsfind /csv/ -name 'out*' | sort -n
/csv/out_2022_Apr_21_13_54.csv
/csv/out_2022_Apr_21_14_03.csv
/csv/out_2022_Apr_21_14_07.csv
/csv/out_2022_Apr_21_15_02.csv
/csv/out_2022_Apr_21_16_02.csv
/csv/out_2022_Apr_21_17_02.csv
/csv/out_2022_Apr_21_18_02.csv
/csv/out_2022_Apr_21_19_02.csv
/csv/out_2022_Apr_21_20_02.csv
/csv/out_2022_Apr_21_21_03.csv
/csv/out_2022_Apr_21_22_04.csv
/csv/out_2022_Apr_21_23_07.csv
/csv/out_2022_Apr_22_00_08.csv
/csv/out_2022_Apr_22_01_09.csv
/csv/out_2022_Apr_22_02_10.csv
/csv/out_2022_Apr_22_03_10.csv
/csv/out_2022_Apr_22_04_10.csv
/csv/out_2022_Apr_22_05_10.csv
/csv/out_2022_Apr_22_06_10.csv
/csv/out_2022_Apr_22_07_10.csv
/csv/out_2022_Apr_22_08_10.csv
/csv/out_2022_Apr_22_09_10.csv
/csv/out_2022_Apr_22_10_10.csv
/csv/out_2022_Apr_22_11_10.csv
Example: Find all files in a directory created since start of current date
In this example the value passed to the -newermt option is replaced by the output of the date command, a way to automate tasks such as search only for current daydate +"%Y-%m-%d"
2022-04-22
We can notice that it returned far less files because it returned only files of current datefind /csv/ -name 'out*' -newermt "$(date +"%Y-%m-%d")" | sort -n
/csv/out_2022_Apr_22_00_08.csv
/csv/out_2022_Apr_22_01_09.csv
/csv/out_2022_Apr_22_02_10.csv
/csv/out_2022_Apr_22_03_10.csv
/csv/out_2022_Apr_22_04_10.csv
/csv/out_2022_Apr_22_05_10.csv
/csv/out_2022_Apr_22_06_10.csv
/csv/out_2022_Apr_22_07_10.csv
/csv/out_2022_Apr_22_08_10.csv
/csv/out_2022_Apr_22_09_10.csv
/csv/out_2022_Apr_22_10_10.csv
/csv/out_2022_Apr_22_11_10.csv
Find all files in a directory created since start of current day but not newer than 9 am of current day
In this example we used the -newermt option twice, the second -newermt option has the ! in-front, this means negate the option from “newer than” to “no newer than”find /csv/ -name 'out*' -newermt "$(date +"%Y-%m-%d")" ! -newermt "$(date +"%Y-%m-%d 09")" | sort -n
/csv/out_2022_Apr_22_00_08.csv
/csv/out_2022_Apr_22_01_09.csv
/csv/out_2022_Apr_22_02_10.csv
/csv/out_2022_Apr_22_03_10.csv
/csv/out_2022_Apr_22_04_10.csv
/csv/out_2022_Apr_22_05_10.csv
/csv/out_2022_Apr_22_06_10.csv
/csv/out_2022_Apr_22_07_10.csv
/csv/out_2022_Apr_22_08_10.csv
Example: How to see a file timestamps
We can use the stat command to see the timestamps of a file
- access: when accessed last time (read/write).
- modify: when content changed.
- changed: when content or rights changed.stat /csv/out_2022_Apr_22_08_10.csv
File: '/csv/out_2022_Apr_22_08_10.csv'
Size: 151849 Blocks: 304 IO Block: 4096 regular file
Device: fd00h/64768d Inode: 2101150 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 1014/kpatronas) Gid: ( 1014/kpatronas)
Context: unconfined_u:object_r:user_home_t:s0
Access: 2022-04-22 10:34:15.162655553 +0300
Modify: 2022-04-22 08:10:22.079922129 +0300
Change: 2022-04-22 08:10:22.079922129 +0300
Birth: -
I hope you found this article useful! :)