简体   繁体   English

检查文件是否存在于给定目录中,而不是子目录中

[英]Check if a file exists in a given directory, NOT subdirectories

I have written a shell-script that basically looks for rar files and unrars them. 我写了一个shell脚本,该脚本基本上查找rar文件并将其解压缩。

To avoid unraring the same file multiple times I added a check for the type of files I'll be unraring, namely *.avi, *.mkv and *.mp4. 为了避免多次解压缩同一文件,我添加了将要解压缩的文件类型的检查,即* .avi,*。mkv和* .mp4。

find "/mnt/media1/TV Shows/" -type f -name "*.rar" -print0 | while IFS= read -r -d '' file; do
    DIR=$(dirname "${file}")
    MKV=$(find "${DIR}" -name "*.mkv" -print0)
    AVI=$(find "${DIR}" -name "*.avi" -print0)
    MP4=$(find "${DIR}" -name "*.mp4" -print0)
    if [ ! -f "$MKV" ] && [ ! -f "$AVI" ] && [ ! -f "$MP4" ]; then
            printf "UNRARING ${DIR}\n" >> /home/johan/unrarer.log
            sudo unrar e "${file}"
            MKV=$(find "${DIR}" -name "*.mkv" -print0)
            AVI=$(find "${DIR}" -name "*.avi" -print0)
            MP4=$(find "${DIR}" -name "*.mp4" -print0)
            if [ ! -f "$MKV" ] && [ ! -f "$AVI" ] && [ ! -f "$MP4" ]; then
                    printf "UNRAR FAILED\n" >> /home/johan/unrarer.log
            else
                    printf "UNRAR SUCCESSFUL\n" >> /home/johan/unrarer.log
            fi
    else
            printf "${DIR}: Found an mkv, avi or mp4 file, skipping\n" >> /home/johan/unrarer.log
    fi
done

Now, the problem is that most, if not all, directories have a "Sample" subdirectory with a video file in it. 现在的问题是,大多数(如果不是全部)目录都有一个“ Sample”子目录,其中包含视频文件。 This file gets flagged with my current implementation, which is not what I desire. 该文件被标记为当前的实现,这不是我想要的。

I want it to look in the exact directory, $DIR , not any of its subdirectories. 我希望它查看确切的目录$DIR ,而不是其任何子目录。

Currently I have made a workaround where I simply delete the Sample directory if it exists, but this breaks torrents. 目前,我已经采取了一种解决方法,如果存在Sample目录,我将其简单地删除,但这会破坏种子。

I have tried experimenting a bit with -wholename but I haven't found anything that works. 我曾尝试使用-wholename进行实验,但没有找到任何有效的方法。

Any help appreciated. 任何帮助表示赞赏。

Regards, Johan 问候,约翰

Use the -maxdepth option to limit the find to the current directory. 使用-maxdepth选项可以将查找限制为当前目录。

MKV=$(find "${DIR}" -name "*.mkv" -print0 -maxdepth 0)
AVI=$(find "${DIR}" -name "*.avi" -print0 -maxdepth 0)
MP4=$(find "${DIR}" -name "*.mp4" -print0 -maxdepth 0)

charlton_austin 's answer held the answer. charlton_austin的答案为答案。 However it was -maxdepth 1. 但是它是-maxdepth 1。

Complete script: 完整的脚本:

find "/mnt/media1/TV Shows/" -type f -name "*.rar" -print0 | while IFS= read -r -d '' file; do
    DIR=$(dirname "${file}")
    cd "${DIR}"
    MKV=$(find . -maxdepth 1 -name "*.mkv" -print0)
    AVI=$(find . -maxdepth 1 -name "*.avi" -print0)
    MP4=$(find . -maxdepth 1 -name "*.mp4" -print0)
    if [ ! -f "$MKV" ] && [ ! -f "$AVI" ] && [ ! -f "$MP4" ]; then
            printf "UNRARING ${DIR}\n" >> /home/johan/unrarer.log
            sudo unrar e "${file}"
            MKV=$(find . -maxdepth 1 -name "*.mkv" -print0)
            AVI=$(find . -maxdepth 1 -name "*.avi" -print0)
            MP4=$(find . -maxdepth 1 -name "*.mp4" -print0)
            if [ ! -f "$MKV" ] && [ ! -f "$AVI" ] && [ ! -f "$MP4" ]; then
                    printf "UNRAR FAILED\n" >> /home/johan/unrarer.log
            else
                    printf "UNRAR SUCCESSFUL\n" >> /home/johan/unrarer.log
            fi
    else
            printf "${DIR}: Found an mkv, avi or mp4 file, skipping\n" >> /home/johan/unrarer.log
    fi
done

You can take advances of globbing: 您可以提前进行遍历:

if [[ "$(ls ./*.{mkv,avi,mp4} 2> /dev/null)" != "" ]]; then
    echo "exists"
else
    echo "fail"
fi

find isn't necessary here, since you are only looking for files with a given extension in the specified directory. 在这里不必find ,因为您只在指定目录中查找具有给定扩展名的文件。 Just fill an array using the pattern. 只需使用模式填充数组。 It doesn't appear that you need to keep files of different types separate; 似乎不需要将不同类型的文件分开; you are just checking that something got unarchived. 你只是检查了东西未存档。

cd "$DIR"
files=( "$DIR"/*.mkv "$DIR"/*.avi "$DIR"/*.mp4 )
if (( ${#files[@]} == 0 )); then
    printf "UNRARING $DIR\n" >> /home/johan/unrarer.log
    sudo unrar e "$file"
    files=( "$DIR"/*.mkv "$DIR"/*.avi "$DIR"/*.mp4 )
    if (( ${#files[@]} == 0 )); then
        printf "UNRAR FAILED\n" >> /home/johan/unrarer.log
    else
        printf "UNRAR SUCCESSFUL\n" >> /home/johan/unrarer.log
    fi
else
    printf "$DIR: Found an mov, avi, or mp4 file, skipping\n" >> /home/johan/unrarer.log
fi

If you are using bash 4 or later, you don't need to use find to find the rar files either: 如果您使用的是bash 4或更高版本,则无需使用find查找rar文件:

shopt -s globstar
for file in "/mnt/media1/TV Shows/"**/*.rar; do
    ...
done

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM