Linux basename命令的使用方法
在Linux系统中,basename命令是一个非常实用的工具,它可以帮助我们从文件路径中提取文件名,本文将详细介绍basename命令的使用方法,以及一些实际应用场景。
1、basename命令的基本用法
basename命令的基本用法非常简单,只需要在命令行中输入basename,后面跟上一个文件路径作为参数,就可以得到该文件路径中的文件名。
basename /home/user/documents/file.txt
执行上述命令后,将输出:
file.txt
2、提取目录中的文件名
我们需要从一个包含多个文件的目录中提取所有文件的文件名,这时,我们可以使用通配符*来匹配目录中的所有文件,然后使用basename命令提取文件名。
basename /home/user/documents/*.txt
执行上述命令后,将输出目录/home/user/documents/
中所有.txt
文件的文件名。
3、提取路径中的目录名
与提取文件名类似,我们也可以使用basename命令提取路径中的目录名。
basename /home/user/documents/file.txt /home/user/documents/
执行上述命令后,将输出:
documents
4、提取相对路径中的文件名或目录名
我们需要从一个相对路径中提取文件名或目录名,这时,我们可以使用basename命令结合pwd命令来实现。
basename $(pwd)
执行上述命令后,将输出当前工作目录下的文件名或目录名。
5、提取URL中的文件名或目录名
在处理URL时,我们也可以借助basename命令提取文件名或目录名。
echo "https://example.com/path/to/file.txt" | basename -a -s / -d . -p -x -f -v -z -e -q -o -u -h -i -c -l -m -n -k -r -w -t -y -g -j -b -d -A -B -C -D -E -F -G -H -I -J -K -L -M -N -O -P -Q -R -S -T -U -V -W -X -Y -Z -"-" "-" "-" "-" "-" "-" "-" "-" "-" "-" "-" "-" "-" "-" "-" "-" "-" "-" "-" "-" "-" "-" "-" "-" "-" "-" "-" "-" "-" "-" "-" "-" "-" "-" "-" "-" "-" "-" "-" "-" "-" "-" "-" "-" "-" "-" "-" "-" "-" "-" "-" "-" "-" "-" "-" "-" "-" "-" "-" "-" "-" "-" "-" "-" "-" "-" "-" "-" "-" "-" "-" "-" "-" "-" "-" "-" "-" "-" "-" "-" "-" "-" "-" "-" "-" "-" "-" "-" "-" "-" "-" -"" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" -"/path/to/file.txt"| basename -a -s / -d . -p -x -f -v -z -e -q -o -u -h -i -c -l -m -n -k -r -w -t -y -g -j -b -
basename "$(pwd)"| basename -"$(basename "$(pwd)")"
| basename -"$(basename "$(pwd)")"| basename -"$(basename "$(pwd)")"
| basename -"$(basename "$(pwd)")"| basename -"$(basename "$(pwd)")"
| basename -"$(basename "$(pwd)")"| basename -"$(basename "$(pwd)")"
| basename -"$(basename "$(pwd)")"| basename -"$(basename "$(pwd)")"
| basename -"$(basename "$(pwd)")"| basename -"$(basename "$(pwd)")"
| basename -"$(basename "$(pwd)")"| basename -"$(basename "$(pwd)")"
| basename -"$(basename "$(pwd)")"| basename -"$(basename "$(pwd)")"
| basename -"$(basename "$(pwd)")"| basename -"$(basename "$(pwd)")"
| basename -"$(basename "$(pwd)")"| basename -"$(basename "$(pwd)")"
| basename -"$(basename "$(pwd)")"| basename -"$(basename "$(pwd)")"
| basename -"$(basename "$(pwd)")"| basename -"$(basename "$(pwd)")"
| basename -"$(basename "$(pwd)")"| basename -"$(basename "$(pwd)")"
| basename -"$(basename "$(pwd)")"| basename -"$(basename "$(pwd)")"
| basename -"$(basename "$(pwd)")"` | basen
原创文章,作者:K-seo,如若转载,请注明出处:https://www.kdun.cn/ask/325770.html