Check if a file is empty in Bash
Often you'll need to check if a file is empty, and luckily there are some easy ways to do this using Bash in the terminal or even using a Bash script.
Use the test command with the -s option flag in Bash to check if a file is empty
The test command is one of the most useful shell built-in commands and has many options for files and directories. The -s option of the test command checks if the size of a file is greater than 0.
If the file size is greater than zero, the command will return true. Otherwise, it will return false.
The following demonstrates test -s
the use of . We will use examples to explain the test command.
In this example, we have a file called newFile.txt. We will create a Bash script called empty_test.sh as follows:
#!/bin/bash
test -s newFile.txt && echo "file has something" || echo "file is empty"
If the file newFile.txt is empty, the shell will have the following output:
On the other hand, if newFile.txt has any content, for example:
Hello!
The output will then look like this:
The above command is a straightforward way to check if the selected file is empty. If test -s newFile.txt
evaluates to true, it will print file has something.
Otherwise, it prints file is empty. This command can be used in a Bash script or run directly in a terminal.
It is important to note that if the command is used in a different directory or level than the file being checked, the full file path needs to be written.
There is an alternative way to write the above command. In Bash, [ is also a shell builtin and is equivalent to the test command:
#!/bin/bash
[ -s newFile.txt ] && echo "file has something" || echo "file is empty"
This command is equivalent to the one we used previously.
We can also use test -s
the command to find all empty files in a given directory:
#!/bin/bash
for file in *.txt;
do if [ ! -s $file ];
then echo $file;
fi;
done
The above command will print the names of all empty .txt files in the directory. You can easily customize it to check for different types of files by simply replacing .txt with another extension.
Check if a file is empty using ls command in Bash
The ls command lists all the files and subdirectories in the current directory. The ls command can also be used with the -s option flag to display the size of the file next to the file name.
For example, if we run the command in a test directory we created that contains two subdirectories and a .docx file ls -s
, we will get the following output:
It is important to note that in macOS, this command will display the size of all subdirectories as 0. This is regardless of whether the subdirectories contain other content.
Also note that the default shell used is zsh (not bash). However, this is not a difference caused by the shell but by the operating system itself.
To illustrate this result in a Linux environment, specifically using bash, we created a test folder that contains two empty directories named Test 1 and Test 2. It also contains two text files, test.txt has a small string in it and test2.txt is a blank text file.
Subdirectories are displayed as 4 KB regardless of the size of the contents. This does not represent the actual contents of the folder; it is the space taken up by the entries themselves.
A better approach is to use the ls command with the -l option flag, as this will display more information in a tabular format:
ls -l
We will run this command in the same test directory as before. The output will be as follows:
As shown above, ls -l
the command provides more information and displays the correct size of the subdirectories.
To view sizes in KB, MB, and GB, use the -h option -lh
. Include the -h flag to make the data easier to read.
The output will be similar to the following:
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
在PowerShell中检查字符串是否为空
Publish Date:2024/02/08 Views:842 Category:编程语言
-
本文讨论了一种可以用于检查给定字符串是否为空的字符串方法。在本文中,我们将讨论在Powershell中检查给定字符串是否为null或空的方法。
在 C# 中检查对象是否为空
Publish Date:2024/02/02 Views:132 Category:编程语言
-
在 C# 中,有两种主要方法可用于检查对象是否为 null:==运算符和 is 运算符。本教程将讨论在 C# 中检查对象是否为空的方法。在 C# 中使用 == 运算符检查空对象
在 C# 中检查文本框是否为空
Publish Date:2024/02/01 Views:169 Category:编程语言
-
有两种主要方法可用于检查 C# 中文本框是否为空:String.IsNullOrEmpty()函数和 TextBox.Text.Length 属性。本教程将讨论如何在 C# 中检查文本框是否为空。使用 C# 中的 String.IsNullOrEmpty() 函数检查文本框
在 C# 中检查一个字符串是否为空或 null
Publish Date:2024/01/16 Views:152 Category:编程语言
-
string.IsNullOrEmpty()方法用于检查字符串在 C# 中是否为 null 或 string.Empty 值。检查 C# 中的字符串是空或者 null 如果我们要检查其中包含 null 值或""值的字符串,可以在 C# 中使用 string.IsNullOrEmpty() 方
在 C# 中检查列表是否为空
Publish Date:2024/01/03 Views:165 Category:编程语言
-
有两种主要方法可用于检查 C# 中的列表是否为空:List.Count 方法和 List.Any()函数。
检查 Python 中的集合是否为空
Publish Date:2023/12/22 Views:132 Category:Python
-
本教程介绍了如何在 Python 中检查集合是否为空。本教程将讨论在 Python 中检查一个集合是否为空的各种方法。我们将定义一个方法 empty_set(),如果集合为空,它将返回 True,否则返回 False。
在 Java 中检查对象是否为空
Publish Date:2023/10/10 Views:68 Category:Java
-
本教程介绍了在 Java 中检查对象是否为空的方法。本文将通过一些简单的例子来介绍 Java 中检查对象是否为空的方法。Java 使用 == 操作符检查 Object 是否为空
如何在 Java 中检查一个数组是否为空
Publish Date:2023/09/19 Views:108 Category:Java
-
本文介绍了如何在 Java 中检查一个数组是否为空,还列出了一些示例代码来了解空检查过程。
在 Java 中检查 Int 是否为空
Publish Date:2023/09/10 Views:157 Category:Java
-
在本文中,我们将学习如何在 Java 中检查 int 是否为 null。为了理解这个概念,我们需要对数据类型 int 进行一些基本的了解。让我们继续。Java 中的 int 可以为空吗