简体   繁体   English

Bash脚本在列表中查找文件,将其复制到dest,未找到打印文件

[英]Bash script to find files in a list, copy them to dest, print files not found

I would like to build on the answer I found here: Bash script to find specific files in a hierarchy of files 我想以在这里找到的答案为基础: Bash脚本,用于在文件层次结构中查找特定文件

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

I have a file with a list of file names and I need to find those files on a backup disk, then copy those files found to a destination folder, and lastly print the files that could not be found to a new file. 我有一个带有文件名列表的文件,我需要在备份磁盘上找到这些文件,然后将找到的那些文件复制到目标文件夹,最后将找不到的文件打印到新文件中。

the last step would be helpful so that I wouldn't need to make another list of files copied and then do a compare with original list. 最后一步会有所帮助,这样我就不需要再复制一份文件列表,然后与原始列表进行比较。

If the script can then make a list of the copied files, and do a compare, then print the differences, then that's exactly what's required. 如果脚本随后可以列出已复制文件并进行比较,然后打印差异,那么这正是所需要的。 Unless the shell process find can print to file each time it "Can't find" a file. 除非shell进程find在每次“找不到”文件时都可以打印到文件。

Assuming that your list is separated by newlines; 假设您的列表用换行符隔开; something like this should work 这样的事情应该工作

#!/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"

Hope it helps 希望能帮助到你

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

Will do. 会做。

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

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