linux命令basename:去掉路径和扩展名

2025-10-18 18:03:58

1、basename的语法是:basename[选项][参数]

其中:

选项:为有路径信息的文件名,如/home/test/test.txt

参数:指文件扩展名

2、如攀耕果在编程过程中,想取得纯粹的文件名,则言争该命令将非常有用。下面举两个例子:

3、需要把某个路径下的文件名赋值给变量file_name:

假设文件的路径是/home/test/test.txt,把test赋值给file_name:

[c.plm@localhost ~]$ file_name=`basename /home/test/test.txt `

[c.plm@localhost ~]$ echo $file_name

test.txt

[c.plm@localhost ~]$ 

linux命令basename:去掉路径和扩展名

4、需鉴哄追要把某个路径下的文件名赋值给变量file_name,并去掉扩展名:

同样假设文件的路径是/home/test/test.txt,把test赋值给file_name:

[c.plm@localhost ~]$ file_name=`basename /home/test/test.txt  .txt`

[c.plm@localhost ~]$ echo $file_name

test

[c.plm@localhost ~]$ 

linux命令basename:去掉路径和扩展名

5、linux哲学是,用功能单一的命令,通过组合、输入、输出,完成复杂的任务。我每天都会写出3-5个命令示例,希望对您有所帮助。

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢