3

I have to check a particular path every day and find the file with a name of the form:

StaticData_Sets_yyyymmdd-232550.txt

The date in the name after the string StaticData_Sets_ is updated every day depending on the system date. The number after the date is random.

How can I find the file for the current date in Unix?

6

Use command date to generate current day with proper output format, and append that string to file name in the following fashion:

filename=StaticData_Sets_$(date +"%Y%m%d")
find . -name "$filename*.txt"
  • May be interesting to use "${filename}*.txt" to make clear where the name of the variable ends. – fedorqui Oct 28 '14 at 12:08
1

That find command should find the file with todays date:

find /path/ -regex ".*\/StaticData_Sets_$(date +%Y%m%d)-[0-9]+\.txt"

It find all that contain /StaticData_Sets_, then the date in your desired format, and then a digit ([0-9]+).

  • 1
    Jaipreet if you're trying to find yesterday's file instead of today's file, then replace $(date +%Y%m%d) with $(date --date=yesterday +%y%m%d). See man date and read the short section under "DATE STRING" or for a very long treatise on the subject, try info date and read section "28 Date input formats". It's very flexible in that you can even say something esoteric like date --date="next tuesday 2 years ago" – Ian Macintosh Oct 28 '14 at 10:26
1

If the file is directly under the path you are given (no extra subdirectories) then this should work.

file=$path/StaticData_Sets_$(date +"%Y%m%d")-*.txt

If there are then this should work (might need to manually enable globstar in bash).

    file=$path/**/StaticData_Sets_$(date +"%Y%m%d")-*.txt

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.