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.
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:
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:
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
看到这里,就可以知道,其实该命令的用途非常广泛,上面只是指针文件名和目录名的命名特性来进行提取的一些特例而已。
${}并不是专门为提取文件名和目录名设计的命令,那么basename和dirname命令就是专门为做这一件事而已准备的了。
1、basename
该命令的作用是从路径中提取出目录名,使用方法为 dirname NAME
使用例子如下:
注:该命令不仅能提取出普通文件所的目录,它能提取出任何文件所在的目录,例如目录所在的目录,如下: