[英]BASH How to loop through directories and extract filenames
If I have the following folder structure, how would I loop through each directory, extract the file names in each directory, and get the first bit of text in the array? 如果我具有以下文件夹结构,该如何遍历每个目录,提取每个目录中的文件名,并获取数组中的第一行文本?
Folder1
File1-test
File2-test
Folder2
File3-test
File4-test
I need File1, File2, File3 etc. 我需要File1,File2,File3等。
find . -type f -name "*-test" | awk -F/ '{print $NF}' | sed 's/-test//'
Look for all files match *-test
pattern, then split path using /
and print only the last field, then remove the -test
part. 查找所有匹配
*-test
模式的文件,然后使用/
分割路径并仅打印最后一个字段,然后删除-test
部分。
Alternative method 替代方法
find . -type f -name "*-test" | awk -F/ '{print $NF}' | awk -F- '{print $1}'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.