1DIRNAME(P)                 POSIX Programmer's Manual                DIRNAME(P)
2
3
4

NAME

6       dirname - return the directory portion of a pathname
7

SYNOPSIS

9       dirname string
10

DESCRIPTION

12       The  string  operand  shall be treated as a pathname, as defined in the
13       Base Definitions volume of IEEE Std 1003.1-2001, Section  3.266,  Path‐
14       name. The string string shall be converted to the name of the directory
15       containing the filename corresponding to the last pathname component in
16       string, performing actions equivalent to the following steps in order:
17
18        1. If string is //, skip steps 2 to 5.
19
20        2. If  string  consists  entirely of slash characters, string shall be
21           set to a single slash character. In this case, skip steps 3 to 8.
22
23        3. If there are any trailing slash characters in string, they shall be
24           removed.
25
26        4. If  there are no slash characters remaining in string, string shall
27           be set to a single period character. In this case, skip steps 5  to
28           8.
29
30        5. If  there  are  any  trailing  non-slash characters in string, they
31           shall be removed.
32
33        6. If the remaining string is //, it is implementation-defined whether
34           steps 7 and 8 are skipped or processed.
35
36        7. If there are any trailing slash characters in string, they shall be
37           removed.
38
39        8. If the remaining string is empty, string shall be set to  a  single
40           slash character.
41
42       The resulting string shall be written to standard output.
43

OPTIONS

45       None.
46

OPERANDS

48       The following operand shall be supported:
49
50       string A string.
51
52

STDIN

54       Not used.
55

INPUT FILES

57       None.
58

ENVIRONMENT VARIABLES

60       The  following  environment  variables  shall  affect  the execution of
61       dirname:
62
63       LANG   Provide a default value for the  internationalization  variables
64              that  are  unset  or  null.  (See the Base Definitions volume of
65              IEEE Std 1003.1-2001, Section  8.2,  Internationalization  Vari‐
66              ables  for the precedence of internationalization variables used
67              to determine the values of locale categories.)
68
69       LC_ALL If set to a non-empty string value, override the values  of  all
70              the other internationalization variables.
71
72       LC_CTYPE
73              Determine  the  locale  for  the  interpretation of sequences of
74              bytes of text data as characters (for  example,  single-byte  as
75              opposed to multi-byte characters in arguments).
76
77       LC_MESSAGES
78              Determine  the  locale  that should be used to affect the format
79              and contents of diagnostic messages written to standard error.
80
81       NLSPATH
82              Determine the location of message catalogs for the processing of
83              LC_MESSAGES .
84
85

ASYNCHRONOUS EVENTS

87       Default.
88

STDOUT

90       The  dirname  utility  shall write a line to the standard output in the
91       following format:
92
93
94              "%s\n", <resulting string>
95

STDERR

97       The standard error shall be used only for diagnostic messages.
98

OUTPUT FILES

100       None.
101

EXTENDED DESCRIPTION

103       None.
104

EXIT STATUS

106       The following exit values shall be returned:
107
108        0     Successful completion.
109
110       >0     An error occurred.
111
112

CONSEQUENCES OF ERRORS

114       Default.
115
116       The following sections are informative.
117

APPLICATION USAGE

119       The definition of pathname  specifies  implementation-defined  behavior
120       for  pathnames starting with two slash characters.  Therefore, applica‐
121       tions shall not arbitrarily add slashes to the beginning of a  pathname
122       unless they can ensure that there are more or less than two or are pre‐
123       pared to deal with the implementation-defined consequences.
124

EXAMPLES

126                           Command            Results
127                           dirname /          /
128                           dirname //         / or //
129                           dirname /a/b/      /a
130                           dirname //a//b//   //a
131                           dirname            Unspecified
132
133                           dirname a          . ($? = 0)
134                           dirname ""         . ($? = 0)
135                           dirname /a         /
136                           dirname /a/b       /a
137                           dirname a/b        a
138

RATIONALE

140       The dirname utility originated in System III. It  has  evolved  through
141       the System V releases to a version that matches the requirements speci‐
142       fied in this description in System V Release 3.  4.3  BSD  and  earlier
143       versions did not include dirname.
144
145       The   behaviors   of   basename   and   dirname   in   this  volume  of
146       IEEE Std 1003.1-2001 have been coordinated so that  when  string  is  a
147       valid pathname:
148
149
150              $(basename "string")
151
152       would be a valid filename for the file in the directory:
153
154
155              $(dirname "string")
156
157       This  would  not work for the versions of these utilities in early pro‐
158       posals due to the way processing of  trailing  slashes  was  specified.
159       Consideration was given to leaving processing unspecified if there were
160       trailing slashes, but this cannot be done; the Base Definitions  volume
161       of   IEEE Std 1003.1-2001,  Section  3.266,  Pathname  allows  trailing
162       slashes. The basename and dirname utilities have to specify  consistent
163       handling for all valid pathnames.
164

FUTURE DIRECTIONS

166       None.
167

SEE ALSO

169       basename() , Parameters and Variables
170
172       Portions  of  this text are reprinted and reproduced in electronic form
173       from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
174       --  Portable  Operating  System  Interface (POSIX), The Open Group Base
175       Specifications Issue 6, Copyright (C) 2001-2003  by  the  Institute  of
176       Electrical  and  Electronics  Engineers, Inc and The Open Group. In the
177       event of any discrepancy between this version and the original IEEE and
178       The  Open Group Standard, the original IEEE and The Open Group Standard
179       is the referee document. The original Standard can be  obtained  online
180       at http://www.opengroup.org/unix/online.html .
181
182
183
184IEEE/The Open Group                  2003                           DIRNAME(P)
Impressum