dirname

file managementLinux/Unix
The dirname command is one of the most frequently used commands in Linux/Unix-like operating systems. dirname Strip last component from a file path, returning the directory part

Quick Reference

Command Name:

dirname

Category:

file management

Platform:

Linux/Unix

Basic Usage:

dirname [options] [arguments]

Common Use Cases

  • 1

    Path manipulation

    Extract the directory portion from a file path

  • 2

    Shell scripting

    Build robust scripts that work with file paths

  • 3

    Directory navigation

    Change to parent directories of specific files

  • 4

    File operations

    Perform operations on directories containing specific files

Syntax

dirname [OPTION] NAME...

Options

Option Description
-z, --zero Output null-terminated lines instead of newlines
--help Display help information and exit
--version Output version information and exit

Examples

How to Use These Examples

The examples below show common ways to use the dirname command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

# Basic Examples Basic
dirname /usr/bin/bash
Output the directory component: /usr/bin
dirname file.txt
Output the directory component: .
dirname /path/to/some/directory/
Output the directory component: /path/to/some # Advanced Examples Advanced dirname /usr/bin/bash /etc/passwd Output the directory component for multiple paths. dirname a/b/c d/e/f Process multiple paths in one command. MYDIR=$(dirname "$FULLPATH") Use in a shell script to extract directory path. dirname --zero /path/to/file.txt Print null-terminated output instead of newlines.

Try It Yourself

Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

Understanding Syntax

Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

Notes

Dirname Command Overview: The dirname command strips the last component from a file path, leaving only the directory part. It's a simple but essential tool for shell scripting and command-line operations involving file paths. Path Processing Rules: - If the path contains no slashes, dirname outputs "." (the current directory) - If the path is just "/", dirname outputs "/" - Trailing slashes are removed before processing (except for the root "/") - Multiple paths can be processed in a single command Common Uses: - Shell scripting to get the directory part of a file path - Navigating to a parent directory of a specific file - Processing paths in batch operations - Creating directories with the same structure as existing paths Examples in Context: When you have a full file path like "/home/user/documents/file.txt" and need to operate on its directory, you can use: cd $(dirname "/home/user/documents/file.txt") This would change to the /home/user/documents directory. Dirname and Basename: dirname is often used together with the basename command, which does the opposite - it strips directory information and returns just the filename part. Example: PATH="/usr/bin/python" dirname "$PATH" # outputs: /usr/bin basename "$PATH" # outputs: python

Tips & Tricks

1

Use with full file paths to extract just the directory portion

2

Combine with command substitution in scripts: cd $(dirname "$file")

3

Process multiple paths in a single command

4

Use the -z option for null-terminated output when processing filenames with spaces

5

Pair with basename to extract different parts of a path

Common Use Cases

Path manipulation

Extract the directory portion from a file path

Shell scripting

Build robust scripts that work with file paths

Directory navigation

Change to parent directories of specific files

File operations

Perform operations on directories containing specific files

Path standardization

Process and standardize path formats in scripts

Related Commands

These commands are frequently used alongside dirname or serve similar purposes:

Use Cases

1

Path manipulation

Extract the directory portion from a file path

2

Shell scripting

Build robust scripts that work with file paths

3

Directory navigation

Change to parent directories of specific files

4

File operations

Perform operations on directories containing specific files

5

Path standardization

Process and standardize path formats in scripts

Learn By Doing

The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the dirname command works in different scenarios.

$ dirname
View All Commands