고유 한 콘텐츠가있는 파일을 테스트하는 방법은 무엇입니까?
if diff "$file1" "$file2" > /dev/null; then
...
디렉토리에서 파일 목록을 어떻게 얻을 수 있습니까?
files="$( find ${files_dir} -type f )"
이 목록에서 2 개의 파일을 가져 와서 이름이 다르고 내용이 같은지 확인할 수 있습니다.
#!/bin/bash
# removeDuplicates.sh
files_dir=$1
if [[ -z "$files_dir" ]]; then
echo "Error: files dir is undefined"
fi
files="$( find ${files_dir} -type f )"
for file1 in $files; do
for file2 in $files; do
# echo "checking $file1 and $file2"
if [[ "$file1" != "$file2" && -e "$file1" && -e "$file2" ]]; then
if diff "$file1" "$file2" > /dev/null; then
echo "$file1 and $file2 are duplicates"
rm -v "$file2"
fi
fi
done
done
예를 들어, 우리는 약간의 dir을 가지고 있습니다 :
$> ls .tmp -1
all(2).txt
all.txt
file
text
text(2)
따라서 3 개의 고유 한 파일 만 있습니다.
해당 스크립트를 실행할 수 있습니다.
$> ./removeDuplicates.sh .tmp/
.tmp/text(2) and .tmp/text are duplicates
removed `.tmp/text'
.tmp/all.txt and .tmp/all(2).txt are duplicates
removed `.tmp/all(2).txt'
그리고 3 개의 파일 만 남습니다.
$> ls .tmp/ -1
all.txt
file
text(2)