简体   繁体   English

如何在Shell脚本中使用文件和目录的大小进行算术运算

[英]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 当文件和目录的大小不同时如何对文件和目录的大小进行算术运算,例如可用空间以MB为单位,文件大小以GB为单位

With one preparatory command I am able to fetch the size of the "/home/abc/def" directory in MB. 通过一个准备性命令,我可以获取MB中“ / home / abc / def”目录的大小。 Its 30GB so getting in KB is not a good idea. 它的容量为30GB,因此获得KB并不是一个好主意。

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. 如果文件4的文件大小的两倍小于</ home / abc / def的可用空间,则回显成功或失败。

You could use stat or du -Sh to get the size of file (don't use ls for that in a script). 您可以使用statdu -Sh来获取文件的大小(在脚本中不要使用ls )。

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). 然后,您可以使用test[命令(或[[如果使用Bash的话])进行比较(使用-ge-gt-lt-le选项作为算术运算符)。

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. 这将与百分比一起使用,只是为了给您一个想法,您可以对其进行修改以处理MB或GB等。

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中进行算术运算不是一个好主意,您应该使用处理特殊变量数据类型(例如float或str等)的编程语言。 bash is simpler and doesn't work so well with arithmetic operations. bash比较简单,在算术运算中效果不佳。 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. 尝试使用python或perl,或尝试研究其他内容。 and definitely use, as suggested above, du -sh 并按照上面的建议绝对使用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. lsdf-h标志将产生“人类可读”格式,该格式不适合算术计算,因为它们的单位可能不一致。

To get the size of a file, use stat , or even wc -c . 要获取文件的大小,请使用stat甚至wc -c It's a bad practice to parse the output of ls -l , so don't use that. 解析ls -l的输出是一个不好的做法,所以不要使用它。

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. 如果可以始终以千字节为单位获取文件大小,以字节为单位始终获取可用空间大小,这不是问题,则只需将以字节为单位的大小乘以1024,就可以以一致的单位进行比较。

The specific commands and flags to use will depend on your operating system and the software installed. 具体使用的命令和标志取决于您的操作系统和安装的软件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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