Home Shell script to find files after certain modified date and sort them in order of modified date
Reply: 0

Shell script to find files after certain modified date and sort them in order of modified date

user1830
1#
user1830 Published in April 21, 2018, 4:01 am

I am trying to find all files after certain modified date from a directory in the sorting order of modified date using shell script. This is one of the part of my current shell script.

Condition contains two steps: 1) Fetch files from a directory after certain modified date. 2) sort the files by modified date.

Current directory contains files like this:

Mar 28 11:14 file_H_1
Apr  2 16:37 file_K_2
Apr  1 21:43 file_H_3
Apr 16 19:16 file_H_4
Apr 16 21:00 file_H_5
Apr 16 12:00 file_L_6
Apr  9 14:08 file_B_7
Apr  4 00:39 file_H_8
Apr  4 00:39 file_H_9
Apr  4 00:39 file_C_10
Apr  4 00:39 file_H_11
Mar 27 14:39 file_H_12

I want final output as a list of files with modified date after "2018-04-09 00:00:00" and in order by modified date. OUTPUT should be:

file_B_7
file_L_6
file_H_4
file_H_5

I tried this:
1) For getting files after certain modified date

find . -type f -newermt "2018-04-09 00:00:00"
OUTPUT of this:
file_L_6
file_H_5
file_B_7
file_H_4

2) order the files on modified date

ls -lt
OUTPUT of this
Apr 16 21:00 file_H_5
Apr 16 19:16 file_H_4
Apr 16 12:00 file_L_6
Apr  9 14:08 file_B_7
Apr  4 00:39 file_H_8
Apr  4 00:39 file_H_9
Apr  4 00:39 file_C_10
Apr  4 00:39 file_H_11
Apr  2 16:37 file_K_2
Apr  1 21:43 file_H_3
Mar 28 11:14 file_H_1
Mar 27 14:39 file_H_12

But I am struggling to combine these two conditions.

I tried this also but it is sorting on file name not on modified date:

find . -type f -newermt "2018-04-9 00:00:00" | sort -n | while read file_name; do
echo file=$file_name
done
    OUTPUT of this:
    file_B_7
    file_H_4
    file_H_5
    file_L_6

Please suggest some solution.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.304439 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO