![폴더에서 특정 패턴과 일치하는(또는 일치하지 않는) 모든 파일을 찾는 방법은 무엇입니까?](https://linux55.com/image/72872/%ED%8F%B4%EB%8D%94%EC%97%90%EC%84%9C%20%ED%8A%B9%EC%A0%95%20%ED%8C%A8%ED%84%B4%EA%B3%BC%20%EC%9D%BC%EC%B9%98%ED%95%98%EB%8A%94(%EB%98%90%EB%8A%94%20%EC%9D%BC%EC%B9%98%ED%95%98%EC%A7%80%20%EC%95%8A%EB%8A%94)%20%EB%AA%A8%EB%93%A0%20%ED%8C%8C%EC%9D%BC%EC%9D%84%20%EC%B0%BE%EB%8A%94%20%EB%B0%A9%EB%B2%95%EC%9D%80%20%EB%AC%B4%EC%97%87%EC%9E%85%EB%8B%88%EA%B9%8C%3F.png)
폴더 및 하위 폴더에서 3자리 숫자로 끝나는 모든 파일을 찾아서 디렉터리 구조를 유지하면서 새 위치로 이동하려면 어떻게 해야 합니까?
또는 이름이 세 자리 숫자로 끝나지 않는 모든 파일을 어떻게 찾을 수 있습니까?
답변1
@don_crissti가 링크한 답변을 기반으로 한 보다 깨끗한 솔루션입니다. (Rsync 필터: 하나의 패턴만 복사)
rsync -av --remove-source-files --include='*[0-9][0-9][0-9]' --include='*/' --exclude '*' /tmp/oldstruct/ /tmp/newstruct/
그리고 부정:
rsync -av --remove-source-files --exclude='*[0-9][0-9][0-9]' /tmp/oldstruct /tmp/newstruct/
원래 답변:
이것은 트릭을 수행해야합니다. 입력한 구조에서 3자리로 끝나는 모든 파일을 찾아 cd
대상 폴더를 만든 /tmp/newstruct
다음 파일을 이동합니다.
cd /tmp/oldstruct
find ./ -type f -regextype posix-basic -regex '.*[0-9]\\{3\\}' |
while read i; do
dest=/tmp/newstruct/$(dirname $i)
mkdir -vp $dest
mv -v $i $dest
done
실제로 실행하기 전에 및를 추가하여 예상한 대로 작동하는지 확인하는 것이 좋습니다 mkdir
.mv
echo
3자리 숫자를 부정하려면 do 를 입력하세요 ! -regex
.
이는 rsync에 의존하는 더 간단한 방법입니다. 그러나 rsync
찾은 모든 파일을 호출하므로 확실히 효율적이지는 않습니다.
find ./ -type f -regextype posix-basic -regex '.*[0-9]\{3\}' --exec rsync -av --remove-source-files --relative {} /tmp/newstruct
답변2
Bash를 사용하여 이 작업을 수행할 수 있습니다.
## Make ** match all files and 0 or more dirs and subdirs
shopt globstar
## Iterate over all files and directories
for f in **; do
## Get the name of the parent directory of the
## current file/directory
dir=$(dirname "$f");
## If this file/dir ends with 3 digits and is a file
if [[ $f =~ [0-9]{3} ]] && [ -f "$f" ]; then
## Create the target directory
mkdir -p targetdir1/"$dir"
## Move the file
mv "$f" targetdir1/"$f"
else
## If this is a file but doesn't end with 3 digits
[ -f "$f" ] &&
## Make the target dir
mkdir -p targetdir2/"$dir" &&
## Move the file
mv "$f" targetdir2/"$f"
fi
done