Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

dirname

From Wikipedia, the free encyclopedia
Shell command for extracting the directory path portion from a path
dirname
DevelopersVariousopen-source andcommercial developers
Written inC
Operating systemUnix,Unix-like,IBM i
PlatformCross-platform
TypeCommand
Licensecoreutils:GPLv3+

dirname is ashellcommand for extracting thedirectory path portion of apath; without the last name. The command is specified in theSingle UNIX Specification and is primarily used inshell scripts.

The version inGNU Core Utilities was written by David MacKenzie and Jim Meyering.[1] The command is available forWindows as part of theGnuWin32 project[2] andUnxUtils[3] and is inIBM i.[4]

Usage

[edit]

TheSingle UNIX Specification is:dirname path. The required argument,path, is a file path string.

Examples

[edit]

The command reports the directory path portion of a path ignoring any trailing slashes.

$dirname/path/to/filename.ext/path/to$dirname/path/to//path$dirnamefilename.ext.

Performance

[edit]

Since the command accepts only one operand, its usage within theinner loop of a shell script can be detrimental to performance. Consider:

whilereadfile;dodirname"$file"done<some-input

The above causes a separate process invocation for each line of input. For this reason, shell substitution is typically used instead:

echo"${file%/*}";

Or, if relative pathnames need to be handled as well:

if[-n"${file##*/*}"];thenecho"."elseecho"${file%/*}";fi

Note that these handle trailing slashes differently thandirname.

See also

[edit]

References

[edit]
  1. ^dirname(1) – Linux UserManual – User Commands from Manned.org
  2. ^CoreUtils for Windows
  3. ^Native Win32 ports of some GNU utilities
  4. ^IBM."IBM System i Version 7.2 Programming Qshell"(PDF).IBM.Archived(PDF) from the original on 2020-09-18. Retrieved2020-09-05.

External links

[edit]
The WikibookGuide to Unix has a page on the topic of:Commands
File system
Processes
User environment
Text processing
Shell builtins
Searching
Documentation
Software development
Miscellaneous
File system
Text utilities
Shell utilities
Retrieved from "https://en.wikipedia.org/w/index.php?title=Dirname&oldid=1318289135"
Categories:
Hidden categories:

[8]ページ先頭

©2009-2025 Movatter.jp