Some methods of extracting file name and directory name from Linux shell

Time:2021-2-25

Many times when using 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 and so on. For example, from path / dir1 / dir2/ file.txt Also extract the file name file.txt , extract the directory / dir1 / dir2, extract the file suffix TXT, etc.

Here are two common methods for related operations.

1、 Use ${}

1、${var##*/}
The function of this command is to remove the last ‘/’ character and its left content of the variable var calculated from the left, and return the right content of the last ‘/’ calculated from the left (excluding this character). Examples and results are as follows:

From the running results, we can see that using this command, we can extract the file name we need file.txt .

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

2、${var##*.}

The function of this command is to remove the last character of VaR calculated from the left and its left content, and return the right content of the last character calculated from the left (excluding this character). Examples and results are as follows:

From the running results, we can see that 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, but I want to extract it tar.gz What should I do when I go to school? Then we need to use the following command ${var # *.}.

3、${var#*.}

The function of this command is to remove the first character of VaR from the left and its left contents, and return the contents of the right part of VaR from the left. Examples and results are as follows:

It can be seen from the running results that multiple suffixes of files can be extracted by using this command.

4、${var%/*}

The use of this command is to remove the first ‘/’ character of VaR from the right and its right content, and return the left content of the first ‘/’ (excluding the character) from the right. Examples and results are as follows:

From the running results, we can see that using this command, we can extract the directory of the files we need

5、${var%%.*}

The use of this command is to remove the last character of VaR from the right and its right content, and return the left content of the last character of VaR from the right (excluding this character). Examples and results are as follows:

When we need to create a directory with the same name (no suffix) as the file name corresponding to the corresponding file, we can use this command to operate. For example, decompressing a file is similar to this. We compress the file file.zip Will be in contact with file.zip Create a directory named file under the same level directory.

6. ${} summary

In fact, ${} is not specifically used to extract the file name or directory name. It is used to extract and replace variables. It can extract a lot of content, not necessarily ‘/’ or ‘. In other words, the above usage is only a special case of its usage.

Seeing the above commands may make people feel very difficult to understand and remember. In fact, they are all regular.

#: means the first one from the left

%: means the first one from the right

##: means the last one from the left

%%: means the last from the right

In other words, always means counting from the left, and% always means counting from the right.

*: indicates the content to be deleted. In the case of # and #, it is on the left side of the specified character (in the example, ‘/’ and ‘.’) to delete the specified character and its left content. In the case of% and%%, it is on the right side of the specified character (in the example, ‘/’ and ‘.’) to delete the specified character and its right content. The position of ‘*’ here cannot be interchanged, that is, the * sign cannot be placed to the right of # or #, and vice versa.

For example, ${var%% X *} means to find the last character x from the right and delete the character X and the character to the right.

As you can see, this command has a wide range of uses. It is just a special case of the naming feature of pointer file name and directory name.

2、 Basename and dirname

The ${} command is not specially designed for extracting file names and directory names, so the basename and dirname commands are specially designed for this purpose.

1、basename

The function of this command is to extract the file name from the path by basename name [suffix].

1) The file name (with suffix) is proposed from the path. The example is as follows:

2) As you can see from the usage of the above command, suffix is an option. Therefore, if you only want to extract the file name file without a suffix, you can also add a suffix after the variable. The example is as follows:

2、dirname

The function of this command is to extract the directory name from the path by dirname

Examples are as follows:

That’s the extraction file.txt The directory where the file is located.

Note: this command can extract not only the directory of ordinary files, but also the directory of any file, such as the directory of the directory, as follows:

It extracts directory dir1 where directory dir2 is located

The above is the whole content of this article, I hope to help you learn, and I hope you can support developer more.