Home·Terminal commands

dirname

Files and directories

Usage

dirname [path]

Keeps only the directory part of a path; with no slash in it the answer is a single dot, and like basename it only edits text.

Common flags

FlagMeaning
-zGNU: end each line with NUL instead of a newline
--Everything after this is a path, even if it starts with a dash

Examples

dirname /etc/nginx/nginx.conf

Prints /etc/nginx.

cd "$(dirname "$0")"

Moves a script into its own directory.

Moving, deleting and finding. Some of these do not ask twice, which is why -i becomes a habit.

How to read this

  • Square brackets [ ] mark a part you may leave out.
  • An ellipsis … means you can list more than one.
  • Flags are case-sensitive — in some commands -r and -R do different things.

Questions

Q. What does dirname do?

Keeps only the directory part of a path; with no slash in it the answer is a single dot, and like basename it only edits text.

Q. How do I type it?

dirname [path] — square brackets mark the parts you can leave out.

Q. How many flags are worth knowing?

2 are listed here; the full set is in man dirname. This command sits under Files and directories.

Related commands

man page: man dirname