---
titlecn Linux下獲取和比較文件大小
author Charles Shih <[email protected]>
date 2020-01-14
updated 2020-01-14
tags Linux,bash
---
正文
Linux下獲取文件大小最直接的命令就是ls -l
,然後取出對應字段:
$ ls -l filename
-rw-r--r--. 1 cheshi cheshi 21478375424 Jan 13 23:54 filename
$ ls -l filename | awk '{print $5}'
21478375424
通過ls
命令的-h
參數,可以獲得可讀性較高的版本。
$ ls -lh filename | awk '{print $5}'
21G
比較或判斷特定文件大小可以參考如下代碼:
#!/bin/bash
echo -e "\nEnlarge the image..."
local fsize=$(ls -l $workspace/$image_file | awk '{print $5}')
if [ "$fsize" -lt "$((20 * 1024 * 1024 * 1024))" ]; then
# Enlarge the image...
else
echo -e "Already enlarged to 20GiB, skip this operation."
fi