I want to automate one task in UNIX and make one script.

There is a directory in which everyday 2 files come and that files come anytime in between 10pm to 1am.

The file name is something like batm53_20131025_BARC5300001201310240000000200004233.221341696408.

If you see a filename has 3 parts:

  1. batm53 which is constant
  2. date when file arrive in directory
  3. this part is BARC5300001201310170000000200004233.221341696408 in which BAR5300001 is constant and then after date of file.

Suppose today is 24th Oct and file come after 1am then second part of filename shows 25th Oct but in third part after BAR5300001 it shows 24th Oct date ( 20131024).

I'm facing issue in this scenario. If file comes in between 10pm to 12am then I can make script easy with help of find command but I'm facing issue in making that part. How can I check if file comes after 12am because that file should be previous date and after 12am date should change?

  • @Pallvi have you checked my ans ? is that working or I miss understood ?? let us know.. Commented Oct 25, 2013 at 21:58
  • Your question is not clear: do you want to match files based on their name, or based on their timestamp? If you want to use the name, which part indicates the time of day? If you want to use the timestamp, what is the relevance of the information about the two dates in the name? Commented Nov 9, 2014 at 3:13

1 Answer 1


You can create file "temp" at 12 am then find file newer then rename those file with respective date

For Example:

Step #1

Create file at or after 12 am using following command.

$ touch -d "$(date +%F)" temp.file

touch will give file date if you run on 26 Oct 2013, then file date would be "Oct 26 00:00".

Step #2

As you wish you set previous date to file then create variable to get previous date:

$ Yesterday=$(date +%Y%m%d -d '1 day ago')

NOTE: (this will save value "20131025" in variable).

Step #3

Now you can find file newer than temp.file which we have created.

$ find . -type f -newer temp.file

OK, you can view files created after 12 Am as you wish. Now you want to rename to previous date?

$ find . -type f -newer temp.file | \
    awk -v y=$Yesterday '{split($0,arr,"_"); \
    print "mv -v --",$0, arr[1]"_"y"_"arr[3]}'

The above command will just print output, where first file is the original filename and the second file which you want.

If you think output seems to fine then simply append | sh to same command

$ find . -type f -newer temp.file | \
    awk -v y=$Yesterday '{split($0,arr,"_"); \
    print "mv -v --",$0, arr[1]"_"y"_"arr[3]}' | sh

This will rename file with previous date in second part. So you can create script with help of above commands then run after 12 using cron.

You must log in to answer this question.

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