Bash从长路径中提取文件基本名称 [英] Bash extracting file basename from long path
问题描述
在bash
中,我试图从目录中glob
列出文件列表,以作为程序的输入.但是我也想给这个程序一个文件名列表
In bash
I am trying to glob
a list of files from a directory to give as input to a program. However I would also like to give this program the list of filenames
files="/very/long/path/to/various/files/*.file"
所以我可以这样使用它.
So I could use it like that.
prompt> program -files $files -names $namelist
如果glob
给我:
/very/long/path/to/various/files/AA.file /very/long/path/to/various/files/BB.file /very/long/path/to/various/files/CC.file /very/long/path/to/various/files/DD.file /very/long/path/to/various/files/ZZ.file
我想获取AA BB CC DD ZZ的列表来输入我的程序,而无需使用长路径名和文件扩展名. 但是我不知道如何开始!任何提示,不胜感激!
I'd like to get the list of AA BB CC DD ZZ to feed my program without the long pathname and file extension. However I have no clue on how start there ! Any hint much appreciated !
推荐答案
最好使用数组保存文件名.字符串变量将不处理包含空格的文件名.
It's better to use an array to hold the filenames. A string variable will not handle filenames which contain spaces.
此外,您无需使用basename
命令.而是使用bash的内置字符串操作.
Also, you don't need to use the basename
command. Instead use bash's built-in string manipulation.
尝试一下:
files=( /very/long/path/to/various/files/*.file )
for file in "${files[@]}"
do
filename="${file##*/}"
filenameWithoutExtension="${filename%.*}"
echo "$filenameWithoutExtension"
done
这篇关于Bash从长路径中提取文件基本名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!