SoFunction
Updated on 2025-03-10

shell: Implementation of method of extracting file names and directory names

In writing shell scripts, there is often a need to process the path and file name. Sometimes there is no need to use the sed command to operate. The variable operations provided by bash and some external commands can be handled well.

1. Use the variable operator ${}

1、${var##*/}

This method is to maximize the matching character "/" from the left, then cut off the left content (including the character "/"), and return to the remaining right part.

$ var=/dir1/dir2/
$ echo ${var##*/}

In shell scripts, variables can be used to save this result and then utilize it, such as file=${var##*/}

We replace "/" with "."

$ var=/dir1/dir2/
$ echo ${var##*.}
txt

This way you can extract the file suffix.
What if the file has more than one suffix, for example, the command ${var##*.} can only extract the last suffix, and what should I do when I want to extract it? Then you need to use the ${var#*.} command mentioned below.

2、${var#*.} 

This method is to match the character "." from the left for the first time, then cut off the left content (including the character ".") and return to the remaining right part.

$ var=/dir1/dir2/
$ echo ${var#*.}

This allows multiple suffixes to be extracted from the file. However, be aware that the previous pathname cannot contain the character ".". If it is included, readers should also know what to do.

3、${var%/*}

This method is to match the character "/" for the first time from the right, and then cut off the content on the right (including the character "/") and return to the remaining left part.

$ var=/dir1/dir2/
$ echo ${var%%/*}
/dir1/dir2

This will extract the path where the file we need is located

4、${var%%.*}

This method is to maximize the matching character "." from the right, then cut off the right content (including the characters ".") and return to the remaining left part. I won't give an example here.

Method 1 Summary

bash provides many variable operation methods, but these four are basically enough for string interception.

There is a convenient way to remember. Let’s look at the keyboard. “#” is on the left of “%”. When using #, it means that the content on the left is cut off. On the contrary, % is cut off from the right. And ## and %% are the maximum cutoff.

*: It indicates the content to be deleted, and a character is also needed to indicate the point of quitting.

For example: ${var%%x*} means finding the character "x" that is maximized from the right, and deleting the character x and its characters on the right.

2. Basename and dirname

The basename and dirname commands are specifically used to intercept file names and path names.

1、basename

The function of this command extracts the file name, and uses it as basename NAME [SUFFIX].

1) Propose the file name (with suffix) from the path, the example is as follows:

$ var=/dir1/dir2/
$ echo $(basename ${var})

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

$ var=/dir1/dir2/
$ echo $(basename ${var} .txt)
file

2、dirname

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

$ var=/dir1/dir2/
$ echo $(dirname ${var})
/dir1/dir2

This extracts the path where the file is located.
Note: This command can not only extract the directory where ordinary files are located, but also extract the directory where any files are located, such as the directory where the directory is located, as follows:

$ var=/dir1/dir2/
$ echo $(dirname ${var})
/dir1

It extracts the directory dir1 where directory dir2 is located, so you should note this during use.

This is the article about the method of extracting file names and directory names in shell. For more relevant shell extracting file names and directory names, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!