运行脚本以检查重复文件 - 挫折安装

时间:2014-06-05 14:34:09

标签: bash loops directory

所以我有这个 - 脚本检查输入文件夹中的重复文件...但是由于某种原因它只是没有工作,它真的开始让我感到沮丧......语法似乎从我在网上看到的内容来看是正确的

它只是没有正确循环 - 因为回声只输入一次,但是测试它的文件夹中有3个文件。我试图将第4行中的$ FOLDER位更改为/ stuff / *和stuff / *等实际名称 我正在测试的文件夹的名称。有什么想法吗?

FOLDER=$1
mkdir check

for FILE in $FOLDER;
do
echo $FILE


CHECKSUM=$(cksum $FILE|awk '{print $1}')
if [[ -e check/$CHECKSUM ]];then
echo Duplicate at $FILE
rm $FILE

else

touch check/$CHECKSUM
echo "check/$CHECKSUM"

fi

2 个答案:

答案 0 :(得分:0)

您没有对文件进行路径名扩展。也许你想要:

for FILE in "$FOLDER"/*;

如果您想要定位具有特定扩展名的文件,请执行以下操作:

for FILE in "$FOLDER"/*.ext;

测试文件是否真的是文件而不是目录也是一件好事:

for FILE in "$FOLDER"/*; do
    if [[ -f $FILE ]]; then

如果要递归处理文件,请使用while读取循环而不是find

while IFS= read FILE; do
    ...
done < <(exec find "$FOLDER" -type f)

答案 1 :(得分:0)

#!/bin/bash

# Published under GPLv3. Use at your own risk

# Script to search a folder for redundant files and delete duplicates
# Unix directive of no news is good news is followed
# Usage: <path-to-script> FOLDER [Folder] [...]

# To create a test folder containing files:
# mkdir -p duplicates;
# for foo in {1..5}; do dd if=/dev/zero of=duplicates/$foo bs=4K count=$foo; done;
# cp duplicates/4 duplicates/4-copy

declare -A FILE_CKSUMS;

for FOLDER_TO_CHECK;
do
    [[ -d "${FOLDER_TO_CHECK}" ]] && [[ -w "${FOLDER_TO_CHECK}" ]] \
        &&
        {

            unset 'FILE_CKSUMS[@]';

            [[ "${FOLDER_TO_CHECK: -1}" != "/" ]] \
                && FOLDER_TO_CHECK="${FOLDER_TO_CHECK}/";

            # Globbing through the folder content
            for FILE_TO_CHECK in "${FOLDER_TO_CHECK}"*;
            do
                [[ -f "${FILE_TO_CHECK}" ]] && [[ -w "${FILE_TO_CHECK}" ]] \
                    &&
                    {
                        read -d ' ' FILE_CKSUM <<< `cksum "${FILE_TO_CHECK}"`;
                        [[ -n "${FILE_CKSUM}" ]] \
                            &&
                            {
                                [[ -n "${FILE_CKSUMS[${FILE_CKSUM}]}" ]] \
                                    &&
                                    {
                                        # Deleting file here
                                        rm "${FILE_TO_CHECK}" \
                                            || echo "Failed to delete file ${FILE_TO_CHECK}";
                                    } \
                                    ||
                                    {
                                        FILE_CKSUMS["${FILE_CKSUM}"]="${FILE_TO_CHECK}";
                                    }
                            }
                    }
            done
        }
done