Bash 写入超过 5 个文件的子目录的脚本
Bash script that writes subdirectories who has more than 5 files
虽然我正在努力练习我的linux技能,但我无法解决这个问题。
So its basically saying "Write a bash script that takes a name of
directory as a command argument and printf the name of subdirectories
that has more than 5 files in it."
我以为我们会使用 find
命令,但我还是想不通。我的代码是:
find directory -type d -mindepth5
但它不起作用。
这应该可以解决问题:
find directory/ -type f | sed 's/\(.*\)\/.*//g' | sort | uniq -c | sort -n | awk '{if(>5) print()}'
在这里使用 mindpeth
是没有用的,因为它只列出至少深度为 5 的目录。你说你需要包含超过 5 个文件的子目录。
find directory -type f
打印子目录中的所有文件
sed 's/\(.*\)\/.*//g'
删除文件名,只留下没有文件名的子目录列表
sort
对该列表进行排序,以便我们可以使用 uniq
uniq -c
合并重复行并写入出现的次数
sort -n
按出现次数排序(所以你最终得到一个列表:(多少次,子目录))
awk '{if(>5) print()}'
仅打印第一个 comlun 1 > 5 的那些(并且它仅打印第二列)
所以你最终得到一个子目录列表,里面至少有 5 个文件。
编辑:
建议修复具有 spaces 的路径:
而不是 awk '{if(>5) print()}'
应该有 awk '{if(>5){ =""; print(substr([=21=],2)) }}'
它将行的第一部分设置为 ""
然后打印整行没有前导 space (这是分隔符)。所以放在一起我们得到这个:
find directory/ -type f | sed 's/\(.*\)\/.*//g' | sort | uniq -c | sort -n | awk '{if(>5){ =""; print(substr([=11=],2)) }}'
你可以使用find
两次:
首先你可以使用find
和wc
来计算给定目录中文件的数量:
nb=$(find directory -maxdepth 1 -type f -printf "x\n" | wc -l)
这只是要求 find
为目录 directory
中的每个 文件 在一行上输出一个 x
,非递归地进行,然后 wc -l
计算行数,所以,实际上, nb
是 directory
.
中的文件数
如果你想知道一个目录是否包含超过5个文件,最好在找到6个文件后立即停止find
:
nb=$(find directory -maxdepth 1 -type f -printf "x\n" | head -6 | wc -l)
此处 nb
的阈值上限为 6
。
现在,如果你想为一个目录 directory
的每个子目录输出文件数(阈值为 6),你可以这样做:
find directory -type d -exec bash -c 'nb=$(find "[=12=]" -maxdepth 1 -type f -printf "x\n" | head -6 | wc -l); echo "$nb"' {} \;
其中出现的[=27=]
是第0
个参数,即{}
,find
将被directory
的子目录替换。
最后,如果文件数大于5只显示子目录名:
find . -type d -exec bash -c 'nb=$(find "[=13=]" -maxdepth 1 -type f -printf "x\n" | head -6 | wc -l); ((nb>5))' {} \; -print
最终测试((nb>5))
returns 成功与否 nb
是否大于5,如果成功,find
将-print
子目录名称。
虽然我正在努力练习我的linux技能,但我无法解决这个问题。
So its basically saying "Write a bash script that takes a name of directory as a command argument and printf the name of subdirectories that has more than 5 files in it."
我以为我们会使用 find
命令,但我还是想不通。我的代码是:
find directory -type d -mindepth5
但它不起作用。
这应该可以解决问题:
find directory/ -type f | sed 's/\(.*\)\/.*//g' | sort | uniq -c | sort -n | awk '{if(>5) print()}'
在这里使用 mindpeth
是没有用的,因为它只列出至少深度为 5 的目录。你说你需要包含超过 5 个文件的子目录。
find directory -type f
打印子目录中的所有文件
sed 's/\(.*\)\/.*//g'
删除文件名,只留下没有文件名的子目录列表
sort
对该列表进行排序,以便我们可以使用 uniq
uniq -c
合并重复行并写入出现的次数
sort -n
按出现次数排序(所以你最终得到一个列表:(多少次,子目录))
awk '{if(>5) print()}'
仅打印第一个 comlun 1 > 5 的那些(并且它仅打印第二列)
所以你最终得到一个子目录列表,里面至少有 5 个文件。
编辑:
建议修复具有 spaces 的路径:
而不是 awk '{if(>5) print()}'
应该有 awk '{if(>5){ =""; print(substr([=21=],2)) }}'
它将行的第一部分设置为 ""
然后打印整行没有前导 space (这是分隔符)。所以放在一起我们得到这个:
find directory/ -type f | sed 's/\(.*\)\/.*//g' | sort | uniq -c | sort -n | awk '{if(>5){ =""; print(substr([=11=],2)) }}'
你可以使用find
两次:
首先你可以使用find
和wc
来计算给定目录中文件的数量:
nb=$(find directory -maxdepth 1 -type f -printf "x\n" | wc -l)
这只是要求 find
为目录 directory
中的每个 文件 在一行上输出一个 x
,非递归地进行,然后 wc -l
计算行数,所以,实际上, nb
是 directory
.
如果你想知道一个目录是否包含超过5个文件,最好在找到6个文件后立即停止find
:
nb=$(find directory -maxdepth 1 -type f -printf "x\n" | head -6 | wc -l)
此处 nb
的阈值上限为 6
。
现在,如果你想为一个目录 directory
的每个子目录输出文件数(阈值为 6),你可以这样做:
find directory -type d -exec bash -c 'nb=$(find "[=12=]" -maxdepth 1 -type f -printf "x\n" | head -6 | wc -l); echo "$nb"' {} \;
其中出现的[=27=]
是第0
个参数,即{}
,find
将被directory
的子目录替换。
最后,如果文件数大于5只显示子目录名:
find . -type d -exec bash -c 'nb=$(find "[=13=]" -maxdepth 1 -type f -printf "x\n" | head -6 | wc -l); ((nb>5))' {} \; -print
最终测试((nb>5))
returns 成功与否 nb
是否大于5,如果成功,find
将-print
子目录名称。