Bash脚本用于查找列表中的文件,将它们复制到dest,打印未找到的文件

时间:2013-04-15 22:02:57

标签: linux bash find

我想建立在此处找到的答案:Bash script to find specific files in a hierarchy of files

find $dir -name $name -exec scp {} $destination \;

我有一个包含文件名列表的文件,我需要在备份磁盘上找到这些文件,然后将找到的文件复制到目标文件夹,最后将无法找到的文件打印到新文件中。

最后一步会有所帮助,这样我就不需要复制另一个文件列表,然后与原始列表进行比较。

如果脚本可以制作复制文件的列表,并进行比较,则打印差异,然后这就是所需要的。除非shell进程发现每次“无法找到”文件时都可以打印到文件。

2 个答案:

答案 0 :(得分:2)

假设您的列表由换行符分隔;这样的事情应该有效

#!/bin/bash

dir=someWhere
dest=someWhereElse
toCopyList=filesomewhere
notCopied=filesomewhereElse

while read line; do
   find "$dir" -name "$line" -exec cp '{}' $dest \; -printf "%f\n"     
done < "$toCopyList" > cpList

#sed -i 's#'$dir'/##' cpList
# I used # instead of / in sed to not confuse sed with / in $dir
# Also, I assumed the string in $dir doesnot end with a /

cat cpList "$toCopyList" | sort | uniq -c | sed -nr '/^ +1/s/^ +1 +(.*)/\1/p' > "$notCopied"
# Will not work if you give wild cards in your "toCopyList"

希望有所帮助

答案 1 :(得分:0)

while read fname ; do
  find /FROM/WHERE/TO/COPY/ \
       -type f \
       -name "$fname" \
       -exec cp \{\} /DESTINATION/DIR/ \; 2>/dev/null 
  find /DESTINATION/DIR/ \
       -type f \
       -name "$fname" &>/dev/null || \
       echo $fname
done < FILESTOCOPY > MISSEDFILES

会这样做。