0

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 Answers1

1

Use the date command:

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

You can substitute it in a string with:

$ echo pre__"$(date --date=yesterday +%Y%m%d)"__post
pre__20181103__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*
pre__20181103__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? – Tarek Sayed Nov 04 '18 at 12:54
  • @TarekSayed 1) You don't have to loop. Read more on globbing: https://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. –  Nov 04 '18 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? – Tarek Sayed Nov 04 '18 at 17:55
  • @TarekSayed It's a bad practice to operate on the output of ls. https://unix.stackexchange.com/questions/128985/why-not-parse-ls-and-what-do-to-instead –  Nov 04 '18 at 18:13