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
Share:
13,686

Related videos on Youtube

Super Cool Bucket
Author by

Super Cool Bucket

Updated on June 04, 2022

Comments

  • Super Cool Bucket
    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:

    tree diagram of three files with one subdirectory which contains three more files.

    du ./ --exclude='./file*'
    

    output will be :

    4 ./dir1
    
    4    .
    

    I am getting first output but not second.

    • agc
      agc almost 5 years
      Please clarify whether the desired size should include the sizes of File[1-3], or File[4-6].
    • Super Cool Bucket
      Super Cool Bucket almost 5 years
      No it won't include size of file [1-3]
    • agc
      agc almost 5 years
      Please clarify whether the desired size should include the sizes of File[4-6].
  • gosuto
    gosuto about 3 years
    Indeed, using the -d depth flag is what is being asked for here and the best solution imo.
  • gosuto
    gosuto about 3 years
    Not sure about -S though, it is even considered an illegal option on my system.