简体   繁体   English

Linux Bash 脚本:获取给定目录中“png”和“gif”文件的总大小,然后比较大小

[英]Linux Bash Scripting: Obtain the total size of 'png' and 'gif' files in a given Directory then compare the size

I'm new to Linux bash scripting in general and apologies in advance if this question is stupid.我是 Linux bash 脚本的新手,如果这个问题很愚蠢,请提前致歉。

Basically, I have this scenario where I need to obtain the total size of 'png' and 'jpeg' files in the same directory then afterwards compare this total size with a fixed value of (200kb).基本上,我有这种情况,我需要在同一目录中获取“png”和“jpeg”文件的总大小,然后将这个总大小与固定值 (200kb) 进行比较。 The problem im having is that I'm not sure how to obtain the total file size in the first place.我遇到的问题是我不确定如何首先获得总文件大小。 Any help would be well appreciated..任何帮助将不胜感激..

What I've tried: (find the total file sizes of both jpeg and png files then save it inside variables我试过的是:(找到 jpeg 和 png 文件的总文件大小,然后将其保存在变量中

jpegsize = $({ find <DIR> -type f -name "*.jpg" -printf "%s+"; echo 0; } | bc)

pngsize = $({ find <DIR> -type f -name "*.png" -printf "%s+"; echo 0; } | bc)

totalsize = $jpegsize + $pngsize

if (($totalsize <= 200000))

then 

       echo "total image size is small"
else

then

       echo "total image size is NOT small"
fi

Your method for retrieving the size of jpg and png files seems correct (be careful though, there should be no space between the equals sign when assigning variables, see this answer ).您检索jpgpng文件大小的方法似乎是正确的(不过要小心,分配变量时等号之间不应有空格,请参阅此答案)。


Here is the syntax for summing two variables:以下是对两个变量求和的语法:

totalsize=$((jpegsize + pngsize))

See this answer .看到这个答案


The final code:最终代码:

#!/bin/bash

jpegsize=$({ find -type f -name "*.jpg" -printf "%s+"; echo 0; } | bc)

pngsize=$({ find -type f -name "*.png" -printf "%s+"; echo 0; } | bc)

totalsize=$((jpegsize + pngsize))

if (($totalsize <= 200000))
then
   echo "total image size is small"
else
   echo "total image size is NOT small"
fi

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

相关问题 如何在Linux中列出目录中的文件和文件夹及其总大小? - How to list the files and folders in a directory with its total size in Linux? Linux 中的一个脚本,用于查看已删除文件的总大小 - A script in Linux to view total size of removed files Linux:如何列出有关文件或目录的信息(大小,权限,按类型划分的文件数?) - Linux:How to list the information about file or directory(size,permission,number of files by type?) in total linux bash计数文件夹中文件的大小 - linux bash count size of files in folder 如何在Shell脚本中使用文件和目录的大小进行算术运算 - how to do arithmetic operations with the size of the files and directory in shell scripting Bash脚本想要查找目录的大小,并且如果大小大于x,则执行任务 - Bash scripting wanting to find a size of a directory and if size is greater than x then do a task 如何在linux中仅递归计算某些文件的总大小 - How to calculate the total size of certain files only, recursive, in linux 在Linux中刷新目录大小 - Refresh directory Size in Linux 如何递归获取目录下所有文件的总大小 - How to get total size of all files recursively under directory Kafka 日志目录中文件的总大小小于它们大小的总和 - Total size of files in Kafka logs directory is less than the sum of their sizes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM