Linux判断文件是否为空,不为空则打印该文件的大小

Linux判断文件是否为空,不为空则打印该文件的大小,使用到的命令是-s + filename

-s filename
如果文件大小大于0,则返回true。

例如:

查看当前目录

# ls -l
total 8
-rwxrwxr-x 1 pentester pentester 136 6月  19 15:58 is_Empyt.sh
-rw-r--r-- 1 root      root        7 6月  19 15:59 myfile.txt

查看脚步内容:

# cat is_Empyt.sh 

#! /bin/bash

if [ -s ./myfile.txt ] ; then 
  echo 'ths file is not empyt and file info:'
  du -sh myfile.txt  #打印文件大小
else
  echo 'empty!'
fi

执行效果:

# ./is_Empyt.sh 
ths file is not empyt and file info:
4.0K    myfile.txt
原文地址:https://www.cnblogs.com/tdcqma/p/7049126.html