Back to the command reference

dirname

FilesFundamentals In simulator In real Linux

Strip the last component from a path

Name: dirname: the directory name

You can practise it right away in the simulator, and use it in real Linux mode too.

Description

dirname removes the last component of a path and prints the directory part, or . if there is no slash. It also works on the string only.

"$(dirname "$0")" is a common way for a script to find its own directory.

Syntax

dirname NAME...

Examples

  • $ dirname /home/user/projects/hello.py

    Prints /home/user/projects

    Explain

Related