dirname
Linux · Files · reference only
What it does
Returns only the directory part of a path, removing the last component.
Syntax
dirname [opciones] ruta...
Options and parameters
rutaOne or more paths; each is processed separately.
-zEnds each output line with a null character instead of a newline.
--helpShows help and exits.
--versionShows the version and exits.
Usage examples
dirname /etc/passwdReturns /etc.
dirname /home/ana/foto.jpgReturns /home/ana.
dirname archivo.txtReturns a dot (.), the current directory, because the path has no directory.
cd "$(dirname "$0")"In a script, changes to the directory where the script itself lives.
Notes and common mistakes
- Like basename, it only works with text and does not check that the path exists.