Get the App
SLTechnology News&Howtos  ›  Servers  › 

The usage of linux dirname command

Shulou Source: shulou.com Published: 2022-06-03 04:49:51 09月20日 Update

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.

Tags: Commands examples scripts directories parts files locations manuals paths code examples writing more usefulness good practical content parameters names commonly used Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft Shulou Information OPPO Reno Linux NVidia