bash 查找空目录 UNIX
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2810838/
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
Finding empty directories UNIX
提问by soField
I need to find empty directories for a given list of directories. Some directories have directories inside it.
我需要为给定的目录列表找到空目录。有些目录里面有目录。
If inside directories are also empty I can say main directory is empty otherwise it's not empty.
如果内部目录也是空的,我可以说主目录是空的,否则它不是空的。
How can I test this?
我该如何测试?
For example:
例如:
A>A1(file1),A2 this is not empty beacuse of file1
B>B1(no file) this is empty
C>C1,C2 this is empty
采纳答案by Marcelo Cantos
Check whether find <dir> -type f
outputs anything. Here's an example:
检查是否find <dir> -type f
输出任何内容。下面是一个例子:
for dir in A B C; do
[ -z "`find $dir -type f`" ] && echo "$dir is empty"
done
回答by Martin
It depends a little on what you want to do with the empty directories. I use the command below when I wish to deleteall empty directories within a tree, say test
directory.
这在一定程度上取决于您想对空目录做什么。当我希望删除树中的所有空目录时,我使用下面的命令,比如test
目录。
find test -depth -empty -delete
One thing to notice about the command above is that it will also remove empty files, so use the -type doption to avoid that.
关于上述命令要注意的一件事是它还会删除空文件,因此请使用-type d选项来避免这种情况。
find test -depth -type d -empty -delete
Drop -delete
to see the files and directories matched.
拖放-delete
以查看匹配的文件和目录。
If your definition of an empty directory tree is that it contains no files then you be able to stick something together based on whether find test -type f
returns anything.
如果您对空目录树的定义是它不包含任何文件,那么您可以根据是否find test -type f
返回任何内容将某些内容粘在一起。
find
is a great utility, and RTFM early and often to really understand how much it can do :-)
find
是一个很好的实用程序,RTFM 很早就并且经常真正了解它可以做什么:-)
回答by mosg
You can use the following command:
您可以使用以下命令:
find . -type d -empty
回答by Dr. Person Person II
find directory -mindepth 1 -type d -empty -delete
This is the version that I found most interesting. If executed from inside directory, it will delete all empty directories below (a directory is considered empty if it only contains empty directories).
这是我觉得最有趣的版本。如果从内部目录执行,它将删除下面的所有空目录(如果目录只包含空目录,则认为该目录为空)。
The mindepth option prevents the directory itself from being deleted if it happens to be empty.
如果目录恰好为空,则 mindepth 选项可防止目录本身被删除。
回答by user7194913
find . -type d -empty
find . -type d -empty
finds and lists empty directories and sub-directories in the current tree. E.g. resulting list of empty dirs and subdirs:
在当前树中查找并列出空目录和子目录。例如,空目录和子目录的结果列表:
./2047
./2032
./2049
./2063
./NRCP26LUCcct1/2039
./NRCP26LUCcct1/2054
./NRCP26LUCcct1/2075
./NRCP26LUCcct1/2070
No operation is made on the directories. They are simply listed. This works for me.
不对目录进行任何操作。它们被简单地列出。这对我有用。
回答by olibre
Just find empty dirs
只需找到空目录
In order to just find empty directories (as specified in the question title), the mosg's answeris correct:
为了只找到空目录(如问题标题中所指定),mosg 的答案是正确的:
find -type d -empty
But -empty
may not be available on very old find
versions (this is the case of HP-UXfor example). If this is your case, see the techniques described in below section Is a directory empty?.
但-empty
可能不适用于非常旧的find
版本(例如,HP-UX就是这种情况)。如果是这种情况,请参阅下面部分中描述的技术目录是否为空?.
Delete empty dirs
删除空目录
This is a bit tricky: Suppose a directory MyDir
contains empty directories. After removing these empty directories, MyDir
will become an empty directory and should also be removed. Therefore I use the command rmdir
with the option --parents
(or -p
) that also removes parent directories when possible:
这有点棘手:假设一个目录MyDir
包含空目录。删除这些空目录后,MyDir
会变成空目录,也应该删除。因此,我将命令rmdir
与选项--parents
(or -p
)一起使用,该选项还会在可能的情况下删除父目录:
find -type d -empty -exec rmdir -vp --ignore-fail-on-non-empty {} +
On older find
version the statement +
is not yet supported, therefore you may use ;
instead:
在旧find
版本中,+
尚不支持该语句,因此您可以;
改用:
find -type d -empty -exec rmdir -vp --ignore-fail-on-non-empty {} `;`
Is a directory empty?
目录是否为空?
Most of these answers explain how to check if a directory is empty. Therefore I provide here the three different techniques I know:
这些答案中的大多数解释了如何检查目录是否为空。因此,我在这里提供我所知道的三种不同的技术:
[ $(find your/dir -prune -empty) = your/dir ]
d=your/dir if [ x$(find "$d" -prune -empty) = x"$d" ] then echo "empty (directory or file)" else echo "contains files (or does not exist)" fi
a variation:
d=your/dir if [ x$(find "$d" -prune -empty -type d) = x"$d" ] then echo "empty directory" else echo "contains files (or does not exist or is not a directory)" fi
Explanation:
find -prune
is similar thanfind -maxdepth 0
using less charactersfind -type d
prints directories onlyfind -empty
prints the empty directories and files> mkdir -v empty1 empty2 not_empty mkdir: created directory 'empty1' mkdir: created directory 'empty2' mkdir: created directory 'not_empty' > touch not_empty/file > find empty1 empty2 not_empty -prune -empty empty1 empty2
(( ${#files} ))
This trick is 100%
bash
but invokes (spawns) a sub-shell. The idea is from Bruno De Fraineand improved by teambob's comment. I advice this one if you use bashand if your script does not have to be portable.files=$(shopt -s nullglob dotglob; echo your/dir/*) if (( ${#files} )) then echo "contains files" else echo "empty (or does not exist or is a file)" fi
Note:no difference between an empty directory and a non-existing one (and even when the provided path is a file).
[ $(ls -A your/dir) ]
This trick is inspired from nixCraft's articleposted in 2007. Andrew Tayloranswered in 2008 and gr8can8dianin 2011.
if [ "$(ls -A your/dir)" ] then echo "contains files" else echo "empty (or does not exist or is a file)" fi
or the one-line bashism version:
[[ "$(ls -A your/dir)" ]] && echo "contains files" || echo "empty or ..."
Note:
ls
returns$?=2
when the directory does not exist. But no difference between a file and an empty directory.
[ $(find your/dir -prune -empty) = your/dir ]
d=your/dir if [ x$(find "$d" -prune -empty) = x"$d" ] then echo "empty (directory or file)" else echo "contains files (or does not exist)" fi
一个变体:
d=your/dir if [ x$(find "$d" -prune -empty -type d) = x"$d" ] then echo "empty directory" else echo "contains files (or does not exist or is not a directory)" fi
解释:
find -prune
类似于find -maxdepth 0
使用较少的字符find -type d
仅打印目录find -empty
打印空目录和文件> mkdir -v empty1 empty2 not_empty mkdir: created directory 'empty1' mkdir: created directory 'empty2' mkdir: created directory 'not_empty' > touch not_empty/file > find empty1 empty2 not_empty -prune -empty empty1 empty2
(( ${#files} ))
这个技巧是 100%
bash
但调用(产生)一个子壳。这个想法来自Bruno De Fraine并由teambob的评论改进。如果您使用bash并且您的脚本不必是可移植的,我建议您使用此方法。files=$(shopt -s nullglob dotglob; echo your/dir/*) if (( ${#files} )) then echo "contains files" else echo "empty (or does not exist or is a file)" fi
注意:空目录和不存在的目录之间没有区别(即使提供的路径是文件)。
[ $(ls -A your/dir) ]
这个技巧的灵感来自于nixCraft在 2007 年发表的文章。Andrew Taylor在 2008 年回答,gr8can8dian在 2011 年回答。
if [ "$(ls -A your/dir)" ] then echo "contains files" else echo "empty (or does not exist or is a file)" fi
或单行 bashism 版本:
[[ "$(ls -A your/dir)" ]] && echo "contains files" || echo "empty or ..."
注意:当目录不存在时
ls
返回$?=2
。但是文件和空目录之间没有区别。
回答by evandrix
How about rmdir *
? That command will fail on non-empty directories.
怎么样rmdir *
?该命令将在非空目录上失败。
回答by Paused until further notice.
This recursive function would seem to do the trick:
这个递归函数似乎可以解决问题:
# Bash
findempty() {
find ${1:-.} -mindepth 1 -maxdepth 1 -type d | while read -r dir
do
if [[ -z "$(find "$dir" -mindepth 1 -type f)" ]] >/dev/null
then
findempty "$dir"
echo "$dir"
fi
done
}
Given this example directory structure:
鉴于此示例目录结构:
. |-- dir1/ |-- dir2/ | `-- dirB/ |-- dir3/ | `-- dirC/ | `-- file5 |-- dir4/ | |-- dirD/ | `-- file4 `-- dir5/ `-- dirE/ `-- dir_V/
The result of running that function would be:
运行该函数的结果将是:
./dir1 ./dir5/dirE/dir_V ./dir5/dirE ./dir5 ./dir2/dirB ./dir2
which misses /dir4/dirD
. If you move the recursive call findempty "$dir"
after the fi
, the function will include that directory in its results.
错过了/dir4/dirD
。如果移动递归调用findempty "$dir"
后fi
,功能将包括其结果目录。
回答by Vijay
find . -name -type d -ls |awk '(==0){print }'
回答by James Sumners
I created a simple structure as follows:
我创建了一个简单的结构如下:
test/
test/test2/
test/test2/test2.2/
test/test3/
test/test3/file
The test/test3/file
contains some junk text.
将test/test3/file
包含一些垃圾文字。
Issuing find test -empty
returns "test/test2/test2.2
" as the only empty directory.
发出find test -empty
返回“ test/test2/test2.2
”作为唯一的空目录。