Linux 如何检查文件夹是否为空或文件夹文件是否使用shell脚本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4253698/
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
How to check if folder is empty or have folder file use shell-script?
提问by fifty arashi
i have a question
我有个问题
i try some function like
我尝试一些功能,如
DIR=/path/tmp/
if [ -d "$DIR" ]; then
目录=/路径/tmp/
如果 [ -d "$DIR" ]; 然后
and
和
if [ -f "$DIR" ]; then
如果 [ -f "$DIR" ]; 然后
but only check /path/tmp this path
但只检查 /path/tmp 这个路径
how can i do?
我能怎么做?
采纳答案by T.J. Crowder
You can use ls -A
for this:
您可以ls -A
为此使用:
if [ "$(ls -A "$DIR" 2> /dev/null)" == "" ]; then
# The directory is empty
fi
-A
shows all hidden files and directories except the .
and ..
that are always there, so it will be blank in an empty directory and non-blank in a directory with any files or subdirectories.
-A
显示所有隐藏的文件和目录,除了.
和..
是永远存在的,所以这将是在任何文件或子目录的目录空目录和非空白的空白。
The 2> /dev/null
throws away any error messages ls
may print (note that checking a non-existant directory will yield a false positive, but you said you already checked that it existed). Checking a directory where you do not have read access also yields a false positive.
该2> /dev/null
扔掉任何错误信息ls
可以打印(注意选中一个不存在的目录会产生假阳性,但你说你已经检查了它的存在)。检查您没有读取权限的目录也会产生误报。
回答by wizztjh
why don't use ls -v
? so will print out empty if no file such as
为什么不使用ls -v
?如果没有文件,那么将打印为空,例如
if $(ls -v $DIR)
回答by Simone
rmdir "$DIR"
rmdir "$DIR"
If $?
is 1, the directory isn't empty. If 0, it was empty and it gets removed.
如果$?
是 1,则目录不为空。如果为 0,则它为空并被删除。
You may recreate it if you don't intend to remove it: mkdir "$DIR"
如果您不打算删除它,您可以重新创建它: mkdir "$DIR"
回答by SiegeX
From the Bash FAQ#4 -- How can I check whether a directory is empty or not?
来自Bash FAQ#4 --如何检查目录是否为空?
shopt -s nullglob dotglob
files=(*)
(( ${#files[*]} )) || echo directory is empty
shopt -u nullglob dotglob
This small script fills the array files
with each file found in the path expansion *
. It then checks to see the size of the array and if it's 0 it prints 'directory is empty'. This will work with hidden files thanks to the use of dotglob
.
这个小脚本用files
在路径扩展中找到的每个文件填充数组*
。然后检查数组的大小,如果为 0,则打印“目录为空”。由于使用了dotglob
.
Note
笔记
Answers which ask to parse ls
is in general a bad idea and poor form. To understand why, read Why you shouldn't parse the output of ls
要求解析的答案ls
通常是一个坏主意和糟糕的形式。要了解原因,请阅读为什么您不应该解析 ls 的输出
回答by Daishi
This tells me if the directory is empty or if it's not, the number of files it contains.
这告诉我目录是否为空,如果不是,则告诉我它包含的文件数。
directory="/path/tmp"
number_of_files=$(ls -A $directory | wc -l)
if [ "$number_of_files" == "0" ]; then
echo "directory $directory is empty"
else
echo "directory $directory contains $number_of_files files"
fi
回答by michaeljt
To do this using only shell built-ins (it should work for all shells I have worked with):
为此,仅使用 shell 内置函数(它应该适用于我使用过的所有 shell):
if test "`echo /tmp/testdir/* /tmp/testdir/.?*`" = \
"/tmp/testdir/* /tmp/testdir/.."; then
[...]
fi
We do not check /tmp/testdir/.* because that will expand to /tmp/testdir/. /tmp/testdir/.. for an empty folder.
我们不检查 /tmp/testdir/.* 因为它会扩展到 /tmp/testdir/。/tmp/testdir/.. 用于空文件夹。
Another built-in-only variant:
另一个内置变体:
for i in /tmp/testdir/* /tmp/testdir/.?*; do
test -e $i || break
[...]
break
done
回答by Zrin
[ -z "$(find "$DIR" -maxdepth 0 -empty)" ] || echo "$DIR is empty!"
find
has predicate -empty
which tests if a directory or file is empty, so it will list the directory only if it's empty. -z
tests if output of find is empty. If it is, then the directory contains entries, if it isn't then it's empty.
find
有谓词-empty
which 测试目录或文件是否为空,因此它只会在目录或文件为空时列出该目录。-z
测试 find 的输出是否为空。如果是,则该目录包含条目,如果不是,则该目录为空。
In other code words:
换句话说:
[ -n "$(find "$DIR" -maxdepth 0 -empty)" ] && echo "$DIR is empty!"
Another quite nice one:
另一个相当不错的:
if ! ls -AU "$DIR" | read _; then
echo "$DIR is empty!"
fi