[Linux] para ver el directorio con el nombre de archivo (sin el sufijo)

1. Descripción del requisito

archivos de la siguiente lista existente:

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

Necesitamos para extraer sólo el nombre del archivo sin el sufijo, como por ejemplo: test1, test2

2. código de implementación

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

el uso 3.basename

3.1 sintaxis

basename(选项)(参数)

3.2 Opciones

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

3.3 Formato de los comandos

basename 名称 [后缀]
basename 选项

3.4 ejercicio práctico

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

Supongo que te gusta

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