删除没有具有特定名称的子目录的目录
Remove directories that doesn't have a subdirectory with a certain name
我想删除所有没有 directoryB
的目录。树结构如下:
--directory1
|
directoryA---directoryB
|
directoryC
|
directoryD---directoryB
我在 directory1
中尝试了以下脚本:
du -a -h --max-depth=1 | sort -hr
所以我知道我要删除的所有目录的大小都是 8k。如果我在外部尝试下面的脚本 directory1
几乎可以显示内部的所有内容。
find directory1 -maxdepth 1 -type d -size 8
我认为我的安全选择是删除没有 directoryB
的目录。我该怎么做?
我认为您不需要 find。一个简单的循环就可以了。
for d in directory1/*/; do
if ! test -d "${d}directoryB"; then
echo rm -r "${d}"
fi
done
如果您对输出满意,请删除 echo。
find .
在当前工作目录中查找
-type d
仅文件夹
-depth
深度优先模式[1]
-depth 1
强制深度为 1
! -exec test -e "{}/dirB/" \;
如果此文件不包含dirB
-print
打印路径
find . -type d -depth -depth 1 ! -exec test -e "{}/dirB/" \; -print
如果输出看起来不错,请将 -print
替换为 -exec rm -r "{}" \;
to remove the folders:
find . -type d -depth -depth 1 ! -exec test -e "{}/dirB/" \; -exec rm -rf {} \;
示例:
$ tree
.
|-- dirA
| `-- dirB
|-- dirC
| `-- dirX
`-- dirD
`-- dirB
6 directories, 0 files
$
$
$ find . -type d -depth -depth 1 ! -exec test -e "{}/dirB/" \; -exec rm -rf {} \;
$
$ tree
.
|-- dirA
| `-- dirB
`-- dirD
`-- dirB
4 directories, 0 files
$
我想删除所有没有 directoryB
的目录。树结构如下:
--directory1
|
directoryA---directoryB
|
directoryC
|
directoryD---directoryB
我在 directory1
中尝试了以下脚本:
du -a -h --max-depth=1 | sort -hr
所以我知道我要删除的所有目录的大小都是 8k。如果我在外部尝试下面的脚本 directory1
几乎可以显示内部的所有内容。
find directory1 -maxdepth 1 -type d -size 8
我认为我的安全选择是删除没有 directoryB
的目录。我该怎么做?
我认为您不需要 find。一个简单的循环就可以了。
for d in directory1/*/; do
if ! test -d "${d}directoryB"; then
echo rm -r "${d}"
fi
done
如果您对输出满意,请删除 echo。
find .
在当前工作目录中查找-type d
仅文件夹-depth
深度优先模式[1]-depth 1
强制深度为 1! -exec test -e "{}/dirB/" \;
如果此文件不包含dirB
-print
打印路径
find . -type d -depth -depth 1 ! -exec test -e "{}/dirB/" \; -print
如果输出看起来不错,请将 -print
替换为 -exec rm -r "{}" \;
to remove the folders:
find . -type d -depth -depth 1 ! -exec test -e "{}/dirB/" \; -exec rm -rf {} \;
示例:
$ tree
.
|-- dirA
| `-- dirB
|-- dirC
| `-- dirX
`-- dirD
`-- dirB
6 directories, 0 files
$
$
$ find . -type d -depth -depth 1 ! -exec test -e "{}/dirB/" \; -exec rm -rf {} \;
$
$ tree
.
|-- dirA
| `-- dirB
`-- dirD
`-- dirB
4 directories, 0 files
$