可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I am writing a script in bash on Linux and need to go through all subdirectory names in a given directory. How can I loop through these directories (and skip regular files)?
For example:
the given directory is /tmp/
it has the following subdirectories: /tmp/A, /tmp/B, /tmp/C
I want to retrieve A, B, C.
回答1:
cd /tmp
find . -maxdepth 1 -mindepth 1 -type d -printf '%f\n'
A short explanation: find
finds files (quite obviously)
. is the current directory, after the cd it's /tmp
(IMHO this is more flexible than having /tmp
directly in the find command. You have only one place, the cd
, to change, if you want more actions to take place in this folder)
-maxdepth 1
and -mindepth 1
make sure, that find
really, only looks in the current dir and doesn't include '.
' in the result
-type d
looks only for directories
-printf '%f\n
prints only the found folder's name (plus a newline) for each hit.
E voila!
回答2:
All answers so far use find
, so here's one with just the shell. No need for external tools in your case:
for dir in /tmp/*/ # list directories in the form "/tmp/dirname/"
do
dir=${dir%*/} # remove the trailing "/"
echo ${dir##*/} # print everything after the final "/"
done
回答3:
You can loop through all directories including hidden directrories (beginning with a dot) with:
for file in */ .*/ ; do echo "$file is a directory"; done
note: using the list */ .*/
works in zsh only if there exist at least one hidden directory in the folder. In bash it will show also .
and ..
Another possibility for bash to include hidden directories would be to use:
shopt -s dotglob;
for file in */ ; do echo "$file is a directory"; done
If you want to exclude symlinks:
for file in */ ; do
if [[ -d "$file" && ! -L "$file" ]]; then
echo "$file is a directory";
fi;
done
To output only the trailing directory name (A,B,C as questioned) in each solution use this within the loops:
file="${file%/}" # strip trailing slash
file="${file##*/}" # strip path and leading slash
echo "$file is the directoryname without slashes"
Example (this also works with directories which contains spaces):
mkdir /tmp/A /tmp/B /tmp/C "/tmp/ dir with spaces"
for file in /tmp/*/ ; do file="${file%/}"; echo "${file##*/}"; done
回答4:
Works with directories which contains spaces
Inspired by Sorpigal
while IFS= read -d $'\0' -r file ; do
echo $file; ls $file ;
done < <(find /path/to/dir/ -mindepth 1 -maxdepth 1 -type d -print0)
Original post (Does not work with spaces)
Inspired by Boldewyn: Example of loop with find
command.
for D in $(find /path/to/dir/ -mindepth 1 -maxdepth 1 -type d) ; do
echo $D ;
done
回答5:
find . -mindepth 1 -maxdepth 1 -type d -printf "%P\n"
回答6:
The technique I use most often is find | xargs
. For example, if you want to make every file in this directory and all of its subdirectories world-readable, you can do:
find . -type f -print0 | xargs -0 chmod go+r
find . -type d -print0 | xargs -0 chmod go+rx
The -print0
option terminates with a NULL character instead of a space. The -0
option splits its input the same way. So this is the combination to use on files with spaces.
You can picture this chain of commands as taking every line output by find
and sticking it on the end of a chmod
command.
If the command you want to run as its argument in the middle instead of on the end, you have to be a bit creative. For instance, I needed to change into every subdirectory and run the command latemk -c
. So I used (from Wikipedia):
find . -type d -depth 1 -print0 | \
xargs -0 sh -c 'for dir; do pushd "$dir" && latexmk -c && popd; done' fnord
This has the effect of for dir $(subdirs); do stuff; done
, but is safe for directories with spaces in their names. Also, the separate calls to stuff
are made in the same shell, which is why in my command we have to return back to the current directory with popd
.
回答7:
find . -type d -maxdepth 1
回答8:
a minimal bash loop you can build off of (based off ghostdog74 answer)
for dir in directory/*
do
echo ${dir}
done
to zip a whole bunch of files by directory
for dir in directory/*
do
zip -r ${dir##*/} ${dir}
done
回答9:
If you want to execute multiple commands in a for loop, you can save the result of find
with mapfile
(bash >= 4) as an variable and go through the array with ${dirlist[@]}
. It also works with directories containing spaces.
The find
command is based on the answer by Boldewyn. Further information about the find
command can be found there.
IFS=""
mapfile -t dirlist < <( find . -maxdepth 1 -mindepth 1 -type d -printf '%f\n' )
for dir in ${dirlist[@]}; do
echo ">${dir}<"
# more commands can go here ...
done