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 usage of linux dirname command

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

Share

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

This article shares with you the usage of the linux dirname command. The editor thought it was very practical, so I shared it with you to learn. The following information is an example of the use of the linux dirname command.

NAME

Dirname-strip last component from file name

Example 1 an example from the manual page

$dirname / usr/bin/sort

/ usr/bin

$dirname stdio.h

.

Example two

$dirname / usr/bin

/ usr

$dirname / usr/bin/

/ usr

Get the absolute path where the script file is located in the script

ShellPath=$ (cd "$(dirname" $0 "); pwd)

Echo $shellPaht

Usage description

The dirname command can fetch the directory portion (strip non-directory suffix from file name) of a given path. This command is rarely used directly on the shell command line, and I usually use it in shell scripts to get the directory where the script file is located and then switch to the current directory. According to the manual page, "Print NAME with its trailing / component removed; if NAME contains no / 's, output'. (meaning the current directory)." it seems that "fetching the directory portion of a given path" does not accurately summarize the purpose of the dirname command. Another command under Linux is basename, which, in contrast to dirname, gets the file name part.

Common parameters

None.

Use the example

Example 1 an example from the manual page

[root@qzt196] # dirname / usr/bin/sort / usr/bin [root@qzt196 ~] # dirname stdio.h. [root@qzt196 ~] #

Example two

[root@qzt196 ~] # dirname / usr/bin/ usr [root@qzt196 ~] # dirname / usr/bin/ / usr

Note: the output here seems a little strange. This should be explained by looking at the source code of the dirname command.

Application of example 3 in bash script

Bash Code Collection Code

#! / bin/sh

Jump to the directory where the script is located

Cd $(dirname "$0") | | exit 1

The rest of it.

I often use this because sometimes I can't determine which directory the script is in when it is executed, such as when the script is used in crontab.

Another way to write it is: cd dirname $0, where backquotation marks are equivalent to $().

Comparison of example 4 with another method of intercepting directories

You can use ${pathname%/*} to intercept the portion of the file behind the pathname.

[root@jfht ~] # pathname=/usr/bin/sort; echo $(dirname $pathname) ${pathname%/} / usr/bin/ usr/bin [root@jfht ~] # pathname=/usr/bin/; echo $(dirname $pathname) ${pathname%/} / usr/ usr/bin [root@jfht ~] # pathname=/usr/bin; echo $(dirname $pathname) ${pathname%/} / usr/ usr [root@jfht ~] # pathname=/usr/ Echo $(dirname $pathname) ${pathname%/} / / usr [root@jfht ~] # pathname=/usr; echo $(dirname $pathname) ${pathname%/} / [root@jfht ~] # pathname=/; echo $(dirname $pathname) ${pathname%/} / [root@jfht ~] # pathname=stdio.h; echo $(dirname $pathname) ${pathname%/*}. Stdio.h [root@jfht ~] #

So much for sharing the usage of the dirname command. I hope the above content can be of some help and learn more knowledge. If you think the article is good, you can share it for more people to see.

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