(Transfer) Some methods of extracting file names and directory names in Linux shell

Reprinted from: http://blog.csdn.net/ljianhui/article/details/43128465

And thanks to the original author for the summary, here as a collection.

 

Many times when using the Linux shell, we need to process the file name or directory name. The usual operation is to extract the file name from the path, extract the directory name from the path, extract the file suffix name and so on. For example, extract the file name file.txt from the path /dir1/dir2/file.txt, extract the directory /dir1/dir2, extract the file suffix txt, and so on.

Two common methods are described below to perform related operations.

 

1. Use ${}

1. ${var##*/}
The function of this command is to remove the last '/' character from the left of the variable var and the content on the left, and return the last '/' from the left (excluding the character) to the right of the content. The usage examples and results are as follows:

 

As can be seen from the running results, using this command, we can extract the file name file.txt we need.

If you use it in a shell program file, you can use a variable to save the result and use it again, such as file=${var##*/}


2. ${var##*.}
The function of this command is to remove the last '.' character from the left of the variable var and the content on the left, and return the last '.' from the left (excluding the character) to the right of the content. The usage examples and results are as follows:

As can be seen from the running results, using this command, we can extract the file suffix we need.

If the file has more than one suffix, for example, file.tar.gz, the command ${var##*.} can only extract the last suffix, and what should I do when I want to extract tar.gz? Then use the ${var#*.} command described below.


3. ${var#*.}
The function of this command is to remove the first '.' character from the left of the variable var and the content on the left, and return the first '.' from the left (excluding the character) to the right part of the content. The usage examples and results are as follows:

As can be seen from the running results, using this command, multiple suffixes of the file can be extracted.

4. ${var%/*}
The use of this command is to remove the first '/' character from the right side of the variable var and the content on the right side, and return the first '/' from the right side (excluding the the character) to the left of the content. The usage examples and results are as follows:

As can be seen from the running results, using this command, we can extract the directory where the files we need are located.

5. ${var%%.*}
The use of this command is to remove the last '.' from the right side of the variable var character and the content to the right of it, returning the content to the left of the last '.' (excluding the character) from the right. The usage examples and results are as follows:

When we need to create a directory with the same name as the file name (without suffix) corresponding to the corresponding file, we can use this command to operate. For example, the situation of decompressing files is similar. When we compress the file file.zip, a directory named file will be created in the same directory as file.zip.

6. ${} summary

In fact, ${} is not specifically used to extract file names or directory names. It is used to extract and replace variables. It can extract a lot of content, not necessarily the '/' in the above five examples. or'.'. That said, the usage above is just a special case of its usage.

Seeing these commands above may make people feel very difficult to understand and remember, but in fact, they are all regular.
#: Indicates the first one from the left
%: Indicates the first count from the right
##: Indicates the last one from the left
%%: Indicates the last one from the right
换句话来说,#总是表示左边算起,%总是表示右边算起。

*:表示要删除的内容,对于#和##的情况,它位于指定的字符(例子中的'/'和'.')的左边,表于删除指定字符及其左边的内容;对于%和%%的情况,它位于指定的字符(例子中的'/'和'.')的右边,表示删除指定字符及其右边的内容。这里的'*'的位置不能互换,即不能把*号放在#或##的右边,反之亦然。

例如:${var%%x*}表示找出从右边算起最后一个字符x,并删除字符x及其右边的字符。

看到这里,就可以知道,其实该命令的用途非常广泛,上面只是指针文件名和目录名的命名特性来进行提取的一些特例而已。

二、basename和dirname

${}并不是专门为提取文件名和目录名设计的命令,那么basename和dirname命令就是专门为做这一件事而已准备的了。

1、basename

该命令的作用是从路径中提取出文件名,使用方法为basename NAME [SUFFIX]。
1)从路径中提出出文件名(带后缀),例子如下:
2)从上面命令的用法中可以看到,后缀(SUFFIX)是一个可选项。所以,若只想提取出文件名file,而不带有后缀,还可以在变量的后面加上后缀名,例子如下:
2、 dirname
该命令的作用是从路径中提取出目录名,使用方法为 dirname NAME
使用例子如下:
这样就提取出了file.txt文件所在的目录。

注:该命令不仅能提取出普通文件所的目录,它能提取出任何文件所在的目录,例如目录所在的目录,如下:
它提取出了目录dir2所在的目录dir1.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326679908&siteId=291194637