如何在 bash 中查找没有点的目录?
How to find directories without dot in bash?
我尝试查找没有点符号的文件夹。
我通过这个脚本在用户目录中搜索它:
!#/bin/bash
users=$(ls /home)
for user in $users;
do
find /home/$user/web/ -maxdepth 1 -type d -iname '*' ! -iname "*.*"
done
但我在结果中看到用户的文件夹带有点,例如 - test.uk 或 test.cf
我做错了什么?
提前致谢!
您可以使用 find
和 -regex
选项:
find /home/$user/web/ -maxdepth 1 -type d -regex '\./[^.]*$'
'\./[^.]*$'
将匹配没有任何 DOT 的名称。
问题是您的命令在 /home/username/web/
中查找目录,其中目录不包含点。
它不会检查 username
本身是否包含点。
要查看是否有任何地方有一个点,您可以使用 ipath
而不是 iname
:
!#/bin/bash
users=$(ls /home)
for user in $users;
do
find /home/$user/web/ -maxdepth 1 -type d -iname '*' ! -ipath "*.*"
done
或者更准确简洁:
#!/bin/bash
find /home/*/web/ -maxdepth 1 -type d ! -ipath "*.*"
不需要查找;只需使用扩展的 glob 来匹配任何不包含 .
的文件
shopt -s extglob
for dir in /home/*/;
do
printf '%s\n' "$dir"/!(*.*)
done
您甚至可以完全取消循环:
shopt -s extglob
printf '%s\n' /home/*/!(*.*)
要排除 /home
中包含 .
的目录,您可以在任一示例中将 /home/*/
更改为 /home/!(*.*)/
。
我尝试查找没有点符号的文件夹。 我通过这个脚本在用户目录中搜索它:
!#/bin/bash
users=$(ls /home)
for user in $users;
do
find /home/$user/web/ -maxdepth 1 -type d -iname '*' ! -iname "*.*"
done
但我在结果中看到用户的文件夹带有点,例如 - test.uk 或 test.cf
我做错了什么?
提前致谢!
您可以使用 find
和 -regex
选项:
find /home/$user/web/ -maxdepth 1 -type d -regex '\./[^.]*$'
'\./[^.]*$'
将匹配没有任何 DOT 的名称。
问题是您的命令在 /home/username/web/
中查找目录,其中目录不包含点。
它不会检查 username
本身是否包含点。
要查看是否有任何地方有一个点,您可以使用 ipath
而不是 iname
:
!#/bin/bash
users=$(ls /home)
for user in $users;
do
find /home/$user/web/ -maxdepth 1 -type d -iname '*' ! -ipath "*.*"
done
或者更准确简洁:
#!/bin/bash
find /home/*/web/ -maxdepth 1 -type d ! -ipath "*.*"
不需要查找;只需使用扩展的 glob 来匹配任何不包含 .
shopt -s extglob
for dir in /home/*/;
do
printf '%s\n' "$dir"/!(*.*)
done
您甚至可以完全取消循环:
shopt -s extglob
printf '%s\n' /home/*/!(*.*)
要排除 /home
中包含 .
的目录,您可以在任一示例中将 /home/*/
更改为 /home/!(*.*)/
。