如何在 Bash 中检查文件是否为空?

问题描述 投票:0回答:13

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

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

if [ -s diff.txt ]
then
        touch empty.txt
        rm full.txt
else
        touch full.txt
        rm emtpy.txt
fi
linux bash unix file-handling is-empty
13个回答
419
投票

试试这个:

#!/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

顺便请注意,我已经交换了

empty.txt
full.txt
的角色,正如 @Matthias 所建议的那样。


133
投票
[ -s file.name ] || echo "file is empty"

85
投票

[ -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

28
投票

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

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

20
投票

虽然其他答案是正确的,但使用

"-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

20
投票

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

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"

11
投票

@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"

4
投票

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

示例 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
}

2
投票

类似于@noam-manos的基于

grep
的答案,我使用
cat
解决了这个问题。对我来说,
-s
不起作用,因为我的“空”文件有>0字节。

if [[ ! -z $(cat diff.txt) ]] ; then
    echo "diff.txt is not empty"
else
    echo "diff.txt is empty"
fi

0
投票

我来这里寻找如何删除空的

__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。不管怎样,我想我会分享!


0
投票
  1. (对于上面的人)我不确定您是否真的想在
    [[
    程序中使用逻辑运算符。也许您应该使用
    [[ expr_1 ]] && [[ expr_2 ]]
    而不是
    [[ expr_1 && expr_2 ]]
  2. 表达式
    [[ -s file.txt ]]
    还会检查文件是否存在,因此我认为在此之前没有任何理由使用
    -f

这是一个简单的语句,用于检查文件是否存在、不为空且包含 0:

if [[ ! -s ./example.txt  ]] || grep -q 0 "./example.txt"; then
  echo "example.txt doesn't exist, is empty or contains 0"
else
  echo "example.txt contains 1"

最后一点: 请注意,空文件并不总是空的:

  • touch example.txt
    - 该文件是空的
  • echo "" > example.txt
    - 该文件不为空

-1
投票

我的答案有些特定于与“虚拟”文件系统(如

sysfs
)一起使用。如果您使用此处大多数答案中提到的方法,此类文件系统可能会给出错误的结果。因为文件可以被
stat
'ed 为非零长度,但如果您尝试读取其内容,您将什么也得不到(或 I/O 错误)。

cat -n 1 '/path/to/file' &> /dev/null && echo true || echo false

一些解释。在这里,我们尝试读取文件的第一行并默默地忽略其内容(将其发送到

/dev/null
)。根据退出状态,我们打印“true”或“false”。

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.