pharpan pharpan - 9 months ago 23
Linux Question

find a match in filename in a directory

I have the following files in a directory

-rw-r--r--. 1 svcamt pharpan 212 Feb 16 17:04 ManPOC_Process_Log.2017_02_16
-rw-r--r--. 1 svcamt pharpan 212 Feb 17 17:04 ManPOC_Process_Log.2017_02_17
-rw-r--r--. 1 svcamt pharpan 271 Feb 18 17:04 ManPOC_Process_Log.2017_02_18
-rw-r--r--. 1 svcamt pharpan 212 Feb 18 17:04 ManPOC_Process_Log.2017_02_19
-rw-r--r--. 1 svcamt pharpan 212 Feb 20 17:04 ManPOC_Process_Log.2017_02_20
-rw-r--r--. 1 svcamt pharpan 334 Feb 21 17:04 ManPOC_Process_Log.2017_02_21
-rw-r--r--. 1 svcamt pharpan 212 Feb 22 17:04 ManPOC_Process_Log.2017_02_22
-rw-r--r--. 1 svcamt pharpan 212 Feb 23 17:04 ManPOC_Process_Log.2017_02_23
-rw-r--r--. 1 svcamt pharpan 213 Feb 24 17:04 ManPOC_Process_Log.2017_02_24
-rw-r--r--. 1 svcamt pharpan 272 Feb 25 17:04 ManPOC_Process_Log.2017_02_25
-rw-r--r--. 1 svcamt pharpan 213 Feb 26 17:04 ManPOC_Process_Log.2017_02_26
-rw-r--r--. 1 svcamt pharpan 213 Feb 27 17:04 ManPOC_Process_Log.2017_02_27
-rw-r--r--. 1 svcamt pharpan 213 Feb 28 17:04 ManPOC_Process_Log.2017_02_28


I want all the files which are older than 2017_02_26. Here older means 25th,24th etc This is my code so far

dir='/logs/Manpoc/'
check=`date -d "- 12 days" "+%F"`
for f in ls "$dir"*
do
var= "$($f|awk '{print $9}'| cut -d'_' -f 2)"
if [ $var < $check ]
then
echo $f
fi
done


This is not giving me the expected result. How to get the desired records?

Answer Source

Simply use find:

find /logs/Manpoc -type f -ctime +12

If you don't want subdirectories:

find /logs/Manpoc -maxdepth 1 -type f -ctime +12

With some formatting:

find /logs/Manpoc -type f -ctime +12 -printf "%f\n"