Linux下獲取和比較文件大小

---
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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章