Some ways to extract file names and directory names in the Linux shell

Source: Internet
Author: User

many times when using the Linux shell, we need to handle 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 filename 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.

first, use ${} 1, ${var##*/}
The purpose of this command is to remove the variable var from the left side of the last '/' character and its left side of the content, return from the left side of the last '/' (excluding the character) to the right of the content. Examples of use and results are as follows:

as you can see from the running results, using this command, you can extract the file name file.txt we need.
If used in Shell program files, you can use variables to save this result, and then use, such as file=${var##*/}

2, ${var##*.}
The purpose of this command is to remove the variable var from the left side of the last '. ' Character and its left side, returns the last '. ' From the left. (without the character) to the right of the content. Examples of use and results are as follows:

As you can see from the running results, using this command, you can extract the file suffixes we need.

If the file suffix has not only one, for example, file.tar.gz, command ${var##*.} Only the last suffix can be extracted, and what should I do when I want to extract tar.gz? Then use the ${var#* described below .} The order.
3, ${var#*.}
The purpose of this command is to remove the variable var from the left side of the first '. ' Character and its left side, return the first '. ' From the left. The contents of the right part (excluding the character). Examples of use and results are as follows:

As you can see from the running results, using this command, you can extract multiple suffixes of a file.

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

As you can see from the running results, using this command, you can extract the directory where the files we need are located

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

This command can be used when we need to create a directory with the same name as the filename (without a suffix) relative to the corresponding file. For example, it is similar to extracting a file, and when we compress the file File.zip, we create a directory named file in the File.zip sibling directory.

6. ${} Summary
In fact, ${} is not specifically to extract the file name or directory name, its use is variable extraction and substitution, and so on, it can extract a lot of content, not necessarily the above five examples of '/' or '. '. In other words, the above method is only a special case of its use.

Seeing the above commands may make people feel very difficult to understand and remember, but they are all regular.
#: Indicates that the first one is counted from the left
%: Indicates that the first one is counted from the right
# #: Indicates the last one to be counted from the left
percent: Indicates that the last one is counted from the right
in other words, # always indicates that the left is counted, and% always represents the right.

*: Indicates the content to be deleted, for # and # #的情况, it is located in the specified character (example of '/' and '. ') ) to the left, the table deletes the specified character and its left content, and for the case of% and percent, it is located in the specified character (the '/' and '. ' in the example). ) to the right, which indicates the deletion of the specified character and its right content. The position of the ' * ' is not interchangeable, that is, you cannot put the * number in # or # #的右边, or vice versa.

For example: ${var%%x*} means finding the last character X from the right and removing the character x and its right character.

See here, you can know, in fact, the purpose of the command is very extensive, above just the pointer file name and directory name of the naming characteristics to extract some special cases.

ii. basename and DirName ${} is not a command specifically designed to extract filenames and directory names, so the basename and DirName commands are specifically prepared for this.

1, basename
The purpose of this command is to extract the file name from the path, using the basename name [SUFFIX].

1) file name (with suffix) from the path, examples are as follows:


2) from the usage of the above command, you can see that the suffix (SUFFIX) is an optional option. So, if you just want to extract the file name, without a suffix, you can also add a suffix after the variable, such as the following example:


2. dirname
The purpose of this command is to extract the directory name from the path, using the dirname name
Examples of use are as follows:

This extracts the directory where the File.txt file is located.

Note: This command not only extracts the directory of the normal file, it can extract the directory where any files are located, for example, the directory is located in the directory, as follows:

It extracts the directory where the directory Dir2 resides Dir1.

Some ways to extract file names and directory names in the Linux shell

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.