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

How to use the Basename command in Linux

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

Editor to share with you how to use the Basename command in Linux. I hope you will get something after reading this article. Let's discuss it together.

Basename is a useful gadget on the command line that removes directories and suffixes from a given file name.

System environment

Centos7

How to use the basename command

On Centos7 systems, the basename command is installed by default, which is included in the coreutils installation package.

Basename has two grammars:

Basename NAME [SUFFIX] basename OPTION... NAME...

The last part of basename. You can also delete any ending suffix. This is a simple command, the most basic is to remove the directory in front of the file name and print it out:

[root@localhost ~] # basename / etc/yum.repos.d/CentOS-Base.repo CentOS-Base.repo

The basename command removes all ending / characters by default:

[root@localhost ~] # basename / usr/local/ local [root@localhost ~] # basename / usr/local local

By default, each output line ends with a newline character (\ n). To end with NUL, use the-z (--zero) option.

[root@localhost ~] # basename-z / usr/local local [root@localhost ~] #

Basename accepts multiple files

The basename command can accept multiple names as parameters. You can use the-a (--multiple) option, and then use spaces to separate the list of files. For example, to get the file names of / etc/passwd and / etc/shadow, run:

[root@localhost] # basename-a / etc/passwd / etc/shadow passwd shadow

Delete the suffix at the end specified

To remove any ending suffix from the file name, pass the suffix as the second parameter:

[root@localhost ~] # basename / etc/hostname name host another method: [root@localhost ~] # basename-s name / etc/hostname host

In the above example, by specifying name as the suffix, you can see that the output only shows / after and before name.

Typically, this feature is used to delete the file extension:

[root@localhost ~] # basename-s .conf / etc/httpd/conf/httpd.conf httpd or [root@localhost ~] # basename / etc/httpd/conf/httpd.conf .conf httpd

The following example uses the-an option to specify multiple files and the-s option to specify the suffix content:

[root@localhost] # basename-a-s. Conf / etc/sysctl.conf / etc/httpd/conf/httpd.conf sysctl httpd

Another way to delete the trailing suffix is to specify the suffix using the-s (--suffix = SUFFIX) option. In the above example to show.

Use an example

The following example shows how to use the for loop, mv command, and basename command in a bash script to replace the file extension from ".jpg" to ".jpeg" by changing the picture file under the current directory:

[root@localhost test] # vim convert.sh #! / bin/bash for file in * .jpg do mv "$file"$(basename $file .jpg) .jpeg" done

Picture

After reading this article, I believe you have a certain understanding of "how to use Basename commands in Linux". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!

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