# dirname

*noun*

say it: **“DUR-naym”** **/ˈdɝneɪm/**

A command that cuts the last part off a path and prints what's left, the folder the file is in.

## What does **dirname** do?

`dirname` cuts the last part, the file's name, off a path and prints what's left: the folder it's in. Scripts use it with `${BASH_SOURCE[0]}` to find their own folder. `basename` does the opposite and keeps only the name.

```bash
dirname /home/sam/terminal-practice/shuffler/stats.sh   # /home/sam/terminal-practice/shuffler
dirname shuffler/mix-lib.sh                             # shuffler
dirname stats.sh                                        # .
```

## In a sentence

- “The script runs **dirname** on its own path to learn which folder it lives in.”
- “**dirname** turned /home/sam/terminal-practice/shuffler/stats.sh into the shuffler folder.”

Level: Apprentice · [More Unix terms at this level](https://www.ask-codey.com/glossary?category=unix&level=2)

Updated: November 10, 2026

Source: https://www.ask-codey.com/glossary/dirname
