I have some files like T24_STFBNK_CUSTOMER_2018100107553815383733380046446200.txt. The bold numbers 20181001 is the date of the file,I need a shell script to list the files which its date is yesterday only! how can I do this?

1 Answer 1


Use the date command:

date --date=yesterday +%Y%m%d

You can substitute it in a string with:

$ echo pre__"$(date --date=yesterday +%Y%m%d)"__post

If you understand this, you can now use ls and globbing, adjusting the searched pattern as needed. Eg.:

$ ls *pre__"$(date --date=yesterday +%Y%m%d)"__post*

This has listed the file pre__20181103__post present in the current directory.

  • thank you very much for your reply! i understand you ! i have a folder that contains some files with this name! so i have to loop through this files and list the files with date of yesterday! how can i get the pattern 20181001? Nov 4, 2018 at 12:54
  • @TarekSayed 1) You don't have to loop. Read more on globbing: en.wikipedia.org/wiki/Glob_(programming) 2) If you want to use a simple text like 20181001, you don't have to produce it with any automatic tool. It is a valid string itself.
    – user147505
    Nov 4, 2018 at 13:22
  • how about using ls to list all files in one file and after that i will use grep | TZ=aaa24 date +%Y%m%d to to find the files with yesterday's date! after that what should i do? Nov 4, 2018 at 17:55
  • @TarekSayed It's a bad practice to operate on the output of ls. unix.stackexchange.com/questions/128985/…
    – user147505
    Nov 4, 2018 at 18:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .