annuaire
1. Description de l'exigence
Les fichiers existants dans la liste suivante:
[root@localhost logan]# ls
test1.txt test2.txt
Nous avons besoin pour extraire uniquement le nom du fichier sans le suffixe, tels que: test1, test2
2. Code de mise en œuvre
[root@localhost logan]# for file_name in `ls ./ `;do basename $file_name .txt;done
test1
test2
l'utilisation de 3.basename
3.1 Syntaxe
basename(选项)(参数)
3.2 options
--help:显示帮助;
--version:显示版本号。
3.3 Format de commande
basename 名称 [后缀]
basename 选项
3.4 exercice pratique
# 显示文件名,不包含目录
[root@localhost logan]# basename /home/logan/test1.txt
test1.txt
# 显示文件名,不包含目录与后缀
[root@localhost logan]# basename /home/logan/test1.txt .txt
test1
#----或者:-s指定移出的后缀
[root@localhost logan]# basename -s .txt /home/logan/test1.txt
test1
# 将多个参数按照顺序输出
[root@localhost logan]# basename -a /home/logan/test1.txt /home/logan/test2.txt
test1.txt
test2.txt
# 显示最后一个目录的名字
[root@localhost logan]# basename /home/logan
logan