linux截取文件名字是非常常见的操作,但是每次用都要谷歌搜几分钟,所以现在将本人比较常见的操作记录如下
$fullfile=/the/path/foo.txt
$fullname=$(basename $fullfile)
$dir=$(dirname $fullfile)
$filename=$(echo $fullname | cut -d . -f1)
$extension=$(echo $fullname | cut -d . -f2)
$ echo $dir , $fullname , $filename , $extension
/the/path , foo.txt , foo , txt
Shell 截取文件名和后缀 | Cloud's Blog (zuyunfei.com)
Linux shell中提取文件名和路径 - 鲁娜的博客 | Luna's Blog (dulunar.github.io)
初步认为使用awk这种方式是比较有效的
例子:循环获取文件名之中以.为分割符的第三位
ls |head |while read id;do str=$id;file=$(basename $str);name=`echo $str|awk -F '.' '{print $3}'`;echo $name;done
网友评论