1

How can I check how many files have been created in one week? Situation: The system will create four files at every week, if the system is running normally. For example:

A_08-07-2015.csv
B_08-07-2015.csv
C_08-07-2015.csv
D_08-07-2015.csv

A_01-07-2015.csv
B_01-07-2015.csv
C_01-07-2015.csv
D_01-07-2015.csv

A_24-06-2015.csv
B_24-06-2015.csv
C_24-06-2015.csv
D_24-06-2015.csv
      ⋮

Now, I want to write a shell script to check whether the system created these four files at every week to ensure the system running normally. But, I don't know how to write...

4
$ ls
A_01-07-2015.csv  B_01-07-2015.csv  C_01-07-2015.csv  D_01-07-2015.csv
A_08-07-2015.csv  B_08-07-2015.csv  C_08-07-2015.csv  D_08-07-2015.csv
A_24-06-2015.csv  B_24-06-2015.csv  C_24-06-2015.csv  D_24-06-2015.csv
$ ls | awk -F'[^0-9]' '{ print $5$4$3 }' | sort | uniq -c
      4 20150624
      4 20150701
      4 20150708
$ rm B_08-07-2015.csv
$ ls | awk -F'[^0-9]' '{ print $5$4$3 }' | sort | uniq -c
      4 20150624
      4 20150701
      3 20150708

A date not having 4 files is easy to spot this way.


As reply to the 1st comment:

$ touch {A,B_B,banana,popaye}_{01-07,08-07,24-06}-2015.lol.csv
$ ls
A_01-07-2015.lol.csv  banana_01-07-2015.lol.csv  B_B_01-07-2015.lol.csv  popaye_01-07-2015.lol.csv
A_08-07-2015.lol.csv  banana_08-07-2015.lol.csv  B_B_08-07-2015.lol.csv  popaye_08-07-2015.lol.csv
A_24-06-2015.lol.csv  banana_24-06-2015.lol.csv  B_B_24-06-2015.lol.csv  popaye_24-06-2015.lol.csv
$ ls | awk -F'[^0-9]+' '{ print $4$3$2 }' | sort | uniq -c
      4 20150624
      4 20150701
      4 20150708
$ rm B_B_08-07-2015.lol.csv 
$ ls | awk -F'[^0-9]+' '{ print $4$3$2 }' | sort | uniq -c
      4 20150624
      4 20150701
      3 20150708

This version accepts every string of nondigits as field separator, so it'll fail if the prefix in front of the date contains a digit too.

1
  • Thanks! But I found some problem, is my mistake, the file name should be A_01-07-2015.csv B_B_01-07-2015.csv C_C_01-07-2015.csv D_D_01-07-2015.csv – kachan2011 Jul 8 '15 at 6:11
4

On Unix/Linux filesystems file attributes normally don't include a creation time field (as e.g. they do on VFAT and NTFS).

So unless you can rely on the date like patterns in the file names you will have to keep a list of previous file names, and compare last week's list with this week's list.

Neither method gives you any guarantee that a file that existed last week was deleted and recreated in the intermediate time. If the circumstances are not further constrained, you cannot in tell which files were created with certainty.

2
  • The very latest linux kernels do for btrfs. They don't have a userspace tool for reading it though. – mikeserv Jul 8 '15 at 6:45
  • @mikeserv I was not sure which ones did include this information, there are bound to be others, but indeed nothing that a normal stat() gives you access to. – Anthon Jul 8 '15 at 8:25

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.