Skip to content

The dirname command

The dirname command prints the folder part of a path…

The dirname command prints the folder part of a path: everything before the last slash.

A practice shell in your browser that behaves like bash on Linux for these commands. Nothing you type leaves the page.

Usage

dirname PATH...

Strip the last part from a path. Prints the folder part of a path: everything before the last slash, or . for a name with no slash in it.

How to use dirname

Get the folder from a path

A name with no slash gives ., the current folder.

dirname /home/learner/projects/app/main.js
/home/learner/projects/app
dirname notes.txt
.

More examples

dirname /home/learner/notes.txt
/home/learner
/home/learner

Questions about dirname

What is dirname used for?

Mostly in scripts, to find the folder a file is in, as in cd "$(dirname "$file")".

Every everyday command on one page, to print or keep open: the Linux commands cheat sheet.