如何获得在bash特定扩展名的文件列表中只有一条线路? [英] How to get the list of files of specific extension in bash in just one line?
问题描述
这是我如何能得到 * PNG
从正
文件夹子目录中的文件:
FILES =。/正面/ * / *。PNG
如何修改此命令同时获得 *。PNG
, *。JPG
和 *。BMP
文件?只有一行。
解决方案1(感谢PiotrSliwa):
FILES =($(找到./positive/ -name'*巴纽-o -name* .JPG-o -name'* .BMP))
和做搜索不区分大小写:
FILES =($(找到./positive/ -iname'*巴纽-o -iname* .JPG-o -iname'* .BMP))
解决方案2(感谢赛勒斯):
禁用了javascript -s extglob;
FILES =(./positive/*/*.@(png|jpg|bmp))
您可以只使用找到
:
找到./positive/ -name * .PNG
为了使用它的 *。PNG *
, *。JPG
和 * .BMP
,你可以利用它代表或关键字 -o
选项。有了它,你可以加入一些 -name<名称>
前pressions:
找到./positive/ -name *巴纽-o -name * .JPG -o -name * .BMP
或替代(如果你是热衷于图形或
运算符):
找到./positive/ -name *巴纽|| -name * .JPG || -name * .BMP
此外,如果你不能确定所有的文件有小写名称 - 您可能需要使用 -iname
(不区分大小写)选项,而不是 -name
。
This is how I can get *.png
files from the positive
folder sub-directories:
FILES=./positive/*/*.png
How to modify this command to get both *.png
, *.jpg
and *.bmp
files? Just one line.
Solution 1 (thanks to PiotrSliwa):
FILES=($(find ./positive/ -name '*.png' -o -name '*.jpg' -o -name '*.bmp'))
And to make the search case insensitive:
FILES=($(find ./positive/ -iname '*.png' -o -iname '*.jpg' -o -iname '*.bmp'))
Solution 2 (thanks to Cyrus):
shopt -s extglob;
FILES=( ./positive/*/*.@(png|jpg|bmp) )
You could just use find
:
find ./positive/ -name *.png
In order to use it for *.png*
, *.jpg
and *.bmp
, you may take advantage of -o
option which stands for "or" keyword. With it, you can join a few -name <NAME>
expressions:
find ./positive/ -name *.png -o -name *.jpg -o -name *.bmp
or alternatively (if you are keen on graphical or
operator):
find ./positive/ -name *.png || -name *.jpg || -name *.bmp
Furthermore, if you are not certain that all of the files have lowercase names - you may need to use -iname
(case insensitive) option instead of -name
.
这篇关于如何获得在bash特定扩展名的文件列表中只有一条线路?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!