Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux Bash脚本-查找目录,并在其中执行某些操作。或者找到一个文件,用它做点什么_Linux_Bash_Ubuntu_Command Line_Terminal - Fatal编程技术网

Linux Bash脚本-查找目录,并在其中执行某些操作。或者找到一个文件,用它做点什么

Linux Bash脚本-查找目录,并在其中执行某些操作。或者找到一个文件,用它做点什么,linux,bash,ubuntu,command-line,terminal,Linux,Bash,Ubuntu,Command Line,Terminal,这就是我到目前为止所做的: 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脚本: 查找“所需目录”。如果找到,请检查它 或 查找“requi

这就是我到目前为止所做的:

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脚本:

查找“所需目录”。如果找到,请检查它

查找“required_file.txt”。如果找到,请将其签出

谢谢你的帮助

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

您可以使用linux的命令

find
将递归搜索作为参数提供的目录名。
假设您有如下目录结构:

parent
-dir1
--file1
--file2
--file3
-dir2
--file4
--file5
--file6
--dir3
parent/dir1
parent/dir2
parent/dir2/dir3
现在执行以下命令:

find parent -name dir*
您将得到如下输出:

parent
-dir1
--file1
--file2
--file3
-dir2
--file4
--file5
--file6
--dir3
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
这需要输入-->

  • 您的svn父目录
  • 要搜索的文件/目录的名称

  • 希望这有帮助。如果您需要更多信息,请告诉我。

    您也可以参考此

    ,无需太多细节(因为我现在没有时间),这里是一个简短的示例,您可以使用它

    #!/bin/bash
    
    sudo updatedb
    if [ ! -f testfile1.txt ];
    then
    filelocation=$(locate awesomeo.txt)
    head -10 $filelocation
    fi
    
    它所做的是更新文件数据库,如果您要查找的文件不在当前目录中,那么它将定位该文件的目录并显示该文件的前十行

    但在您的情况下,您可能需要执行
    filelocation=$(locate-forloopvariable)
    或您正在查找的操作。当然,与if语句相比,else语句更符合您的需要


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

    我想这段代码应该有用:

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