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 get the file name of the path in Linux

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

Share

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

This article shows you how to get the file name of the path in Linux, the content is concise and easy to understand, it will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

The code is as follows:

[root@dabu.info] # basename / root/aaa/bbb/dabu.txt

Display:

The code is as follows:

Dabu.txt # get the file name of the path

How do I get the path of the script file in the shell script?

Method 1:

The code is as follows:

[root@dabu.info] # DIR=$ (cd "$(dirname" $0 "); pwd)

[root@dabu.info] # echo $DIR

But if you write dirname "$0" like this, you will get the wrong result when you encounter the source command.

Method 2:

The code is as follows:

[root@dabu.info] # echo "$(cd" $(dirname "${BASH_SOURCE [0]}") "& & pwd)"

The above command gets the absolute wheel diameter of the script, no matter where you call the script.

But if it contains soft links, it cannot be used. So, in order to resolve the soft link to the script correctly, we can use the following multi-line command:

The code is as follows:

SOURCE= "${BASH_SOURCE [0]}"

While [- h "$SOURCE"]; do # resolve $SOURCE until the file is no longer a symlink

DIR= "$(cd-P" $(dirname "$SOURCE") "& & pwd)"

SOURCE= "$(readlink" $SOURCE ")"

[[$SOURCE! = / *]] & & SOURCE= "$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located

Done

DIR= "$(cd-P" $(dirname "$SOURCE") "& & pwd)"

Can also be used with the source,bash-c command

However, if you use cd to change to another directory in the script, the above command cannot wait for the correct result when running the above command snippet. You can refer to the article on the $CDPATH trap. To understand how it works, run the following code:

The code is as follows:

#! / bin/bash

SOURCE= "${BASH_SOURCE [0]}"

While [- h "$SOURCE"]; do # resolve $SOURCE until the file is no longer a symlink

TARGET= "$(readlink" $SOURCE ")"

If [[$SOURCE = = / *]]; then

Echo "SOURCE' $SOURCE' is an absolute symlink to'$TARGET'"

SOURCE= "$TARGET"

Else

DIR= "$(dirname" $SOURCE ")"

Echo "SOURCE' $SOURCE' is a relative symlink to'$TARGET' (relative to'$DIR')"

SOURCE= "$DIR/$TARGET" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located

Fi

Done

Echo "SOURCE is'$SOURCE'"

RDIR= "$(dirname" $SOURCE ")"

DIR= "$(cd-P" $(dirname "$SOURCE") "& & pwd)"

If ["$DIR"! = "$RDIR"]; then

Echo "DIR' $RDIR' resolves to'$DIR'"

Fi

Echo "DIR is'$DIR'"

The above is the file name of how to get the path in Linux. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to 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

Servers

Wechat

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

12
Report