Linux bash 查找目录

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

bash find directories

linuxbashdirectory

提问by

I am new to bash scripts. I'm just trying to make a script that will search through a directory and echo the names of all subdirectories.

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

The basis for the code is the following script (call it isitadirectory.sh):

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

     #!/bin/bash

     if test -d 
         then
                echo ""
     fi

so in the command line if I type

所以在命令行中,如果我输入

       $bash isitadirectory.sh somefilename 

It will echo somefilename, if it is a directory.

如果它是一个目录,它将回显某个文件名。

But I want to search through all files in the parent directory.

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

So, I'm trying to find a way to do something like

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

           ls -l|isitadirectory.sh

But of course the above command doesn't work. Can anyone explain a good script for doing this?

但是当然上面的命令不起作用。任何人都可以解释这样做的好脚本吗?

采纳答案by Learner

Following lines may give you an idea...what you are asking for

以下几行可能会给你一个想法......你在要求什么

#!/bin/bash

for FILE in `ls -l`
do
    if test -d $FILE
    then
      echo "$FILE is a subdirectory..."
    fi
done

You may have a look into bash 'for' loop.

您可以查看 bash 'for' 循环。

回答by zb'

try to use

尝试使用

find $path -type d?

find $path -type d?

for current directory

对于当前目录

find . -type d

find . -type d

回答by Ignacio Vazquez-Abrams

find . -mindepth 1 -maxdepth 1 -type d

回答by Jan Marek

You have to use:

你必须使用:

ls -lR | isitadirectory.sh

(parameter -R is recursion)

(参数 -R 是递归)

回答by Johan

Not sure.. but maybe the tree command is something you should look at. http://linux.die.net/man/1/tree

不确定……但也许 tree 命令是您应该查看的内容。http://linux.die.net/man/1/tree

tree -L 2 -fi

回答by jm666

Here is already much solutions, so only for fun:

这里已经有很多解决方案,所以只是为了好玩:

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

回答by fiorentinoing

In the particular case you are looking for a 1) directory which you know the 2) name, why not trying with this:

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

find . -name "octave" -type d

回答by Jason Jewel

find ./path/to/directory -iname "test" -type d

I found this very useful for finding directory names using -iname for case insensitive searching. Where "test" is the search term.

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