Is there a way to list out the content of a directory including subdirectories with a file count and cumulative size?
I would like to see:
- Number of directories
- Number of sub directories
- number of files
- cumulative size
command linedisk-usagels
Is there a way to list out the content of a directory including subdirectories with a file count and cumulative size?
I would like to see:
Best Answer
If I understand you correctly, this will give you want you want:
If you run this on
/boot
for example, you get output like this:To have easy access to this command, you could turn it into a function. Add these lines to your shell's initialization file (
~/.bashrc
for bash):You can now run it as
dirsize /path/
.Explanation
The function above has 5 major parts:
find /path/to/target -type d | while IFS= read -r dir; do ... ; done
: This will find all directories under/path/to/target
and process each of them by setting the variabledir
to their name. TheIFS=
ensures this won't break on directories with spaces in their names.echo -ne "$dir\tsize: $(du -sh "$dir" | cut -f 1)"
: This uses the commanddu
to get the directory's size andcut
to print only the first field ofdu
.echo -ne "\tsubdirs: $(find "$dir" -mindepth 1 -type d | wc -l)"
: This find command looks for subdirectories of$dir
. Thetype -d
ensures we only find directories, no files and the-mindepth
makes sure we do not count the current directory,.
.echo -e "\tfiles: $(find "$dir" -maxdepth 1 -type f | wc -l)";
: This one looks for files (-type f
) that are directly (-maxdepth 1
) under$dir
. It will not count files that are in subdirectories of$d
.| tac
: Finally, the whole thing is passed throughtac
which simply reverses the order in which lines are printed. This means that the total size of the target directory will be shown as the last line. If that is not what you want, simply delete| tac
.