Julia: How to Navigate Directories

Hongtao Hao / 2021-07-11

This post is tested under masOS High Sierra and Julia v1.6.1.

Suppose you have folder named A with following directory tree:

├── B
│   ├── D
│   │   ├── F
│   │   └── YourScript.jl
│   └── E
└── C

Current directory #

pwd() # returns "/Users/USERNAME/Desktop/A/B/D"

Or,

@__DIR__ # returns /Users/USERNAME/Desktop/A/B/D"

To go up by one level #

You need to use dirname()

dirname(pwd()) # returns "/Users/USERNAME/Desktop/A/B"

Or,

cd(pwd, "..") # returns "/Users/USERNAME/Desktop/A/B"

To go up by one level and access another folder #

In this case, to access E.

You can use the function of joinpath()

joinpath(dirname(pwd()), "E") # returns "/Users/USERNAME/Desktop/A/B/E"

Or, use string() :

string(dirname(pwd()), "/E") # returns "/Users/USERNAME/Desktop/A/B/E"

To go up by more than one level #

cd(pwd, "../..") # returns "/Users/USERNAME/Desktop/A"

Home directory #

Use homedir()

homedir() ## returns "/Users/USERNAME"

References #

Last modified on 2021-10-05