Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Linux dirname command

Linux dirname command

Linux Command Reference Linux Command Reference


What is the dirname command

dirname It is a simple but practical command-line tool in Linux/Unix systems, used to extract the directory portion from a file path. It helps you quickly obtain the parent directory in a path without manually parsing strings.

Basic Functionality


Command syntax



dirname [选项] 文件名...

Parameter Description

Optional Parameters

Although in most cases dirname no options are needed, the GNU version supports the following standard options:

Option Description
-z Use the NUL character (\0) to separate output instead of newline characters
--help Display help information
--version Display version information

How It Works

dirname The command processes a path through the following steps:

  1. Remove all trailing slashes (/) from the path
  2. Remove the last slash and all characters after it
  3. If the result is empty, output "." (the current directory)

Processing Flowchart


Usage examples

Basic Usage

Example

$

dirname

/

home

/

user

/

docs

/

file.txt

/

home

/

user

/

docs

$

dirname

relative

/

path

/

to

/

file

relative

/

path

/

to

Processing Multiple Files

Example

$

dirname

/

a

/

b

/

c.txt

/

x

/

y

/

z.txt

/

a

/

b

/

x

/

y

Special Path Handling

Example

$

dirname

/

usr

/

bin

/

     

Note Trailing Slashes

/

usr

$

dirname

file.txt      

Only a Filename

.

$

dirname

/

             

Root Directory

/

Use in Scripts

Example

#!/bin/bash

Get the Directory Where the Script Resides

SCRIPT_DIR

=$

(

dirname

"$0"

)

echo

"Script directory: $SCRIPT_DIR"

Get the Directory of a Configuration File Path

CONFIG_PATH

=

"/etc/app/config.cfg"

CONFIG_DIR

=$

(

dirname

"$CONFIG_PATH"

)

echo

"Config directory: $CONFIG_DIR"


Common Application Scenarios

1. Get the Directory Where the Script Resides

Example

#!/bin/bash

Get the absolute path where the script resides

SCRIPT_DIR

=$

(

dirname

"$(readlink -f "$0")"

)

echo

"Script directory: $SCRIPT_DIR"

2. Build a Relative Path

Example

Suppose we know the file is in a subdirectory of a certain directory

BASE_DIR

=

"/var/log"

FULL_PATH

=

"$BASE_DIR/app/error.log"

Get the log directory

LOG_DIR

=$

(

dirname

"$FULL_PATH"

)

3. Use with basename

Example

Split a full path

FULL_PATH

=

"/home/user/docs/report.pdf"

DIR

=$

(

dirname

"$FULL_PATH"

)

FILE

=$

(

basename

"$FULL_PATH"

)

echo

"Directory: $DIR"

echo

"Filename: $FILE"


Notes

Symbolic links:dirname It does not resolve symbolic links; if needed, it can be combined with readlink Use

Example

dirname

"$(readlink -f "/path/with/symlink")"

Handling Spaces: when a path contains spaces, make sure to use quotes

Example

dirname

"/path/with spaces/file.txt"

Relative path: the output result retains the relative path form

Example

$

dirname

..

/

parent

/

file.txt

..

/

parent

Empty Input: if no arguments are provided, the GNU version outputs ".", but this is not standard POSIX behavior


Comparison with Similar Commands

Command Function Example input Example output
dirname Extract the directory part /a/b/c.txt /a/b
basename Extract the filename portion /a/b/c.txt c.txt
realpath Get the absolute path (resolve symbolic links) ../file /full/path/to/file

Practical Exercise

Create a test file and get its directory path

Example

touch

/

tmp

/

testfile

dirname

/

tmp

/

testfile

Write a script that displays the directory where it resides

Example

#!/bin/bash

echo

"This script is located at: $(dirname "$0")"

Try handling paths that contain spaces and special characters

Example

mkdir

-p

"/tmp/my dir"

touch

"/tmp/my dir/special file.txt"

dirname

"/tmp/my dir/special file.txt"


Linux Command Reference Linux Command Reference