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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9964823/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 21:54:09  来源:igfitidea点击:

How to check if a file is empty in Bash?

bashif-statementfile-handlingis-empty

提问by Mich

I have a file called diff.txt. Want to check if it is empty. Did something like this but couldn't get it working.

我有一个名为 diff.txt 的文件。想检查它是否为空。做了这样的事情,但无法让它工作。

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

回答by thb

Misspellings are irritating, aren't they? Check your spelling of empty, but then also try this:

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

#!/bin/bash -e

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

I like shell scripting a lot, but one disadvantage of it is that the shell cannot help you when you misspell, whereas a compiler like your C++ compiler can help you.

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

Notice incidentally that I have swapped the roles of empty.txtand full.txt, as @Matthias suggests.

顺便说一下,正如@Matthias 所建议的那样,我已经交换了empty.txt和的角色full.txt

回答by mickey white

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

回答by Surya

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

[[ -s file ]] --> 检查文件的大小是否大于 0

if [[ -s diff.txt ]]; then echo "file has something"; else echo "file is empty"; fi

If needed, this checks all the *.txt files in the current directory; and reports all the empty file:

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

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

回答by geedoubleya

While the other answers are correct, using the "-s"option will also show the file is empty even if the file does not exist.
By adding this additional check "-f"to see if the file exists first, we ensure the result is correct.

虽然其他答案是正确的,但"-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

回答by smarber

@geedoubleyaanswer is my favorite.

@geedoubleya答案是我最喜欢的。

However, I do prefer this

不过,我更喜欢这个

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

回答by Noam Manos

To check if file is empty or has only white spaces, you can use grep:

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

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

回答by Mike Q

Many of the answers are correct but I feel like they could be more complete / simplistic etc. for example :

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

Example 1 : Basic if statement

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

if $read_file is empty or not there stop the show with exit. More than once I have had misread the top answer here to mean the opposite.

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

Example 2 : As a function

示例 2:作为函数

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

回答by Radek 'Goblin' Pieczonka

[[ -f filename && ! -s filename ]] && echo "filename exists and is empty"

回答by Nabeel Shaikh

Easiest way for checking file is empty or not:

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

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

You can also write it on single line:

你也可以单行写:

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

回答by SamMorrowDrums

I came here looking for how to delete empty __init__.pyfiles as they are implicit in Python 3.3+ and ended up using:

我来这里是为了寻找如何删除空__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

Also (at least in zsh) using $path as the variable also breaks your $PATH env and so it'll break your open shell. Anyway, thought I'd share!

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