[Linux] para ver o diretório sob o nome do arquivo (sem o sufixo)

1. Descrição do Requisito

Existente arquivos na lista a seguir:

[root@localhost logan]# ls
test1.txt  test2.txt

Precisamos extrair apenas o nome do arquivo sem o sufixo, tais como: test1, test2

2. código de implementação

[root@localhost logan]# for file_name in `ls ./ `;do basename $file_name .txt;done
test1
test2

uso 3.basename

3.1 Sintaxe

basename(选项)(参数)

3.2 Opções

--help:显示帮助;
--version:显示版本号。

3.3 Os formatos de comando

basename 名称 [后缀]
basename 选项

3.4 exercício prático

# 显示文件名,不包含目录
[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

Acho que você gosta

Origin www.cnblogs.com/OliverQin/p/12502790.html
Recomendado
Clasificación