Ad space (banner)
🖥️Linux Lessons
Lesson 47 / 61

Extracting Just the Folder Part with dirname

dirname is for when "I want to extract just the folder part from a long file path." dirname takes the given path and extracts just the folder part, with the trailing filename removed.

Use it in the form dirname path. It's the exact counterpart to basename, and the two are used together whenever you're splitting a path into its "folder part" and "filename part."

Running dirname /home/user/project/notes.txt extracts and displays just the folder part, /home/user/project, with the filename removed.

A common early mistake is passing a path with a trailing slash and getting an unexpected result. Pay attention to the exact format at the end of your path.

In real work, this is used inside shell scripts to extract just the "location" of a given file path and hand it off to further processing.

Linux
Try it (type this into the pseudo terminal)
dirname /home/user/project/notes.txt

🧑‍💻 You can type this command into the Linux pseudo terminal to try it yourself (this page itself has no interactive terminal).

Ad space (banner)
Ad space (in-article)