Real path to get file in shell (absolute path)

Source: Internet
Author: User

In a shell script, a relative path is usually used to do the processing, but sometimes we may need to use an absolute path.
How do I get an absolute path to a file or directory in the shell?
Perhaps you will immediately think of Realpath (if you have written PHP), so the shell supports this command. Take a look here and you'll know.
Realpath appears to come from Debian, and are not part of the Coreutils,realpath appears not to be standard issue.
This command is not always available. We can actually use the Readlink command: readlink-f file_path
file_real_path=$ (readlink-f.) Get the current real path
file_real_path=$ (readlink-f.    /..) Get the real path to the first level
So you can get the absolute path of the file, readlink the specific use of the man Readlink.

Readlink for Softlink's processing to get the actual file path, not Softlink path.

A better way to get the path of the currently executing script is:

curdir=$ (CD $ (dirname ${bash_source[0]}); pwd)

Echo $CURDIR

Other common paths are:

Gets the path of the current execution script
filepath=$ (CD "$ (dirname" $) "; pwd)

echo "$ (basename $) $ (dirname $)--$filepath"
The absolute path to the script file exists in the environment variable filepath, which can be used with echo $filepath


$: Gets the name of the current script
$#: Number of parameters passed to script
$$: The process number of the shell script



Related Article

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.