[英]bash loop through files that have no extension
related to: Loop through all the files with a specific extension相关: 遍历具有特定扩展名的所有文件
I want to loop through files that matches a pattern:我想遍历匹配模式的文件:
for item in ./bob* ; do
echo $item
done
I have a file list like:我有一个文件列表,如:
bob
bobob
bobob.log
I only want to list files that have no extension:我只想列出没有扩展名的文件:
bob
bobob
what is the best way to archive this?存档的最佳方法是什么? - can I do it in the loop somehow or do I need an if statement within the loop?
- 我可以在循环中以某种方式执行它还是需要在循环中使用 if 语句?
In bash
you can use features of xtended globbing :在
bash
,您可以使用xtended globbing的功能:
shopt -s extglob
for item in ./bob!(*.*) ; do
echo $item
done
You can put shopt -s extglob
in your .bashrc
file to enable it.您可以将
shopt -s extglob
放入您的.bashrc
文件中以启用它。
Recent Bash versions have regular expression support:最近的 Bash 版本支持正则表达式:
for f in *
do
if [[ "$f" =~ .*\..* ]]
then
: ignore
else
echo "$f"
fi
done
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.