displaying disk space usage of the current directory excluding size of subdirectories
13,686
Solution 1
du -Sd 1
Output will be:
4 ./dir1
4 .
Solution 2
Suppose the current directory is /tmp/foo, which has no files, except for a single directory /tmp/foo/bar, into which is put a copy of bash
(1113504 bytes). Running the tree
util:
tree --du "$(pwd)"
...reports:
/tmp/foo
└── [ 1117600] bar
└── [ 1113504] bash
1121696 bytes used in 1 directory, 1 file
To get the size in bytes of /tmp/foo, (but not /tmp/foo/bar), this works:
du -bSd 1 "$(pwd)" | grep -w "$(pwd)$"
Output:
4096 /tmp/foo
The same line of code can be reused, just cd
to any directory:
cd foo/bar/
du -bSd 1 "$(pwd)" | grep -w "$(pwd)$"
Output:
1117600 /tmp/foo/bar
Related videos on Youtube
Author by
Super Cool Bucket
Updated on June 04, 2022Comments
-
Super Cool Bucket almost 2 years
I want to write a command to display the disk space usage the current directory excluding the size of subdirectories. The following image describes the files and directories of the current directory:
du ./ --exclude='./file*'
output will be :
4 ./dir1 4 .
I am getting first output but not second.
-
agc almost 5 yearsPlease clarify whether the desired size should include the sizes of File[1-3], or File[4-6].
-
Super Cool Bucket almost 5 yearsNo it won't include size of file [1-3]
-
agc almost 5 yearsPlease clarify whether the desired size should include the sizes of File[4-6].
-
-
gosuto about 3 yearsIndeed, using the
-d depth
flag is what is being asked for here and the best solution imo. -
gosuto about 3 yearsNot sure about
-S
though, it is even considered an illegal option on my system.