4

我通过 Cygwin 使用 bash。我有一个大文件夹 (a),有许多子文件夹 (b)。这些子文件夹各有一个或两个子文件夹 (c)。我想找到所有具有两个子文件夹 (c) 的子文件夹 (b) 并输出它们。

结构如下:

a
 b1
  c1 
 b2
  c1
  c2
 b3
  c1
  c2

到目前为止,我只知道如何使用find和管道输出主文件夹 (a) 中的所有子文件夹。

find . -type d > folders.txt

如何仅b将具有两个c文件夹的所有文件夹输出到每行一个文件夹的文本文件?(在我的示例中,输出应为:

b2
b3
4

4 回答 4

4

Try doing this using :

cd a
find . -type d |
awk -F/ '{arr[$2]++}END{for (a in arr) {if (arr[a] == 3) print a}}'

Or using :

cd a
for i in */; do x=( $i/*/ ); (( ${#x[@]} == 2 )) && echo "${i%/}"; done
于 2013-02-16T16:40:02.497 回答
3

..有一个更简单的解决方案,它利用来自每个子目录的父目录链接将目录的链接计数增加 1的事实。没有子目录的目录的链接计数为 2(.和来自其自己父目录的链接其名称)。因此,具有两个子目录的目录的链接计数为 4:

find . -type d -links 4

对目录进行其他硬链接是不合法的,因此不应出现误报。

于 2013-02-17T04:05:21.957 回答
0

There is a terrific open-source utility that will do what you want. It's called "tree", and it has a lot of useful options. It's included in most Linux distributions, but you should be able to compile for Cygwin.

You can find "tree" at: http://mama.indstate.edu/users/ice/tree/

You could also use Perl to this. It would be more flexible than a Bash shell script. Use the File::Find module in Perl (http://perldoc.perl.org/File/Find.html), which would allow you to easily do what you want. Do you have Perl installed in your Cygwin environment? Do you know Perl? Let me know if you'd like me to post a Perl script to do this.

于 2013-02-16T16:57:59.303 回答
0

试试这个bash 脚本-

#!/bin/bash

cd a
for i in `pwd`/* ; do
  if [ -d "$i" ]; then
    DIR=0;
    cd $i;
    for j in * ; do
    if [ -d "$j" ]; then
    let DIR=DIR+1;
    fi
    done
    if [ $DIR -eq 2 ]; then
    echo $i;
    fi
  fi
done

假设脚本的名称是test.sh,你可以做

$./test.sh > folder.txt
于 2013-02-16T17:08:43.923 回答