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 of Linux

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Most people do not understand the knowledge of this article "how to use the basename command of Linux", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to use the basename command of Linux" article.

As long as the basename command is used to remove the path and the file suffix part of the file name or directory name, specify a path to the basename, the basename command will delete all prefixes including the last character, and then display the suffix string.

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 thing is to remove the directory in front of the file and print it out:

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

The basename command removes all ending / characters by default:

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

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/locallocal [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/shadowpasswdshadow deletes 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 namehost another method: [root@localhost ~] # basename-s name / etc/hostnamehost

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.confhttpd or [root@localhost ~] # basename / etc/httpd/conf/httpd.conf .confhttpd

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.confsysctlhttpd

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.shrunken bind done Bash for file in * .jpgdo mv "$file"$(basename $file .jpg) .jpeg" done

The above is about the content of this article on "how to use the basename command of Linux". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow 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

Development

Wechat

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

12
Report