Home > Blockchain >  Find files with latest dates with find command
Find files with latest dates with find command

Time:10-01

I have a piece of code where I need to find the latest modified date file of the below mentioned 3 patterns.

array=( $(find /path -maxdepth 1 -type f \( -name "REF_DATA*" -o -name "XR_CUST_LIST*"  -o -name "PB_INSIGHTS*" \) -printf '%f\n' ))

Basically there can files of many dates at /path directory, but i need to pick the latest one. How can i achieve that?

CodePudding user response:

One option is this command line (to keep your first approach) :

array=( $(find /path -maxdepth 1 -type f \( -name "REF_DATA*" -o -name "XR_CUST_LIST*"  -o -name "PB_INSIGHTS*" \) -printf '%c %f\n' ))|sort |tail -n 1

Focus on the part added/updated :

-printf '%c %f\n' ))|sort |tail -n 1
  • add %c in printf option add the update time of the file
  • add sort to sort this list by this date
  • add tail -n1 to have the last element of this list which is the last updated file here
  • Related