Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9242021
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:29:36+00:00 2026-06-18T08:29:36+00:00

I have a bash script that gives me counts of files in all of

  • 0

I have a bash script that gives me counts of files in all of the directories recursively that were edited in the last 45 days

 find . -type f -mtime -45| rev | cut -d . -f1 | rev | sort | uniq -ic | sort -rn

I have a directory called

\parent

and in parent I have:

\parent\a
\parent\b
\parent\c

I would run the above script once on folder a, once on b and once on c.

The current output is:

     91 xls
     85 xlsx
     49 doc
     46 db
     31 docx
     24 jpg
     22 pub
     10 pdf
      4 msg
      2 xml
      2 txt
      1 zip
      1 thmx
      1 htm
      1 /ic

I would like to run the script from \parent on all the folders inside \parent and get an output like this:

+-------+------+--------+
| count | ext  | folder |
+-------+------+--------+
|    91 | xls  | a      |
|    85 | xlsx | a      |
|    49 | doc  | a      |
|    46 | db   | a      |
|    31 | docx | a      |
|    24 | jpg  | a      |
|    22 | pub  | a      |
|    10 | pdf  | a      |
|     4 | msg  | a      |
|    98 | jpg  | b      |
|    92 | pub  | b      |
|    62 | pdf  | b      |
|     2 | xml  | b      |
|     2 | txt  | b      |
|     1 | zip  | b      |
|     1 | thmx | b      |
|     1 | htm  | b      |
|     1 | /ic  | b      |
|    66 | txt  | c      |
|    48 | msg  | c      |
|    44 | xml  | c      |
|    30 | zip  | c      |
|    12 | doc  | c      |
|     6 | db   | c      |
|     6 | docx | c      |
|     3 | jpg  | c      |
+-------+------+--------+

How can I accomplish this with bash?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T08:29:37+00:00Added an answer on June 18, 2026 at 8:29 am

    Put it into a script, make it executable: chmod +x script.sh and run it with: ./script.sh

    #!/bin/sh
    
    find . -type f -mtime -45 2>/dev/null \
        | sed 's|^\./\([^/]*\)/|\1/|; s|/.*/|/|; s|/.*.\.| |p; d' \
        | sort | uniq -ic \
        | sort -b -k2,2 -k1,1rn \
        | awk '
    BEGIN{ 
        sep = "+-------+------+--------+"
        print sep "\n| count | ext  | folder |\n" sep
    }
    
    { printf("| %5d | %-4s | %-6s |\n", $1, $3, $2) }
    
    END{ print sep }'
    
    • sed 's|^\./\([^/]*\)/|\1/|; s|/.*/|/|; s|/.*.\.| |p; d'

      1. s|^\./\([^/]*\)/.*/|\1 | substitutes ./a/file.xls with a/file.xls.
      2. s|/.*/|/| substitutes b/some/dir/file.mp3 with b/file.mp3.
      3. s|/.*.\.| |p substitutes a file.xls with a xls, if s///p is successful then it also prints to standard out, (to avoid files without extension).
      4. d deletes the line (to avoid printing matching (again) or non-matching lines).
    • sort | uniq -ic counts each group of extension and directory name.

    • sort -b -k2,2 -k1,1rn sorts first by directory (field 2), small -> large, and then by count (field 1) in reverse order (large -> small) and numerically. -b makes sort(1) ignore blanks (spaces/tabs).

    • the last awk part pretty prints the output, maybe you want to put this into a separate script.

    If you want to see how each pipe filters the results just try to remove each and you will see the output.

    Here you can find good tutorials about sh/awk/sed, etc.

    http://www.grymoire.com/Unix/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a bash script named except.sh which is passed a list of files/directories
I have a bash script that has set -x in it. Is it possible
I have a bash script that does ssh to a remote machine and executes
I have a bash script that cuts out a section of a logfile between
I have a bash script that sources contents from another file. The contents of
I have a bash script that simply calls different calls and redirect stdout and
I have a bash script that installs some software. I want to fail as
I have a bash script that uses growlnotify to send notifications. However, growlnotify doesn't
I have a bash script that has this function in it: function start_vi() {
I have a bash script that I need to write my password to run

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.