1DIRNAME(1P) POSIX Programmer's Manual DIRNAME(1P)
2
3
4
6 This manual page is part of the POSIX Programmer's Manual. The Linux
7 implementation of this interface may differ (consult the corresponding
8 Linux manual page for details of Linux behavior), or the interface may
9 not be implemented on Linux.
10
11
13 dirname — return the directory portion of a pathname
14
16 dirname string
17
19 The string operand shall be treated as a pathname, as defined in the
20 Base Definitions volume of POSIX.1‐2008, Section 3.267, Pathname. The
21 string string shall be converted to the name of the directory contain‐
22 ing the filename corresponding to the last pathname component in
23 string, performing actions equivalent to the following steps in order:
24
25 1. If string is //, skip steps 2 to 5.
26
27 2. If string consists entirely of <slash> characters, string shall be
28 set to a single <slash> character. In this case, skip steps 3 to 8.
29
30 3. If there are any trailing <slash> characters in string, they shall
31 be removed.
32
33 4. If there are no <slash> characters remaining in string, string
34 shall be set to a single <period> character. In this case, skip
35 steps 5 to 8.
36
37 5. If there are any trailing non-<slash> characters in string, they
38 shall be removed.
39
40 6. If the remaining string is //, it is implementation-defined whether
41 steps 7 and 8 are skipped or processed.
42
43 7. If there are any trailing <slash> characters in string, they shall
44 be removed.
45
46 8. If the remaining string is empty, string shall be set to a single
47 <slash> character.
48
49 The resulting string shall be written to standard output.
50
52 None.
53
55 The following operand shall be supported:
56
57 string A string.
58
60 Not used.
61
63 None.
64
66 The following environment variables shall affect the execution of
67 dirname:
68
69 LANG Provide a default value for the internationalization vari‐
70 ables that are unset or null. (See the Base Definitions vol‐
71 ume of POSIX.1‐2008, Section 8.2, Internationalization Vari‐
72 ables for the precedence of internationalization variables
73 used to determine the values of locale categories.)
74
75 LC_ALL If set to a non-empty string value, override the values of
76 all the other internationalization variables.
77
78 LC_CTYPE Determine the locale for the interpretation of sequences of
79 bytes of text data as characters (for example, single-byte as
80 opposed to multi-byte characters in arguments).
81
82 LC_MESSAGES
83 Determine the locale that should be used to affect the format
84 and contents of diagnostic messages written to standard
85 error.
86
87 NLSPATH Determine the location of message catalogs for the processing
88 of LC_MESSAGES.
89
91 Default.
92
94 The dirname utility shall write a line to the standard output in the
95 following format:
96
97 "%s\n", <resulting string>
98
100 The standard error shall be used only for diagnostic messages.
101
103 None.
104
106 None.
107
109 The following exit values shall be returned:
110
111 0 Successful completion.
112
113 >0 An error occurred.
114
116 Default.
117
118 The following sections are informative.
119
121 The definition of pathname specifies implementation-defined behavior
122 for pathnames starting with two <slash> characters. Therefore, applica‐
123 tions shall not arbitrarily add <slash> characters to the beginning of
124 a pathname unless they can ensure that there are more or less than two
125 or are prepared to deal with the implementation-defined consequences.
126
128 ┌─────────────────┬─────────────┐
129 │ Command │ Results │
130 ├─────────────────┼─────────────┤
131 │dirname / │ / │
132 │dirname // │ / or // │
133 │dirname /a/b/ │ /a │
134 │dirname //a//b// │ //a │
135 │dirname │ Unspecified │
136 │dirname a │ . ($? = 0) │
137 │dirname "" │ . ($? = 0) │
138 │dirname /a │ / │
139 │dirname /a/b │ /a │
140 │dirname a/b │ a │
141 └─────────────────┴─────────────┘
142 See also the examples for the basename utility.
143
145 The dirname utility originated in System III. It has evolved through
146 the System V releases to a version that matches the requirements speci‐
147 fied in this description in System V Release 3. 4.3 BSD and earlier
148 versions did not include dirname.
149
150 The behaviors of basename and dirname in this volume of POSIX.1‐2008
151 have been coordinated so that when string is a valid pathname:
152
153 $(basename -- "string")
154
155 would be a valid filename for the file in the directory:
156
157 $(dirname -- "string")
158
159 This would not work for the versions of these utilities in early pro‐
160 posals due to the way processing of trailing <slash> characters was
161 specified. Consideration was given to leaving processing unspecified if
162 there were trailing <slash> characters, but this cannot be done; the
163 Base Definitions volume of POSIX.1‐2008, Section 3.267, Pathname allows
164 trailing <slash> characters. The basename and dirname utilities have to
165 specify consistent handling for all valid pathnames.
166
168 None.
169
171 Section 2.5, Parameters and Variables, basename
172
173 The Base Definitions volume of POSIX.1‐2008, Section 3.267, Pathname,
174 Chapter 8, Environment Variables
175
177 Portions of this text are reprinted and reproduced in electronic form
178 from IEEE Std 1003.1, 2013 Edition, Standard for Information Technology
179 -- Portable Operating System Interface (POSIX), The Open Group Base
180 Specifications Issue 7, Copyright (C) 2013 by the Institute of Electri‐
181 cal and Electronics Engineers, Inc and The Open Group. (This is
182 POSIX.1-2008 with the 2013 Technical Corrigendum 1 applied.) In the
183 event of any discrepancy between this version and the original IEEE and
184 The Open Group Standard, the original IEEE and The Open Group Standard
185 is the referee document. The original Standard can be obtained online
186 at http://www.unix.org/online.html .
187
188 Any typographical or formatting errors that appear in this page are
189 most likely to have been introduced during the conversion of the source
190 files to man page format. To report such errors, see https://www.ker‐
191 nel.org/doc/man-pages/reporting_bugs.html .
192
193
194
195IEEE/The Open Group 2013 DIRNAME(1P)