Bash 检查文件夹是否有内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20456666/
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
Bash checking if folder has contents
提问by Syler
I'm trying to create a Bash script that will delete everything in my .waste
directory. I have a basic script I wrote but I want it to first check if the .waste
directory has contents, and if so, to echo out a simple "Folder already empty!"
message. I'm not too savvy about if
and if else
statements, and I don't know what the [ ]
equation needs to check for presence.
我正在尝试创建一个 Bash 脚本,该脚本将删除我.waste
目录中的所有内容。我有一个我编写的基本脚本,但我希望它首先检查.waste
目录是否有内容,如果有,则回显一条简单的"Folder already empty!"
消息。我对if
andif else
语句不太了解,我不知道[ ]
需要什么等式来检查是否存在。
Basic code:
基本代码:
#! /bin/bash
echo "The files have been deleted:"
cd /home/user/bin/.waste/
ls
rm -rf /home/user/bin/.waste/*
(P.S. not sure if the asterisk is correct at the end, I did try the script with it and I recall it deleted everything in the bin
directory as well)
(PS 不确定最后星号是否正确,我确实尝试过使用它的脚本,我记得它也删除了bin
目录中的所有内容)
回答by janos
You can check if a directory is empty using find
, and processing its output:
您可以使用 来检查目录是否为空find
,并处理其输出:
#!/bin/sh
target=
if find "$target" -mindepth 1 -print -quit 2>/dev/null | grep -q .; then
echo "Not empty, do something"
else
echo "Target '$target' is empty or not a directory"
fi
That is:
那是:
- Use
find
to find the first filesystem entry under$target
(-mindepth 1
), print it (-print
), and stop processing (-quit
)- Redirect
stderr
to suppress any error messages (= noise)
- Redirect
- Check if the output of the
find
command is empty usinggrep -q .
grep -q .
will exit after processing at most one character. If it sees a character it exits with success, if it doesn't (its input is empty) then it exits with failure.
- If the output of the
find
command is not empty, then the directory is not empty, andgrep -q .
exits with success. - If the output of the
find
command is empty, then$target
is either an empty directory, or not a directory (does not exist), andgrep -q .
exits with failure.
- 使用
find
找到下的第一个文件系统条目$target
(-mindepth 1
),打印(-print
),并停止处理(-quit
)- 重定向
stderr
以抑制任何错误消息(= 噪音)
- 重定向
- 使用检查
find
命令的输出是否为空grep -q .
grep -q .
最多处理一个字符后退出。如果它看到一个字符,它会成功退出,如果没有(它的输入为空),则它会失败退出。
- 如果该
find
命令的输出不为空,则该目录不为空,并grep -q .
成功退出。 - 如果
find
命令的输出为空,则$target
要么是空目录,要么不是目录(不存在),并grep -q .
失败退出。
The reason we have to rely on the stdout
of find
rather than its own exit code directly is that there's no way to make the find
command use distinguishable exit codes in case files were found or not.
我们必须直接依赖stdout
offind
而不是它自己的退出代码的原因是没有办法让find
命令使用可区分的退出代码,以防文件被找到。
Instead of piping to grep -q
, another alternative would be to capture the output of find
and check if it's an empty string or not.
除了管道到grep -q
,另一种替代方法是捕获 的输出find
并检查它是否为空字符串。
#!/bin/sh
target=
if [ "$(find "$target" -mindepth 1 -print -quit 2>/dev/null)" ]; then
echo "Not empty, do something"
else
echo "Target '$target' is empty or not a directory"
fi
Capturing command output like this uses a sub-shell.
I think the solution using grep
is probably faster,
but I haven't tested it.
像这样捕获命令输出使用子外壳。我认为使用的解决方案grep
可能更快,但我还没有测试过。
回答by kdubs
GNU find will let you do this
GNU find 会让你这样做
find . -maxdepth 0 -empty -exec echo {} is empty. \;
pretty quick and no pipes
非常快,没有管道
回答by Reese Murdock
Sorry, I don't have enough rep to answer Dominik's comment with a comment, so this is the best I can do...
对不起,我没有足够的代表来回答多米尼克的评论,所以这是我能做的最好的......
My find on Solaris has no maxdepth option... how can I achieve the same? – Dominik Nov 14 at 12:46
我在 Solaris 上的发现没有 maxdepth 选项......我怎样才能达到同样的目标?– 多米尼克 11 月 14 日 12:46
I can't say for sure on earlier versions, but on Solaris 10 or better:
我不能肯定早期版本,但在 Solaris 10 或更高版本上:
find . ! -name . -prune