![]() ![]() Pathnames that are directories ( -type d) are collected and a sh -c script is executed with batches of these as arguments. This limits the search to only the directory referenced by $topdir. The find utility is asked to ignore pathnames that are identical to the starting search path with ! -path "$topdir", and then to prune (not enter) any other pathnames found. Set topdir to some other pathname to use it on another directory. The above code assumes that the top directory that we're interested in is the current directory (. Printf "\"%s\" is a directory\n" "$dirpath" This answer is assuming that all that needs to be done is to find the sub-directories of some top-level directory.įor an answer specific to bash (and to some degree, the zsh shell), see the second part of rubo77's wiki answer, where dotglob and nullglob is being used to correctly loop over the directories (or symbolic links to directories) in a single directory.įor a portable solution that does not depend on bash for doing the actual selection of files, you may use find to loop over all directories in some top-level directory like so: topdir=.įind "$topdir" ! -path "$topdir" -prune -type d -exec sh -c ' ![]() You may unset dotglob and nullglob with shopt -u dotglob nullglob (Use the pattern *(ND/) in the zsh shell the / makes the preceding * match only directories, and the ND makes it act as if both nullglob and dotglob were set) The nullglob shell option makes the pattern disappear completely (instead of remaining unexpanded) if no name matches it. will be with the dotglob shell option in bash: shopt -s dotglob nullglobįor name in */ do printf '%s is a directory\n' "$name" done while in zsh it will not show these but throw an error if there is no hidden file in the folderĪ cleaner version that will include hidden directories and exclude. */ works in bash, but also displays the folders. If you want to exclude symbolic links: for name in * do */ do printf '%s is a directory\n' "$name" done You can loop through all directories including hidden directories (beginning with a dot) in one line and multiple commands with: for name in */. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |