barskyn
barskyn

Reputation: 383

Find all folders starting with a character

I want to find the sum of the size of all the folders in my root directory csv, that begin with a capital or lower case h. My current command only gives all the files that contain an h, not beginning with. What am I doing wrong?

find csv -iname ^h -type d | du -h

Upvotes: 0

Views: 294

Answers (2)

kvantour
kvantour

Reputation: 26481

The following line will print all directories starting with h, and sends the output to du -bc :

find csv -iname 'h*' -type d -exec du -bc '{}' +

The command du -bc will print all sizes of subdirectories in bytes and in the end show the total bytesize.

If you just want to see the total, you can pipe it to tail -1

Upvotes: 1

anubhava
anubhava

Reputation: 785186

You can use this pipeline to get sum of all folders that start with h or H:

find csv -type d -iname 'h*' -print0 |
xargs -0 du -s |
awk '{sum+=$1} END{print sum}'

Note that output will be in kb. If you want to get mb or gb them change expression in END block.

Upvotes: 0

Related Questions