1

$SKIPを使用してサブディレクトリをスキップしたいfind
残念ながら、この基本的なコマンドはすべての場合に機能するとは限りません。

find $DIR -path $SKIP -prune -o print

スクリプトの改善を手伝ってくださいskipit.sh

#!/bin/bash
SKIP=$1
shift
find "${@:-.}" -path "$SKIP" -prune -o -print

基本的な例

例は、次のディレクトリ ツリーに基づいています。

dir
├── skip
├── a
└── b

わかった:

> ./skipit.sh  dir/skip  dir
dir
dir/a
dir/b

不合格:

> ./skipit.sh  dir/skip  ./dir
./dir
./dir/skip
./dir/b
./dir/a

不合格:

> ./skipit.sh  ./dir/skip  dir
dir
dir/skip
dir/b
dir/a

使用して-regex

> cat  skipit.sh
#!/bin/bash
SKIP=${1#./}  #remove leading './'
shift
find "${@:-.}" -regex "[.]?/?$SKIP" -prune -o -print

わかった:

> ./skipit.sh  dir/skip  ./dir
./dir
./dir/b
./dir/a

わかった:

> ./skipit.sh  ./dir/skip  dir
dir
dir/b
dir/a

不合格:

> ./skipit.sh  ./dir/skip  dir/skip/..
dir/skip/..
dir/skip/../skip
dir/skip/../b
dir/skip/../a

私のfindバージョン

> find --version
find (GNU findutils) 4.4.2
Copyright (C) 2007 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Eric B. Decker, James Youngman, and Kevin Dalley.
Construit avec GNU gnulib version e5573b1bad88bfabcda181b9e0125fb0c52b7d3b
Fonctions activées : D_TYPE O_NOFOLLOW(enabled) LEAF_OPTIMISATION FTS() CBO(level=0) 

使用できますrealpathが、このコマンドはデフォルトではインストールされないため、他のユーザーにインストールを強制したくありません。も使用できますreadlinkが、機能に基づいたソリューションをお勧めしfindます ;)

チャレンジ

/home/user/dir1
├── skip
└── dir
    └── skip
/home/user/dir2
└── skip

skipit.sh次の例では、 must ignore directory /home/user/dir1/skiponlyの出力は次のようになります。

/home/user> ./skipit.sh  /home/user/dir1/skip dir1 dir2
dir1
dir1/dir    
dir1/dir/skip
dir2
dir2/skip

/home/user> ./skipit.sh  dir1/skip ./dir1 ./dir2
./dir1
./dir1/dir    
./dir1/dir/skip
./dir2
./dir2/skip

/home/user> ./skipit.sh  ../user/dir1/skip ./dir1 ./dir2
./dir1
./dir1/dir    
./dir1/dir/skip
./dir2
./dir2/skip

/home/user> ./skipit.sh  dir1/skip /home/user/dir1 /home/user/dir2
/home/user/dir1
/home/user/dir1/dir    
/home/user/dir1/dir/skip
/home/user/dir2
/home/user/dir2/skip
4

2 に答える 2

2

これを試して:

#!/bin/bash

SKIP="$(sed -r 's|^(\.?\./)*||' <<<$1)"
shift

for arg in "${@:-.}"; do
  root="$(realpath "$arg")"
  find "$root" -regex ".*$SKIP$" -prune -o -print
done

フォルダ構造の例:

/tmp/test
├── dir1
│   ├── dir
│   │   └── skip
│   └── skip
└── dir2
    └── skip

出力:

/tmp/test $ ./test.sh dir/skip dir1/skip/..
/tmp/test/dir1
/tmp/test/dir1/dir
/tmp/test/dir1/skip

/tmp/test $ ./test.sh /tmp/test/dir1/skip dir1 dir2
/tmp/test/dir1
/tmp/test/dir1/dir
/tmp/test/dir1/dir/skip
/tmp/test/dir2
/tmp/test/dir2/skip

/tmp/test $ ./test.sh dir1/skip dir1 dir2
/tmp/test/dir1
/tmp/test/dir1/dir
/tmp/test/dir1/dir/skip
/tmp/test/dir2
/tmp/test/dir2/skip

/tmp/test $ ./test.sh ../test/dir1/skip ./dir1 ./dir2
/tmp/test/dir1
/tmp/test/dir1/dir
/tmp/test/dir1/dir/skip
/tmp/test/dir2
/tmp/test/dir2/skip

/tmp/test $ ./test.sh dir1/skip /tmp/test/dir1 /tmp/test/dir2
/tmp/test/dir1
/tmp/test/dir1/dir
/tmp/test/dir1/dir/skip
/tmp/test/dir2
/tmp/test/dir2/skip
于 2013-07-28T18:56:22.580 に答える