【问题标题】:Bash script - find directory, and do something in it. Or find a file, and do something with itBash 脚本 - 找到目录,并在其中做一些事情。或者找到一个文件,然后用它做点什么
【发布时间】:2013-03-15 18:45:34
【问题描述】:

这是我目前所拥有的:

for f in 'svn ls repository_dir'; 
do 
svn checkout repository_dir/$f/trunk/dir1/dir2/dir3/dir4/needed_dir 
done

这对于在正确位置具有所需目录的项目(其中有 100 个)非常有用。但有些项目 ($f) 的目录结构略有不同。所以“needed_dir”可能在不同的位置。

在 do 循环中,我如何告诉我的 bash 脚本:

“查找“needed_dir”。如果找到,请查看。”

或者

“查找“needed_file.txt”。如果找到,请查看。”

感谢您的帮助

编辑:抱歉,这可能更像是一个 superuser.com 问题。我其实是想把它写在那里。但是,也许有人也可以在这里帮助我!

【问题讨论】:

    标签: linux bash ubuntu command-line terminal


    【解决方案1】:

    你可以使用linux的find命令。

    find 将递归搜索您作为参数提供的目录名称。
    假设你有一个这样的目录结构:

    parent
    -dir1
    --file1
    --file2
    --file3
    -dir2
    --file4
    --file5
    --file6
    --dir3
    

    现在你执行以下命令:

    find parent -name dir*
    

    你会得到这样的输出:

    parent/dir1
    parent/dir2
    parent/dir2/dir3
    

    例如看下面的代码:-

    #!/bin/sh
    parentdir=$1
    tofind=$2
    for i in `find $parentdir -name $tofind*`;  # * is added for regular expression..do whatever you need here
    do
     #check if it is directory
     if [ -d $i ]; then
        # do what you want to do 
        echo $i
     else
        # do something else
        echo $i
     fi
    done
    

    这需要输入-->

    1. 你的 svn 父目录
    2. 要搜索的文件/目录的名称


    希望这会有所帮助。如果您需要进一步的输入,请告诉我。
    你也可以参考这个question

    【讨论】:

      【解决方案2】:

      这里没有详细介绍(因为我现在没有时间)这里是一个简短的例子,你可以使用它。

      #!/bin/bash
      
      sudo updatedb
      if [ ! -f testfile1.txt ];
      then
      filelocation=$(locate awesomeo.txt)
      head -10 $filelocation
      fi
      

      它的作用是更新文件数据库,如果您要查找的文件不在当前目录中,则它会定位文件的目录并显示该文件的前十行。

      但在您的情况下,您可能想做filelocation=$(locate forloopvariable) 或您正在寻找的东西。这当然不是 if 语句,而是满足您需求的 else 语句。

      如果您有任何其他问题,请随时提出。

      【讨论】:

        【解决方案3】:

        我猜这个 sn-p 应该可以工作:

        for f in $(svn ls repository_dir); 
        do 
          find repository_dir/$f -type d -name needed_dir | xargs -r svn checkout 
        done
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-08-29
          • 2010-10-16
          • 2017-09-08
          • 2021-12-31
          • 2014-05-27
          • 2013-09-24
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多