bash 在 shell 脚本中使用 for 循环遍历目录结构

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15081421/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 04:40:25  来源:igfitidea点击:

Traverse a directory structure using a for loop in shell scripting

bashshellunix

提问by gmhk

I am looking to traverse a directory using a conditional for / while Loop

我正在寻找使用条件 for / while 循环遍历目录

Scenario : path is /home/ABCD/apple/ball/car/divider.txt

场景:路径为/home/ABCD/apple/ball/car/divider.txt

Say I always start my program from /home I need to iterate from home -- > ABCD --> apple --> ball --> car --> divider.txt

假设我总是从 /home 开始我的程序,我需要从家里迭代 --> ABCD --> 苹果 --> 球 --> 汽车 -->divider.txt

Every time I iterate, check if the obtained path is a directory or a file, if file exit the loop and return me the path if the returned path is directory, loop one more round and continue..

每次迭代时,检查获取的路径是目录还是文件,如果文件退出循环,如果返回路径是目录,则返回路径,再循环一轮并继续..

Updated question

更新的问题

FILES="home"
for f in $FILES
do

    echo "Processing $f"  >> "I get ABCD as output
        if[-d $f]
  --> returns true, in my next loop, I should get the output as /home/ABCD/apple..
        else 
          Break;
        fi

done

after I exit the for loop, I should have the /home/ABCD/apple/ball/car/ as output

退出 for 循环后,我应该将 /home/ABCD/apple/ball/car/ 作为输出

回答by Perleone

Besides the multi-purpose findyou might also want to take a look at tree. It will list contents of directories in a tree-like format.

除了多用途之外,find您可能还想看看tree. 它将以树状格式列出目录的内容

$ tree -F /home/ABCD/
/home/ABCD/
`-- apple/
    `-- ball/
        `-- car/
            `-- divider.txt

3 directories, 1 file

回答by gmhk

This is the way I have implemented to get it working

这是我为使其工作而实施的方式

for names in $(find /tmp/files/ -type f); 
do
    echo " ${directoryName} -- Directory Name found after find command : names" 

    <== Do your Processing here  ==>

done

Names will have each file with the complete folder level

名称将包含具有完整文件夹级别的每个文件

/tmp/files is the folder under which I am finding the files

/tmp/files 是我在其下查找文件的文件夹

回答by MelBurslan

find /home -type d

find /home -type d

will give you all the directories under /home and nothing else. replace /home with the directory of your choice and you will get directories under that level.

会给你 /home 下的所有目录,没有别的。将 /home 替换为您选择的目录,您将获得该级别下的目录。

if your heart is set on checking every file one by one, the if..then test condition you are looking for is :

如果您一心一意地逐个检查每个文件,那么您正在寻找的 if..then 测试条件是:

if [ -f $FILE ]
then
echo "this is a regular file"
else 
echo "this is not a regular file, but it might be a special file, a pipe etc."
fi

-or-

-或者-

if [ -d $FILE ]
then
echo "this is a directory. Your search should go further"
else 
echo "this is a file and buck stops here"
fi