Beginner
Beginner

Reputation: 43

How to get the record count on hourly basis unix?

I have thousands of files on my unix server and i want to count the records of files on hourly basis in below format. What is the easiest way to do it?

Date       Hour records

2022-07-08 00   5565

2022-07-08 01   77878

2022-07-08 02   545

.

.

2022-07-08 23   656

2022-07-09 00   787

2022-07-09 01   54547

Upvotes: 0

Views: 453

Answers (1)

F. Hauri  - Give Up GitHub
F. Hauri - Give Up GitHub

Reputation: 70842

Counting (recursively) all files in current dir, by hour

find is the command to use for finding filesystem entries regarding any kind of consideration. This way will print one date, limited by hour, for each file found.

find . -type f -printf '%TY-%Tm-%Td %TH\n' | sort | uniq -c

Output could look like:

    851 2022-07-13 00
    849 2022-07-13 01
    855 2022-07-13 02
    858 2022-07-13 03
...

Some cosmetic, using sed:

find . -type f -printf '%TY-%Tm-%Td %TH\n' |
    sort |
    uniq -c |
    sed 's/^\( *[0-9]\+\) \([0-9-]\+\) \([0-9]\+\)/    \2  \3  \1/;
         1i\    Date        Hour  Count'

Will produce:

    Date        Hour  Count
...
    2022-07-13  00      851
    2022-07-13  01      849
    2022-07-13  02      855
    2022-07-13  03      858
...

By using ls instead of find?

Without recursivity!

ls -ARlrt  --time-style="+|%Y-%m-%d:%H|" |
    grep -a ^-|
    cut -d \| -f 2 |
    sort |
    uniq -c

Will produce near same result:

...
    851 2022-07-13:00
    849 2022-07-13:01
    855 2022-07-13:02
    858 2022-07-13:03
...

But as ls will print filenames who could contain special characters, could force grep to procuce wrong output... This way is not recommended!

Upvotes: 1

Related Questions