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

Analysis of Command usage examples in Linux

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

Share

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

This article gives you an analysis of "Command usage example Analysis in Linux". The content is detailed and easy to understand. Friends who are interested in "case analysis of the use of commands in Linux" can follow the editor's train of thought to read it in depth. I hope it will be helpful to you after reading. Let's follow the editor to learn more about "command usage case analysis in Linux".

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 thing is to remove the directory in front of the file 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/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

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 namehost 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.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.conf sysctlhttpd

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/bashfor file in * .jpgdo mv "$file"$(basename $file .jpg) .jpeg" done

This is the end of the case analysis on the use of commands in Linux. I hope the above content can improve everyone. If you want to learn more knowledge, please pay more attention to the editor's updates. Thank you for following the website!

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