【问题标题】:bash find directoriesbash 查找目录
【发布时间】:2011-10-10 21:39:24
【问题描述】:

我是 bash 脚本的新手。我只是在尝试制作一个脚本来搜索目录并回显所有子目录的名称。

代码的基础是以下脚本(称为isitadirectory.sh):

     #!/bin/bash

     if test -d $1
         then
                echo "$1"
     fi

如果我在命令行中输入

       $bash isitadirectory.sh somefilename 

如果是目录,它会回显一些文件名。

但我想搜索父目录中的所有文件。

所以,我正在尝试找到一种方法来做类似的事情

           ls -l|isitadirectory.sh

当然,上面的命令是行不通的。谁能解释一个很好的脚本来做到这一点?

【问题讨论】:

  • 首先,如果没有授予执行权限chmod +x isitadirectory.sh,您可以执行脚本。然后你执行你的脚本./isitadirectory.sh

标签: linux bash directory


【解决方案1】:

尝试使用

find $path -type d?

当前目录

find . -type d

【讨论】:

    【解决方案2】:
    find . -mindepth 1 -maxdepth 1 -type d
    

    【讨论】:

      【解决方案3】:

      你必须使用:

      ls -lR | isitadirectory.sh
      

      (参数-R为递归)

      【讨论】:

        【解决方案4】:

        不确定.. 但也许 tree 命令是你应该看的东西。 http://linux.die.net/man/1/tree

        tree -L 2 -fi
        

        【讨论】:

          【解决方案5】:

          以下几行可能会给您一个想法……您的要求是什么

          #!/bin/bash 对于 `ls -l` 中的文件 做 如果测试 -d $FILE 然后 echo "$FILE 是一个子目录..." 菲 完毕

          你可以看看 bash 'for' 循环。

          【讨论】:

          【解决方案6】:

          这里已经有很多解决方案了,仅供娱乐:

           file ./**/* | grep directory | sed 's/:.*//'
          

          【讨论】:

            【解决方案7】:

            在特定情况下,您正在寻找一个 1) 您知道 2) 名称的目录,为什么不试试这个:

            find . -name "octave" -type d
            

            【讨论】:

              【解决方案8】:
              find ./path/to/directory -iname "test" -type d
              

              我发现这对于使用 -iname 进行不区分大小写的搜索来查找目录名称非常有用。其中“test”是搜索词。

              【讨论】:

                【解决方案9】:

                在我的情况下,我需要完整路径,所以我最终使用了

                find $(pwd) -maxdepth 1 -type d -not -path '*/\.*' | sort
                

                我需要使用一个 bash 脚本提取大量存储库。这是脚本:

                #!/bin/bash
                
                cd /path/where/to/look/for/dirs
                
                # Iterate over files ending with .pub
                echo -e "Pulling all repos"
                FILES=`find $(pwd) -maxdepth 1 -type d -not -path '*/\.*' | sort`
                for f in $FILES
                do
                    echo -e "pulling from repo $f..."
                    cd $f
                    git pull
                done
                

                【讨论】:

                  猜你喜欢
                  • 2018-12-15
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2021-12-14
                  • 2011-12-29
                  • 2016-04-22
                  • 2022-01-15
                  • 2015-06-27
                  相关资源
                  最近更新 更多