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
12 dirname - return the directory portion of a pathname
13
15 dirname string
16
18 The string operand shall be treated as a pathname, as defined in the
19 Base Definitions volume of IEEE Std 1003.1-2001, Section 3.266, Path‐
20 name. The string string shall be converted to the name of the directory
21 containing the filename corresponding to the last pathname component in
22 string, performing actions equivalent to the following steps in order:
23
24 1. If string is //, skip steps 2 to 5.
25
26 2. If string consists entirely of slash characters, string shall be
27 set to a single slash character. In this case, skip steps 3 to 8.
28
29 3. If there are any trailing slash characters in string, they shall be
30 removed.
31
32 4. If there are no slash characters remaining in string, string shall
33 be set to a single period character. In this case, skip steps 5 to
34 8.
35
36 5. If there are any trailing non-slash characters in string, they
37 shall be removed.
38
39 6. If the remaining string is //, it is implementation-defined whether
40 steps 7 and 8 are skipped or processed.
41
42 7. If there are any trailing slash characters in string, they shall be
43 removed.
44
45 8. If the remaining string is empty, string shall be set to a single
46 slash character.
47
48 The resulting string shall be written to standard output.
49
51 None.
52
54 The following operand shall be supported:
55
56 string A string.
57
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 variables
70 that are unset or null. (See the Base Definitions volume of
71 IEEE Std 1003.1-2001, Section 8.2, Internationalization Vari‐
72 ables for the precedence of internationalization variables used
73 to determine the values of locale categories.)
74
75 LC_ALL If set to a non-empty string value, override the values of all
76 the other internationalization variables.
77
78 LC_CTYPE
79 Determine the locale for the interpretation of sequences of
80 bytes of text data as characters (for example, single-byte as
81 opposed to multi-byte characters in arguments).
82
83 LC_MESSAGES
84 Determine the locale that should be used to affect the format
85 and contents of diagnostic messages written to standard error.
86
87 NLSPATH
88 Determine the location of message catalogs for the processing of
89 LC_MESSAGES .
90
91
93 Default.
94
96 The dirname utility shall write a line to the standard output in the
97 following format:
98
99
100 "%s\n", <resulting string>
101
103 The standard error shall be used only for diagnostic messages.
104
106 None.
107
109 None.
110
112 The following exit values shall be returned:
113
114 0 Successful completion.
115
116 >0 An error occurred.
117
118
120 Default.
121
122 The following sections are informative.
123
125 The definition of pathname specifies implementation-defined behavior
126 for pathnames starting with two slash characters. Therefore, applica‐
127 tions shall not arbitrarily add slashes to the beginning of a pathname
128 unless they can ensure that there are more or less than two or are pre‐
129 pared to deal with the implementation-defined consequences.
130
132 Command Results
133 dirname / /
134
135 dirname // / or //
136 dirname /a/b/ /a
137 dirname //a//b// //a
138 dirname Unspecified
139 dirname a . ($? = 0)
140 dirname "" . ($? = 0)
141 dirname /a /
142 dirname /a/b /a
143 dirname a/b a
144
146 The dirname utility originated in System III. It has evolved through
147 the System V releases to a version that matches the requirements speci‐
148 fied in this description in System V Release 3. 4.3 BSD and earlier
149 versions did not include dirname.
150
151 The behaviors of basename and dirname in this volume of
152 IEEE Std 1003.1-2001 have been coordinated so that when string is a
153 valid pathname:
154
155
156 $(basename "string")
157
158 would be a valid filename for the file in the directory:
159
160
161 $(dirname "string")
162
163 This would not work for the versions of these utilities in early pro‐
164 posals due to the way processing of trailing slashes was specified.
165 Consideration was given to leaving processing unspecified if there were
166 trailing slashes, but this cannot be done; the Base Definitions volume
167 of IEEE Std 1003.1-2001, Section 3.266, Pathname allows trailing
168 slashes. The basename and dirname utilities have to specify consistent
169 handling for all valid pathnames.
170
172 None.
173
175 basename(), Parameters and Variables
176
178 Portions of this text are reprinted and reproduced in electronic form
179 from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
180 -- Portable Operating System Interface (POSIX), The Open Group Base
181 Specifications Issue 6, Copyright (C) 2001-2003 by the Institute of
182 Electrical and Electronics Engineers, Inc and The Open Group. 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.opengroup.org/unix/online.html .
187
188
189
190IEEE/The Open Group 2003 DIRNAME(1P)