The POSIX specification is the definitive reference on how unix tools should behave. The section on pathname resolution explains the meaning of trailing slashes in file names.
A pathname that contains at least one non-<slash> character and that ends with one or more trailing <slash> characters shall not be resolved successfully unless the last pathname component before the trailing <slash> characters names an existing directory or a directory entry that is to be created for a directory immediately after the pathname is resolved.
In other words: foo/
requires foo
to be an existing directory, or a directory that the program will create (so mkdir foo/
is permitted). If foo
is some other type of file (regular file, name piped, etc.), then accessing it as foo/
must not work.
The sentence above is incomplete: foo/
is actually a valid way to reference a symbolic link to a directory. This is specified below:
If a symbolic link is encountered during pathname resolution, the behavior shall depend on whether the pathname component is at the end of the pathname and on the function being performed. If all of the following are true, then pathname resolution is complete:
1. This is the last pathname component of the pathname.
2. The pathname has no trailing .
3. The function is required to act on the symbolic link itself, or certain arguments direct that the function act on the symbolic link itself.
In all other cases, the system shall prefix the remaining pathname, if any, with the contents of the symbolic link (…).
In other words: if foo
is a symbolic link, and the program is supposed to follow symbolic links, then foo/
is equivalent to the target of the link, which may be a directory. So if foo
is a symbolic link to a directory, foo/
is a valid way to refer to this directory. But if foo
is a symbolic to a regular file or other non-directory, then foo/
is not a valid way to refer to that file.
Functions such as open
return the error ENOTDIR
if given a pathname with a trailing slash that is an existing non-directory.
[ENOTDIR
]
(…) O_CREAT
and O_EXCL
are not specified, the path argument contains at least one non-<slash> character and ends with one or more trailing <slash> characters, and the last pathname component names an existing file that is neither a directory nor a symbolic link to a directory (…)
The impact of a slash in */
is described implicitly in the section on pattern matching in sh
. There is no special rule for the /
in a pattern (other than the rule that it cannot appear in a bracket expression, which is not relevant here). Therefore a /
in a pattern must match a /
in a pathname. For example, the pattern */
matches foo/
but not foo
. Therefore */
matches directories and symbolic link to directories, but not regular files, symbolic link to regular files, broken symbolic links, named pipes, etc.
FILENAME GENERATION
– steeldriver Mar 28 '20 at 20:58for i in *; do
does? – rr0ss0rr Mar 28 '20 at 20:59