find directories having size greater than x MB
If I'm interpreting your question right, I think this might be what you want:
cd /home
du -sm * | awk '$1 > 1000'
This will show all directories in /home
that contain more than 1000MB. If your version of du
doesn't support -m
, you can use du -sk
and adjust the awk
bit to look for more than 1,000,000KB instead...
Unix command to list all directories larger than 10mb
du
is the easiest way. Grab the directories of interest with perl.
du -m . | perl -ne '@l = split();print "@l\n" if $l[0]>=10'
Linux Command , how to find files by size larger than x?
Try to use the power of find
utility.
Find files greater than 32 bytes:
find -size +32c
Of course you could also ask for files smaller than 32 bytes:
find -size -32c
And files with exact 32 bytes:
find -size 32c
For files with 32 bytes or more:
find -size 32c -o -size +32c
Or not smaller than 32 bytes (the same as above, but written another way):
find ! -size -32c
And files between 32 and 64 bytes:
find -size +32c -size -64c
And you can also apply other common suffixes:
find -size +32k -size -64M
Using ls to list directories and their total sizes
Try something like:
du -sh *
short version of:
du --summarize --human-readable *
Explanation:
du
: Disk Usage
-s
: Display a summary for each specified file. (Equivalent to -d 0
)
-h
: "Human-readable" output. Use unit suffixes: Byte, Kibibyte (KiB), Mebibyte (MiB), Gibibyte (GiB), Tebibyte (TiB) and Pebibyte (PiB). (BASE2)
How to get the summarized sizes of directories and their subdirectories?
This does what you're looking for:
du -sh /*
What this means:
-s
to give only the total for each command line argument.-h
for human-readable suffixes likeM
for megabytes andG
for gigabytes (optional)./*
simply expands to all directories (and files) in/
.Note: dotfiles are not included; run
shopt -s dotglob
to include those too.
Also useful is sorting by size:
du -sh /* | sort -h
Here:
-h
ensures thatsort
interprets the human-readable suffixes correctly.
Get Folder Size from Windows Command Line
You can just add up sizes recursively (the following is a batch file):
@echo off
set size=0
for /r %%x in (folder\*) do set /a size+=%%~zx
echo %size% Bytes
However, this has several problems because cmd
is limited to 32-bit signed integer arithmetic. So it will get sizes above 2 GiB wrong1. Furthermore it will likely count symlinks and junctions multiple times so it's at best an upper bound, not the true size (you'll have that problem with any tool, though).
An alternative is PowerShell:
Get-ChildItem -Recurse | Measure-Object -Sum Length
or shorter:
ls -r | measure -sum Length
If you want it prettier:
switch((ls -r|measure -sum Length).Sum) {
{$_ -gt 1GB} {
'{0:0.0} GiB' -f ($_/1GB)
break
}
{$_ -gt 1MB} {
'{0:0.0} MiB' -f ($_/1MB)
break
}
{$_ -gt 1KB} {
'{0:0.0} KiB' -f ($_/1KB)
break
}
default { "$_ bytes" }
}
You can use this directly from cmd
:
powershell -noprofile -command "ls -r|measure -sum Length"
1 I do have a partially-finished bignum library in batch files somewhere which at least gets arbitrary-precision integer addition right. I should really release it, I guess :-)
What's the best way to calculate the size of a directory in .NET?
I do not believe there is a Win32 API to calculate the space consumed by a directory, although I stand to be corrected on this. If there were then I would assume Explorer would use it. If you get the Properties of a large directory in Explorer, the time it takes to give you the folder size is proportional to the number of files/sub-directories it contains.
Your routine seems fairly neat & simple. Bear in mind that you are calculating the sum of the file lengths, not the actual space consumed on the disk. Space consumed by wasted space at the end of clusters, file streams etc, are being ignored.
How do I get the size of sub directory from a directory in python?
To print the size of each immediate subdirectory and the total size for the parent directory similar to du -bcs */
command:
#!/usr/bin/env python3.6
"""Usage: du-bcs <parent-dir>"""
import os
import sys
if len(sys.argv) != 2:
sys.exit(__doc__) # print usage
parent_dir = sys.argv[1]
total = 0
for entry in os.scandir(parent_dir):
if entry.is_dir(follow_symlinks=False): # directory
size = get_tree_size_scandir(entry)
# print the size of each immediate subdirectory
print(size, entry.name, sep='\t')
elif entry.is_file(follow_symlinks=False): # regular file
size = entry.stat(follow_symlinks=False).st_size
else:
continue
total += size
print(total, parent_dir, sep='\t') # print the total size for the parent dir
where get_tree_size_scandir()
[text in Russian, code in Python, C, C++, bash].
The size of a directory here is the apparent size of all regular files in it and its subdirectories recursively. It doesn't count the size for the directory entries themselves or the actual disk usage for the files. Related: why is the output of du
often so different from du -b
.
How to list all folder with size via batch file
For each folder in the list, use dir
command to retrieve the size of the files under the folder
@echo off
setlocal disabledelayedexpansion
set "folder=%~1"
if not defined folder set "folder=%cd%"
for /d %%a in ("%folder%\*") do (
set "size=0"
for /f "tokens=3,5" %%b in ('dir /-c /a /w /s "%%~fa\*" 2^>nul ^| findstr /b /c:" "') do if "%%~c"=="" set "size=%%~b"
setlocal enabledelayedexpansion
echo(%%~nxa # !size!
endlocal
)
endlocal
It iterates over the indicated folder (passed as parameter to the batch file, or current directory if there is no paramter).
For each folder inside it (for /d
) a recursive dir
command is executed inside the inner for
command, and from its output, the summary line at the end (extracted by findstr
) is parsed (the tokens
in for
command) and the total size of all the files under this subfolder is retrieved. Then the name (and extension if it has) of the folder and the size of the elements under it is echoed to console.
If a file needs to be created, redirect the output of the batch to a file
getSizes.cmd "c:\temp" > C:\folderList.txt
Related Topics
How to Diff Top Lines of Two Files Without Intermediate File
Running R Scripts or Commands with Interpretor in Unix for Unix-Layman
When and How Are System Calls Interrupted
Git- How to Kill Ssh-Agent Properly on Linux
Changing the PHPmyadmin Default Url
Find Command in Bash Script Resulting in "No Such File or Directory" Error Only for Directories
Cannot Clone Repository: Fatal: R Any Gitolite-Admin Gitolite Denied by Fallthru
Linux Command to Replace String in Large File with Another String
On Linux, How to Make Sure to Unlock a Mutex Which Was Locked in a Thread That Dies/Terminates
Pass Private Key Password to Openvpn Command Directly in Ubuntu 10.10
Can't Build 32Bit Wine on 64Bit Linux
Compile Programs on Multicore or Distributed System
How to Run an Opengl Application Installed on a Linux MAChine from My Windows MAChine