Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of extracting File name and extension in Shell script

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

How to extract file names and extensions in Shell scripts? This problem may be our daily study or work often see. I hope you learned something from this question. The following is the reference content brought to you by Xiaobian. Let's take a look together!

Sometimes you may need to extract file names and extensions from different variables to accomplish tasks in bash shell programming.

The following is the detailed content

1. Get the file name without path:

First delete the full file path from the input file name. For example, if the filename is entered as/etc/apache2/apache2.conf, only the full filename apache.conf.

#!/ bin/bashfullfilename="/etc/apache2/apache2.conf"filename=$(basename "$fullfilename")echo $filename

2. File names without extensions:

Now extract file names without extensions from the extracted full file names without paths, as shown below.

#!/ bin/bashfullfilename="/etc/apache2/apache2.conf"filename=$(basename "$fullfilename")fname="${filename%.*} "echo $fname

File extensions without names:

Now extract the file extension without name from the extracted full file name without path.

#!/ bin/bashfullfilename="/etc/apache2/apache2.conf"filename=$(basename "$fullfilename")ext="${filename##*.} "echo $ext

4. Testing:

Finally test everything in a shell script. Create a new script file using the following. The file name is passed as a command-line argument during script execution.

#!/ bin/bashfullfilename=$1filename=$(basename "$fullfilename")fname="${filename%.*} "ext="${filename##*.} "echo "Input File: $fullfilename"echo "Filename without Path: $filename"echo "Filename without Extension: $fname"echo "File Extension without Name: $ext"

We execute scripts with file names as command-line arguments.

$ ./ script.sh File: /etc/apache2/apache2.confFilename without Path: apache2.confFilename without Extension: apache2File Extension without Name: conf Thank you for reading! After reading this, do you have a general idea of how to extract filenames and extensions from Shell scripts? I hope the content of this article is helpful to everyone. If you want to know more about related articles, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report