简体   繁体   中英

How to get absolute path of a directory?

I want to automate the following thing:

cd into current directory

cd workdir

make a new directory

mkdir mydata

and get the absolute path to this mydata directory

有一个Unix实用程序可以实现:

readlink --canonicalize nameofyourfileorfolder

Assuming cd workdir; mkdir mydata does not fail:

dir=$(cd workdir; mkdir mydata; pwd)

Slightly more robust:

dir=$(mkdir -p workdir/mydata; pwd)

For a still more robust approach, you could run the following command after checking the created directory exists:

dir=$(cd workdir/mydata; pwd)

If by "absolute path" you mean the physical path, then consider using pwd -P instead of just pwd .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM