BASH:编写一个脚本来递归遍历 N 级目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18897264/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
BASH: Writing a Script to Recursively Travel a Directory of N Levels
提问by user000001
I have the following directory structure for example:
例如,我有以下目录结构:
/test_dir/d
/test_dir/d/cron
/test_dir/d/cache
/test_dir/d/...(more sub dirs)
/test_dir/tree
/test_dir/tree/a
/test_dir/tree/a/a1
/test_dir/tree/a/a2
...(and so on for b/ and c/ )
I've written the following bash script that effectively travels to the second level of /test_dir
so it will reach /test_dir/d/cron
or /test_dir/tree/a
but will not go further. I cannot figure out why the recursive script will not travel further can someone please debug the script and point out my error?
我已经编写了以下 bash 脚本,它可以有效地传送到第二级,/test_dir
因此它将达到/test_dir/d/cron
或/test_dir/tree/a
但不会更进一步。我无法弄清楚为什么递归脚本不会进一步传播有人可以调试脚本并指出我的错误吗?
here is what I have written:
这是我写的:
#!/bin/bash
#script to recursively travel a dir of n levels
function traverse() {
for file in `ls `
do
#current={$file}
if [ ! -d ${file} ] ; then
echo " ${file} is a file"
else
#echo "entering recursion with: ${file}"
traverse "/${file}"
fi
done
}
function main() {
traverse
}
main
here is the output:
这是输出:
/test_dir/a is a file
/test_dir/b is a file
/test_dir//dcache is a file
/test_dir//dcron is a file
/test_dir//dgames is a file
/test_dir//dlib is a file
/test_dir//dlog is a file
/test_dir//drun is a file
/test_dir//dtmp is a file
/test_dir/movies is a file
/test_dir//treea is a file
/test_dir//treeb is a file
/test_dir//treec is a file
/test_dir//treed is a file
I know there are probably more elegant one line commands to do this. But im trying to do it in this explicit manner. I apologize for the length of this post.
我知道可能有更优雅的单行命令可以做到这一点。但我试图以这种明确的方式做到这一点。对于这篇文章的长度,我深表歉意。
EDIT: using traverse "${1}/${file}"
编辑:使用 traverse "${1}/${file}"
回答by user000001
Several problems with the script. It should be like this:
脚本的几个问题。应该是这样的:
#!/bin/bash
#script to recursively travel a dir of n levels
function traverse() {
for file in ""/*
do
if [ ! -d "${file}" ] ; then
echo "${file} is a file"
else
echo "entering recursion with: ${file}"
traverse "${file}"
fi
done
}
function main() {
traverse ""
}
main ""
However, the correct way to recursively traverse a directory is by using the find command:
但是,递归遍历目录的正确方法是使用 find 命令:
find . -print0 | while IFS= read -r -d '' file
do
echo "$file"
done
回答by konsolebox
/test_dir/treea is a file
/test_dir/treeb is a file
/test_dir/treec is a file
/test_dir/treed is a file
These are the errors. Some which are not only not directories but do not exist are considered files. I think you need to separate your new files from the directory with slash:
这些是错误。一些不仅不是目录而且不存在的被认为是文件。我认为您需要使用斜杠将新文件与目录分开:
traverse "/${file}"
And also check that they do exist as well.
还要检查它们是否也存在。
Since you're using bash as well, I'd suggest this form instead:
由于您也在使用 bash,我建议改为使用这种形式:
#!/bin/bash
shopt -s dotglob ## Optionally would allow matches for directories beginning with .
shopt -s nullglob
function traverse {
local a file
for a; do
for file in "$a"/*; do
if [[ -d $file ]]; then
traverse "$file"
else
echo " $file is a file."
fi
done
done
}
traverse "$@"
You can run the script with multiple arguments.
您可以使用多个参数运行脚本。
A fixed version of your script:
脚本的固定版本:
#!/bin/bash
#script to recursively travel a dir of n levels
function traverse() {
for file in $(ls "")
do
#current={$file}
if [[ ! -d /${file} ]]; then
echo " /${file} is a file"
else
#echo "entering recursion with: ${file}"
traverse "/${file}"
fi
done
}
function main() {
traverse ""
}
main ""