Check folder size in Bash
You can do:
du -hs your_directory
which will give you a brief output of the size of your target directory. Using a wildcard like *
can select multiple directories.
If you want a full listing of sizes for all files and sub-directories inside your target, you can do:
du -h your_directory
Tips:
Add the argument
-c
to see a Total line at the end. Example:du -hcs
ordu -hc
.Remove the argument
-h
to see the sizes in exact KiB instead of human-readable MiB or GiB formats. Example:du -s
ordu -cs
.
Bash shell script for finding file size
Yes, so basically you could divide it into 4 parts:
ls -l
List the current directory content (-l
for long listing format)
| grep TestFile.txt
Pipe the result and look for the file you are interested in
| awk '{print $5}
Pipe the result to awk program which cuts (by using spaces as separator) the fifth column which happens to be the file size in this case (but this can be broken by spaces in the filename, for example)
- var=`...`
The backquotes (`) enclose commands. The output of the commands gets stored in the var variable.
NOTE: You can get the file size directly by using du -b TestFile.txt
or stat -c %s TestFile.txt
get human friendly file size from shell in unix-like environments [portability]
Most implementations of du(1)
accept the -h
flag, which causes it to print the disk usage of the file, or files, specified. The difference between disk usage and file size in normally innocuous, rounding up to the nearest file-system block.
The -h
flag, though widely supported, is an extension. For maximum portability unit conversion would have to be done manually an pointed out by chepner.
*BSD, Illumos, BusyBox, and GNU CoreUtils all support the -h
flag.
Shell file size in Linux
filesize=$(stat -c '%s' testing.txt)
How can I check the size of a file using Bash?
[ -n file.txt ]
doesn't check its size. It checks that the string file.txt
is non-zero length, so it will always succeed.
If you want to say "size is non-zero", you need [ -s file.txt ]
.
To get a file's size, you can use wc -c
to get the size (file length) in bytes:
file=file.txt
minimumsize=90000
actualsize=$(wc -c <"$file")
if [ $actualsize -ge $minimumsize ]; then
echo size is over $minimumsize bytes
else
echo size is under $minimumsize bytes
fi
In this case, it sounds like that's what you want.
But FYI, if you want to know how much disk space the file is using, you could use du -k
to get the size (disk space used) in kilobytes:
file=file.txt
minimumsize=90
actualsize=$(du -k "$file" | cut -f 1)
if [ $actualsize -ge $minimumsize ]; then
echo size is over $minimumsize kilobytes
else
echo size is under $minimumsize kilobytes
fi
If you need more control over the output format, you can also look at stat
. On Linux, you'd start with something like stat -c '%s' file.txt
, and on BSD and Mac OS X, something like stat -f '%z' file.txt
.
Portable way to get file size (in bytes) in the shell
wc -c < filename
(short for word count, -c
prints the byte count) is a portable, POSIX solution. Only the output format might not be uniform across platforms as some spaces may be prepended (which is the case for Solaris).
Do not omit the input redirection. When the file is passed as an argument, the file name is printed after the byte count.
I was worried it wouldn't work for binary files, but it works OK on both Linux and Solaris. You can try it with wc -c < /usr/bin/wc
. Moreover, POSIX utilities are guaranteed to handle binary files, unless specified otherwise explicitly.
How to find the size of a file and assign it to a variable in Unix
You can use the stat
command which eliminates the need to use AWK.
For example, in Linux with Bash where myfile is your file path:
sz=$(stat -c '%s' myfile)
if [ $sz -eq 100 ]; then
echo "myfile is 100 bytes"
fi
Take note of the equality command -eq
that's the arithmetic binary operator in Bash.
Alternatively, you can use a variable for the file path:
f=my/file/path
sz=$(stat -c '%s' $f)
if [ $sz -eq 100 ]; then
echo "$f is 100 bytes"
fi
Unix Bash Shell Scripting File Size
find /your/path/ -size 0 -exec echo rm -i {} \; # will fail if there are spaces in any file names
better way:
find /your/path/ -size 0 -print0 | xargs -0 rm -i
Remove the echo to delete the files
Thanks @Will, @AdamKatz.
Compare file sizes in shell script
Try using square braces ([]
) and -eq
like so:
I=`wc -c $i | cut -d' ' -f1`
J=`wc -c $j | cut -d' ' -f1`
if [ $I -eq $J ]
then
echo $i $j >> $1.pares
fi
Related Topics
How to Forward Localhost Port on My Container to Localhost on My Host
Embed Icc Color Profile in PDF
Why Linux Kernel Use Trap Gate to Handle Divide_Error Exception
How to Setup and Clone a Remote Git Repo on Windows
Using Named Pipes with Bash - Problem with Data Loss
Accessing .So Libraries Using Dlopen() Throws Undefined Symbol Error
Why Doesn't "History | Vim" Work
How to Return Spawned Process Exit Code in Expect Script
Any Porting Available of Backtrace for Uclibc
Extract Average Time from Ping -C
Printing Current Time in Milliseconds or Nanoseconds with Printf Builtin
Force a Shell Script to Fflush
How to Determine Stack Size of a Program in Linux
Executing Script on Receiving Incoming Connection with Xinetd