bash - 从完整路径中获取 Unix 中的文件名

标签 bash shell unix ksh

我正在尝试使用 Unix 命令从 /this/is/could/be/any/path/abc.txt 中获取 "abc.txt"。 请注意,/this/is/could/be/any/path 是动态的。

有什么想法吗?

最佳答案

bash 中:

path=/this/is/could/be/any/path/abc.txt

如果您的路径中有空格,请将其包裹在 "

path="/this/is/could/be/any/path/a b c.txt"

然后要提取路径,请使用 basename function

file=$(basename "$path")

file=${path##*/}

关于bash - 从完整路径中获取 Unix 中的文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10124314/

相关文章:

bash - 银银搜索器 : output file name when used in while-loop

linux - 读取输入文件时出错 : Key has expired

bash - 在 shell 脚本中如何验证给定主机名是否属于 localhostname 系列

linux - 从 Linux shell 发送邮件的格式不正确

shell - 检查系统上某个根CA是否受信任

linux - 如何在 linux 中打印另一个用户的当前工作目录?

php 到 shell 脚本到 php 通过无密码 ipv6 ssh 隧道获取值

linux - 如何在 linux cat 命令中一起处理单引号/撇号和空格?

linux - 创建脚本生成链接然后分析有效性,然后在系统默认浏览器中打开

linux - ptrace 防止信号在跟踪进程中中断 pselect()