【问题标题】:How to check if a file is empty in Bash?如何在 Bash 中检查文件是否为空?
【发布时间】:2012-04-15 10:16:45
【问题描述】:

我有一个名为 diff.txt 的文件。我要检查是否为空。

我写了一个类似下面的 bash 脚本,但我无法让它工作。

if [ -s diff.txt ]
then
        touch empty.txt
        rm full.txt
else
        touch full.txt
        rm emtpy.txt
fi

【问题讨论】:

  • [ -s FILE ] 如果 FILE 存在且大小大于零,则为真。因此,如果“diff.txt”不为空,则会得到“empty.txt”。
  • PS:如果要查看实际的diff调用,只需查看返回值:if diff foo.txt bar.txt; then echo 'No difference'
  • 可以否定测试:if [ ! -s diff.txt ]; then echo "IS EMPTY";else echo "HAS SOMETHING";fi
  • 注意尾随的换行符。使用$ cat diff.txt | hexdump -C检查文件

标签: linux bash unix file-handling is-empty


【解决方案1】:

拼写错误很烦人,不是吗?检查empty 的拼写,然后试试这个:

#!/bin/bash -e

if [ -s diff.txt ]; then
        # The file is not-empty.
        rm -f empty.txt
        touch full.txt
else
        # The file is empty.
        rm -f full.txt
        touch empty.txt
fi

我非常喜欢 shell 脚本,但它的一个缺点是当你拼写错误时 shell 无法帮助你,而像 C++ 编译器这样的编译器可以帮助你。

请注意,正如@Matthias 建议的那样,我已经交换了empty.txtfull.txt 的角色。

【讨论】:

  • shell 可以帮助解决拼写错误。 empty=empty.txt; full=full.txt; diff=diff.txt; if [ -s ${diff?} ]; then r=${empty?} t=${full?}; else r=${full?} t=${empty?}; fi; rm ${r?}; touch ${t?}
  • 使用工具shellcheck可以发现拼写错误就好了。
  • 如果文件不存在,这肯定会失败吗?这应该只是检查文件是否为空。
  • 你也可以set -u
  • shebang 中传递的-e 参数是什么?
【解决方案2】:
[ -s file.name ] || echo "file is empty"

【讨论】:

  • [[ -s file.name ]] && echo "full" ||回声“空”
  • [[ -s file.name ]] || { [[ -f file.name ]] && echo 'empty' || echo 'does not exist'; }
  • @smarber 进行这样的简单检查,请使用[ ... ] 而不是[[ ... ]]。后者是 bashism,它应该只用于不兼容的 bash 检查(例如 regexp)。如果你碰巧写了任何应该是 POSIX shell 可移植的东西(例如 Debian 系统脚本),你自己会喜欢这个习惯的 :)。
【解决方案3】:

[ -s file ] # Checks if file has size greater than 0

[ -s diff.txt ] && echo "file has something" || echo "file is empty"

如果需要,这会检查当前目录中的所有 *.txt 文件;并报告所有空文件:

for file in *.txt; do if [ ! -s $file ]; then echo $file; fi; done

【讨论】:

  • 你不需要做$(ls *.txt,事实上也不应该。有些人为使用长格式的ls 设置了默认值(比如我),并且shell 已经自行扩展*.txt。改用for file in *.txt
  • 不错!如果您想递归检查所有 txt 文件,可以使用 find,如下所示:for file in $(find . -name '*.txt'); do if [[ ! -s $file ]]; then echo $file; fi; done
【解决方案4】:

虽然其他答案是正确的,但即使文件不存在,使用 "-s" 选项也会显示文件为空。
通过添加这个额外的检查"-f"首先查看文件是否存在,我们确保结果是正确的。

if [ -f diff.txt ]
then
  if [ -s diff.txt ]
  then
    rm -f empty.txt
    touch full.txt
  else
    rm -f full.txt
    touch empty.txt
  fi
else
  echo "File diff.txt does not exist"
fi

【讨论】:

    【解决方案5】:

    要检查文件是否为空或只有空格,您可以使用 grep:

    if [[ -z $(grep '[^[:space:]]' $filename) ]] ; then
      echo "Empty file" 
      ...
    fi
    

    【讨论】:

    • 这是唯一好的答案,应该被接受。使用 -s 并不能回答问题。它会查找一个确实存在且大小超过 0 字节的文件。
    【解决方案6】:

    检查文件是否为空的最简单方法:

    if [ -s /path-to-file/filename.txt ]
    then
         echo "File is not empty"
    else
         echo "File is empty"
    fi
    

    你也可以单行写:

    [ -s /path-to-file/filename.txt ] && echo "File is not empty" || echo "File is empty"
    

    【讨论】:

      【解决方案7】:

      @geedoubleya 答案是我的最爱。

      不过,我更喜欢这个

      if [[ -f diff.txt && -s diff.txt ]]
      then
        rm -f empty.txt
        touch full.txt
      elif [[ -f diff.txt && ! -s diff.txt ]]
      then
        rm -f full.txt
        touch empty.txt
      else
        echo "File diff.txt does not exist"
      fi
      

      【讨论】:

        【解决方案8】:
        [[ -f filename && ! -s filename ]] && echo "filename exists and is empty"
        

        【讨论】:

          【解决方案9】:

          许多答案是正确的,但我觉得它们可以更完整 / 简单化等例如:

          示例 1:基本 if 语句

          # BASH4+ example on Linux :
          
          typeset read_file="/tmp/some-file.txt"
          if [ ! -s "${read_file}" ]  || [ ! -f "${read_file}" ] ;then
              echo "Error: file (${read_file}) not found.. "
              exit 7
          fi
          

          如果 $read_file 为空或不存在,则退出并停止显示。我不止一次误读了这里的最佳答案,意思是相反的。

          示例 2:作为函数

          # -- Check if file is missing /or empty --
          # Globals: None
          # Arguments: file name
          # Returns: Bool
          # --
          is_file_empty_or_missing() {
              [[ ! -f "${1}" || ! -s "${1}" ]] && return 0 || return 1
          }
          

          【讨论】:

            【解决方案10】:

            我来这里是为了寻找如何删除空的 __init__.py 文件,因为它们在 Python 3.3+ 中是隐式的,最终使用:

            find -depth '(' -type f  -name __init__.py ')' -print0 |
              while IFS= read -d '' -r file; do if [[ ! -s $file ]]; then rm $file; fi; done
            

            另外(至少在 zsh 中)使用 $path 作为变量也会破坏你的 $PATH 环境,因此它会破坏你打开的 shell。无论如何,我想我会分享!

            【讨论】:

              猜你喜欢
              • 2014-08-23
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2015-10-14
              • 2023-02-16
              • 2013-03-31
              • 1970-01-01
              相关资源
              最近更新 更多