Method of obtaining filename of path in Linux system

Source: Internet
Author: User
Tags relative symlink

This article mainly introduces the Linux system to obtain the path of the file name method, the article summed up two, the need for friends can refer to the

The code is as follows:

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

Show:

The code is as follows:

Dabu.txt #获取路径的文件名

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

Method One:

The code is as follows:

[Root@dabu.info] #DIR =$ (CD "$ (dirname" $) "; pwd)

[Root@dabu.info] #echo $DIR

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

Method Two:

The code is as follows:

[Root@dabu.info] #echo "$ (CD" $ (dirname "${bash_source[0]}") "&& pwd)"

The above line command can get the absolute wheel path of the script, no matter where you call the script.

But if you have soft links, you won't be able to use them. So, in order to properly parse the soft link to the script, you can use the following multiline 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 is a relative symlink, we need to resolve it relative t o 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 a CD to switch to a different directory in your script, the commands above will not wait for the correct result when you run the command fragment above. You can refer to the article about the $cdpath trap. To understand how it works, you can 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 ' 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 is a relative symlink, we need to resolve it relative to the path where the symlink fi Le 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 '"

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.