매일 Bash에서 코딩하지 않습니다. 작은 기능을 구현하려고합니다 : 사용자가 찾기 명령에서 생략 할 디렉토리 또는 파일 배열을 정의합니다. 아쉽게도 별표 및 기타 메타 문자를 셸 (* 병합 중에 확장 됨)으로 확장하는 데 문제가 있습니다. 내 코드 :셸별로 별표를 확장하지 않음 - 경로를 제외 항목으로 검색
excluded=("subdirectory/a/*"
"subdirectory/b/*"
)
cnt=0
for i in "${excluded[@]}"; do
directories="$directories ! -path \"./${excluded[$cnt]}\""
cnt=$(($cnt+1))
done
echo "$directories"
for i in $(find . -type f -name "*.txt" $directories); do
new_path=$(echo "$i"|sed "s/\.\//..\/up\//g")
echo $new_path
done
불행히도 결과에서 제외 된 디렉토리가 표시됩니다.
편집 :
이 기존 질문을 중복되지 않습니다. find에서 디렉토리를 제외하는 방법을 묻지 않습니다. 명령을 찾기 위해 변수를 전달하여 "*"와 같은 메타 문자를 확장하는 데 문제가 있습니다. 예를 들어, 아래에 거의 해결책이 있습니다.
excluded=("subdirectory/a/*"
"subdirectory/b/*"
)
cnt=0
for i in "${excluded[@]}"; do
directories="$directories ! -path ./${excluded[$cnt]}"
cnt=$(($cnt+1))
done
echo "$directories"
for i in $(find . -type f -name "*.txt" $directories); do
new_path=$(echo "$i"|sed "s/\.\//..\/up\//g")
echo $new_path
done
예를 들어 디렉토리 c에 둘 이상의 파일이있는 경우 문제가됩니다. 이 경우 별표 기호가 전체 파일 경로로 바뀝니다. 따라서 오류가 있습니다 :
find: paths must precede expression: ./subdirectory/c/fgo.txt
Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]
왜? 별표 sgin 전체 파일 이름에 확장 되었기 때문에 :
! -path ./subdirectory/a/aaa.txt ! -path ./subdirectory/b/dfdji.txt ! -path ./subdirectory/c/asd.txt ./subdirectory/c/fgo.txt
내 질문은 : 어떻게 이러한 상황을 피하기 위해?
[중복 제외] 명령] (http://stackoverflow.com/questions/4210042/exclude-directory-from-find-command) – ffledgling
별로. 별표 확장을 피하는 것에 대해 물었습니다. 구문을 찾는 것이 아니라 제 EDIT를 참조하십시오. – Viper