【问题标题】:How to loop through directory which have multiple subdirectories?如何遍历具有多个子目录的目录?
【发布时间】:2017-11-14 18:58:36
【问题描述】:

我必须获取所有子目录的目录路径,这些子目录下具有子目录,并将输出打印到具有所有子目录路径的文件中。我的问题是我可以做类似的事情

find .mymountpoint/DataRoot/DATAINPUT_*/PER201448_*/IPD_*/ 
 -maxdepth 1 -mindepth 1 -type d
 -printf '/root/mymountpoint/DataRoot/DATAINPUT/PER201448/IPD/%f\n' 
 > dirpathfile.txt

但它会将所有目录路径的输出打印到我不想要的一个位置。我希望子目录路径必须仅使用 linux 中的 shell 脚本才能转到该特定目录路径。

如果我不清楚,道歉和任何帮助将不胜感激。

谢谢

【问题讨论】:

    标签: bash shell unix directory


    【解决方案1】:

    据我了解,您想打印绝对路径。您可以为此使用realpath 命令:

    find ... -exec realpath {} +
    

    【讨论】:

    • 嗨@hek2mgl 你能给出确切的命令吗,我对realpath不太熟悉。
    • 只是你的命令,但是用-exec realpath {} +替换-printf ...
    • 嗨@hek2mgl 遇到这个错误,[root@testvm01 mymountpoint]# find ./ShopperVisionDataRoot/DATA_PREP_INPUT/PER_/IPD_*/ -maxdepth 1 -mindepth 1 -type d -exec realpath {} + '/%f\n' > /root/mymountpoint/dirpathlist/test.txt -bash: /bin/find: 参数列表太长
    • 仅将 ./ShopperVisionDataRoot/DATA_PREP_INPUT/PER_/ 传递给 find 命令。否则参数列表会变得太长。然后使用find-path 参数进行过滤。 (查看手册页)
    猜你喜欢
    • 1970-01-01
    • 2016-01-22
    • 1970-01-01
    • 1970-01-01
    • 2016-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多