bash 中的單獨檔案和路徑?

bash 中的單獨檔案和路徑?

如何在這樣的 bash 循環中分離路徑和檔案元素?

for file in `find /my/path -name "*.ext"`
do
    #(path,onlyfile) = separate_path_and_file $file
    #dosomethingwith $onlyfile
done

答案1

你不能。但你可以分開做。

$ foo=/usr/local/bin/bar
$ echo "${foo##*/}"
bar
$ echo "${foo%/*}"
/usr/local/bin

答案2

我會建議dirnamebasename:

對於 `find /my/path -name "*.ext"` 中的文件
    路徑=“$(目錄名稱“$檔案”)”
    onlyfile="$(基本名稱"$file")"
    # ...
完畢

相關內容