从未来搜索中排除子目录

时间:2017-03-22 21:19:01

标签: bash shell

我想知道如何设置我的目录以从搜索中排除某个子目录

基本上如果使用./myprogram -exclude [FOLDER]

启动了shellscript

我想从我以后的所有搜索中排除子目录

让我们说我们有一个名为TEST的测试目录

  

TEST

           

>子目录/ file3.tex
  > FILE1.TXT
  > file2.pdf

所以我的程序看起来像这样

#!bin/bash
if [ $1 == "-exclude" ]
then
  DIR="$PWD"
  EXCLUDED="$2"
  if [ -d "$EXCLUDED" ]
  then
    DIR=`find $DIR -type d ! -path "$EXCLUDED"`
    # this actually works will only write the root directory TEST
  fi

  #However, when I do then
  FILES=`find "$DIR" -type f`
  echo "$FILES"
  # I get also the "subdirectory included which should not be happening"

任何设置它的想法,所以我不必手动将每个find命令更改为复杂的-pruning和-o,只使用我的DIR排除指定的子目录?

从技术上讲,我知道为什么会发生这种情况,因为DIR被设置为根目录,因为find $DIR -type d ! -path $EXCLUDED仅返回root(TEST),因为当我使用find $DIR -type f时它会执行命令find TEST -type f,这显然甚至包括我想要排除的目录,但不知怎的,我正在寻找是否有可能以某种方式改变DIR的路径以排除子目录。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

好的,所以我找到了答案,不完全是我想要的,但我想用find命令,我们可以修改目录而不是

EXCLUDED="*/""$2""/*"

并设置FILES=$(find "$DIR" -type f ! -path "$EXCLUDED")并且它可以正常工作

相关问题