美文网首页
Linux截取文件名

Linux截取文件名

作者: 可能性之兽 | 来源:发表于2022-01-27 21:54 被阅读0次

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

相关文章

网友评论

      本文标题:Linux截取文件名

      本文链接:https://www.haomeiwen.com/subject/bfighrtx.html