linux command to get size of files and directories present in a particular folder?
Use ls
command for files and du
command for directories.
Checking File Sizes
ls -l filename #Displays Size of the specified file
ls -l * #Displays Size of All the files in the current directory
ls -al * #Displays Size of All the files including hidden files in the current directory
ls -al dir/ #Displays Size of All the files including hidden files in the 'dir' directory
ls
command will not list the actual size of directories(why?). Therefore, we use du
for this purpose.
Checking Directory sizes
du -sh directory_name #Gives you the summarized(-s) size of the directory in human readable(-h) format
du -bsh * #Gives you the apparent(-b) summarized(-s) size of all the files and directories in the current directory in human readable(-h) format
Including -h
option in any of the above commands (for Ex: ls -lh *
or du -sh
) will give you size in human readable format (kb
, mb
,gb
, ...)
For more information see man ls
and man du
Get size of folder or file
java.io.File file = new java.io.File("myfile.txt");
file.length();
This returns the length of the file in bytes or 0
if the file does not exist. There is no built-in way to get the size of a folder, you are going to have to walk the directory tree recursively (using the listFiles()
method of a file object that represents a directory) and accumulate the directory size for yourself:
public static long folderSize(File directory) {
long length = 0;
for (File file : directory.listFiles()) {
if (file.isFile())
length += file.length();
else
length += folderSize(file);
}
return length;
}
WARNING: This method is not sufficiently robust for production use. directory.listFiles()
may return null
and cause a NullPointerException
. Also, it doesn't consider symlinks and possibly has other failure modes. Use this method.
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 :-)
How can I find the size of all files located inside a folder?
Actually I don't want to use any third party library. Just want to
implement in pure c++.
If you use MSVC++ you have <filesystem>
"as standard C++".
But using boost or MSVC - both are "pure C++".
If you don’t want to use boost, and only the C++ std:: library this answer is somewhat close. As you can see here, there is a Filesystem Library Proposal (Revision 4). Here you can read:
The Boost version of the library has been in widespread use for ten
years. The Dinkumware version of the library, based on N1975
(equivalent to version 2 of the Boost library), ships with Microsoft
Visual C++ 2012.
To illustrate the use, I adapted the answer of @Nayana Adassuriya , with very minor modifications (OK, he forgot to initialize one variable, and I use unsigned long long
, and most important was to use: path filePath(complete (dirIte->path(), folderPath));
to restore the complete path before the call to other functions). I have tested and it work well in windows 7.
#include <iostream>
#include <string>
#include <filesystem>
using namespace std;
using namespace std::tr2::sys;
void getFoldersize(string rootFolder,unsigned long long & f_size)
{
path folderPath(rootFolder);
if (exists(folderPath))
{
directory_iterator end_itr;
for (directory_iterator dirIte(rootFolder); dirIte != end_itr; ++dirIte )
{
path filePath(complete (dirIte->path(), folderPath));
try{
if (!is_directory(dirIte->status()) )
{
f_size = f_size + file_size(filePath);
}else
{
getFoldersize(filePath,f_size);
}
}catch(exception& e){ cout << e.what() << endl; }
}
}
}
int main()
{
unsigned long long f_size=0;
getFoldersize("C:\\Silvio",f_size);
cout << f_size << endl;
system("pause");
return 0;
}
ASP.NET: Fast way to get file size and count of all files?
Try dumping recursion, and try dumping linq - it is slow and eats up a lot of memory.
Try this:
Dim strFolder = "C:\Users\AlbertKallal\Desktop"
Dim MyDir As New DirectoryInfo(strFolder)
Dim MyFiles() As FileInfo = MyDir.GetFiles("*.*", SearchOption.AllDirectories)
For Each MyFile As FileInfo In MyFiles
tSize += MyFile.Length
Next
How to list the size of each file and directory and sort by descending size in Bash?
Simply navigate to directory and run following command:
du -a --max-depth=1 | sort -n
OR add -h for human readable sizes and -r to print bigger directories/files first.
du -a -h --max-depth=1 | sort -hr
Trying to get folder sizes for all users directory
The issue you have is that FullName contains a DirectoryInfo object, you have two options;
Change your select to ExpandProperty which will change it to a string of the full path.
Select-Object -ExpandProperty Fullname
Refer to $Root using the property FullName which is a property on the DirectoryInfo Object.
Get-ChildItem -path $Root.FullName -Recurse
This is one solution to what you are trying to achieve, note that errors (e.g. access denied) are ignored.
Get-ChildItem $StorageLocation | ForEach-Object {
$sizeInMB = (Get-ChildItem $_.FullName -Recurse -ErrorAction SilentlyContinue | Measure-Object -Property Length -Sum -ErrorAction SilentlyContinue).Sum / 1MB
New-Object PSObject -Property @{
FullName = $_.FullName
SizeInMB = $sizeInMB
}
}
Related Topics
Output Redirection Using Fork() and Execl()
Function Already Defined Error in C++
How to Find the Size of All Files Located Inside a Folder
Boost Asio - How to Write Console Server
Print 2-D Array in Clockwise Expanding Spiral from Center
Differencebetween String::At and String::Operator[]
C++: Where Does the Ofstream Class Save the Files To
Why the Initializer of Std::Function Has to Be Copyconstructible
Coding Practices Which Enable the Compiler/Optimizer to Make a Faster Program
How to Pass Std::Unique_Ptr into a Function
Best Bignum Library to Solve Project Euler Problems in C++
Ramifications of C++20 Requiring Two's Complement
How to Speed Up This Histogram of Lut Lookups
Parse Int or Double Using Boost Spirit (Longest_D)
Where Should Non-Member Operator Overloads Be Placed
Qt 5 and Qprocess Redirect Stdout with Signal/Slot Readyread