简体   繁体   English

Shell脚本:检查列表文件中目录中是否存在文件

[英]Shell scripting: Check if file exists in directory from a list file

I am trying to check if file names listed in a text file exist in a directory. 我正在尝试检查目录中是否存在文本文件中列出的文件名。 If the file name in medialist.txt exists in the medialab directory put the file name in foundfiles.txt otherwise put the file name in lostfiles.txt. 如果medialab目录中存在medialist.txt中的文件名,则将该文件名存储在foundfiles.txt中,否则将该文件名存储在lostfiles.txt中。 Where I am having difficulty is passing a single file name from medialist.txt into the if statement. 我遇到困难的地方是将一个文件名从medialist.txt传递到if语句中。 How would I pass a single file name from medialist.txt to the if statement? 如何将单个文件名从medialist.txt传递给if语句?

The following block of code is what I have come up with so far. 到目前为止,下面的代码块是我提出的。

for i in $(cat /home/user/Downloads/medialab/medialist.txt)
do
   if [[ $([[ -e $i ]]) == $(ls /home/user/Downloads/medialab/)  ]]
   then
           echo "$i" > /home/user/foundfiles.txt
   else
           echo "$i" > /home/user/lostfiles.txt
   fi
done

I have experimented with the xargs command in the if statement. 我已经在if语句中尝试了xargs命令。 This would replace the current if statement in the for loop above. 这将替换上面的for循环中的当前if语句。 I would remove the LS variable, it is just for testing the if statement on its own. 我将删除LS变量,它仅用于测试if语句。 Here is the example: 这是示例:

LS=$(ls /home/user/Downloads/medialab/)

if [[ -e $(xargs echo "$LS") ]]
then    
    echo "$i" > /home/user/foundfiles.txt
else    
    echo "$i" > /home/user/lostfiles.txt
fi

You can just read the file in a while-loop and check if the path is a file with the -f (checks if the given path is a file) switch, like so: 您可以仅在while循环中读取文件,然后使用-f (检查给定路径是否为文件)开关来检查路径是否为文件,如下所示:

while read -r || [[ -n $REPLY ]]; do
    [[ -z $REPLY ]] && continue
    if [[ -f "/home/user/Downloads/medialab/${REPLY}" ]]; then
        echo "$REPLY" >> "/home/user/foundfiles.txt"
    else
        echo "$REPLY" >> "/home/user/lostfiles.txt"
    fi
done < "/home/user/Downloads/medialab/medialist.txt"

What's happening here is we're reading the inputfile with a redirection to the while loop, using read to read the input into a variable using the inbuilt $REPLY variable, then the || [[ -n $REPLY ]] 这里发生的是我们正在通过重定向到while循环来读取输入文件,使用read使用内置的$REPLY变量将输入读入变量,然后使用|| [[ -n $REPLY ]] || [[ -n $REPLY ]] handles an inputfile that doesn't end with a newline. || [[ -n $REPLY ]]处理不以换行符结尾的输入文件。

Next we check if the variable is empty with -z , this handles empty lines in the middle of the file skipping them with continue . 接下来,我们使用-z检查变量是否为空,这将处理文件中间的空行,并continue跳过它们。

Then we check if the string in the now populated $REPLY variable is a file with -f (expanding the filename as a part of the path in quotes) and append the appropriate file with >> . 然后,我们检查现在填充的$REPLY变量中的字符串是否是带有-f的文件(将文件名扩展为引号中路径的一部分),并在适当的文件后附加>>

Note how using [[ as the test, you don't need to quote the LHS variable, but you do need to quote a RHS variable (as well as variables in pretty much any other use). 注意如何使用[[作为测试,您不需要引用LHS变量,但是需要引用RHS变量(以及几乎所有其他用途的变量)。

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

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