Hello lad
Hello lad

Reputation: 18790

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

Upvotes: 4

Views: 7182

Answers (2)

edi9999
edi9999

Reputation: 20554

There is a Unix utility that can achieve that:

readlink --canonicalize nameofyourfileorfolder

Upvotes: 6

peak
peak

Reputation: 116710

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.

Upvotes: 0

Related Questions