[Linux] to view the directory under the file name (without the suffix)

1. Description of Requirement

Existing files in the following list:

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

We need to extract only the file name without the suffix, such as: test1, test2

2. implementation code

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

3.basename usage

3.1 Syntax

basename(选项)(参数)

3.2 Options

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

3.3 Command Format

basename 名称 [后缀]
basename 选项

3.4 practical exercise

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

Guess you like

Origin www.cnblogs.com/OliverQin/p/12502790.html