简体   繁体   中英

how to do arithmetic operations with the size of the files and directory in shell scripting

how to do arithmetic operations with the size of the files and directory when they are in different unites like free space is in MB and the file size is in GB

With one preparatory command I am able to fetch the size of the "/home/abc/def" directory in MB. Its 30GB so getting in KB is not a good idea.

mount            fssizeMB  
=====            =======  
/home/abc/def    30002    

root@abc:/home/abc/def> ls -lrth 
total 7.0G 
drwxrwxrwx  3 root root   114 Oct 29  2012 file1  
drwxr-xr-x  3 root root   103 Nov 22  2012 file2  
-rw-r--r--  1 root root   114 Jan 25  2013 file3  
-rw-r--r--  1 mtc  users 3.8G Jul 22 03:02 file4 <------------------- concerned file  
-rw-r--r--  1 mtc  users 3.2G Jul 24 22:26 file5  
-rw-r--r--  1 root root     0 Jan  5 20:30 file6  

How to turn below logic in script:

If twice the file size of file4 is < free space of "/home/abc/def " then echo success or else failure.

You could use stat or du -Sh to get the size of file (don't use ls for that in a script).

And to browse the files of a folder :

for i in <direcory>/*; do ...; done

Then, you could use test or [ commands (or [[ if you use Bash) to make a comparison (with -ge , -gt , -lt , -le options as arithmetic operators).

See the manpages of each command to get more information.

this would work with percentages, just to give you an idea, you could modify it to deal with MB or GB and so on.

my advice: doing arithmetic operations in bash is not such a good idea, you should work with programming languages that deal with special variable data type, like float or str and so on. bash is simpler and doesn't work so well with arithmetic operations. sure it does your + and -, but when it comes to percentages and floats... not so well. try python or perl, or try researching something else. and definitely use, as suggested above, du -sh

#!/usr/bin/env bash
#take df -h and iterate trough percentages
#check to see if file system is full more than 80 %
perc="$(df -h | awk '{print $5}'| sed -e 's/[%/ a-z/A-Z].*//g' )"

#echo $perc

for p in $perc
do 
     if [ $p -gt 30  ]  #change 30 to whatever
        then
        df -h | grep $p
        echo -e "$p Exceeded on `hostname`"
     fi 
done

Most commands have options to show the size using a specific unit.

The -h flag of ls and df are to produce "human readable" format, which is not suitable for arithmetic calculations, as they can be in inconsistent units.

To get the size of a file, use stat , or even wc -c . It's a bad practice to parse the output of ls -l , so don't use that.

If you can get the size of a file consistently in kilobytes, and the size of free space consistently in bytes, not a problem, you can just multiply the size in bytes with 1024 to be able to make comparisons in consistent units.

The specific commands and flags to use will depend on your operating system and the software installed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM