Quickly list file count per directory in the current directory

Getting each file count of each directory

for i in */ .*/ ; do echo -n $i": " ; (find "$i" -type f | wc -l) ; done

Easy-to-read version

If you like it in formatted terms:

for i in */ .*/ ; do
    echo -n $i": " ; 
    (find "$i" -type f | wc -l) ; 
done

Commented version

For those who want to understand every single detail of what they’re entering:

# Use a `for` loop to run the next command using `*/` (regular directories) and
# `.*/` (hidden directories that start with a `.`).  Note the trailing forward
# slash `/`, which targets directories only
for i in */ .*/ ; do

    # Print the folder name to the console. The `-n` flag will prevent a newline
    # character from printing into the console.
    echo -n $i": " ;

    # Get the file count using the `find` command. `-type f` will look for files
    # only. The results are piped `|` to the `wc` command with the `-l` argument
    # which counts the lines (one line per file).
    (find "$i" -type f | wc -l) ;

# End the for loop
done

Bonus: Getting the item count of the current directory

# Using `ls` with `-f` is unsorted, which should work faster.
ls -f | wc -l

Leave a Reply

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.