你如何在bash中获取目录中的第一个文件?首先是 shell glob 首先找到的内容。
我在 bash 中搜索目录中的第一个文件,这让我看到了一个带有非常具体要求的旧帖子。我想为后代记录我对一般问题的解决方案,并为人们提供一个放置他们想要分享的替代解决方案的地方。
you*_*aki 11
要获取当前目录中的第一个文件,您可以将扩展放在数组中并获取第一个元素:
files=(*)
echo "${files[0]}"
# OR
echo "$files" # since we are only concerned with the first element
Run Code Online (Sandbox Code Playgroud)
假设您当前的目录包含多个目录,您可以循环遍历并获取第一个文件,如下所示:
for dir in *; do
files=($dir/*)
echo "${files[0]}"
done
Run Code Online (Sandbox Code Playgroud)