Some ways the shell extracts file names and directory names

Source: Internet
Author: User

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:


[[Email protected]:] Var=/home/user/1.txt

[[Email protected]:] Echo ${var##*/}

1.txt

The operation extracts the file name under path 1.txt


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:


[[Email protected]:] Var=/home/user/1.txt

[[Email protected]:] Echo ${var##*.}

Txt

The operation extracts the suffix name


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:


[[Email protected]:] var=/home/user/game.tar.gz

[[Email protected]:] Echo ${var#*.}

tar.gz

This action 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:


[[Email protected]:] Var=/home/user/1.txt

[[Email protected]:] Echo ${var%/*}

/home/user

This action extracts the directory where the file is 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


[[Email protected]:] Var=/home/user/1.txt

[[Email protected]:] Echo ${var%%/.*}

/home/user/1


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.



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:


[[Email protected]:] Var=/home/user/1.txt

[[Email protected]:] echo ${basename $var}

1.txt


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:


[[Email protected]:] Var=/home/user/1.txt

[[Email protected]:] echo ${basename $var. txt}

1


2, DirName

The purpose of this command is to extract the directory name from the path, using the DirName name example as follows:


[[Email protected]:] Var=/home/user/1.txt

[Email protected]: dirname $var

/home/user


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:


[[Email protected]:] Var=/home/user

[Email protected]: dirname $var

/home






This article is from the "[email protected]" blog, please be sure to keep this source http://superleedo.blog.51cto.com/12164670/1976049

Some ways the shell extracts file names and directory names

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.