Bash脚本在同一目录中查找重复的文件名并发送通知电子邮件 [英] Bash Script to find the Duplicate filenames in the same directory and send a Notification email

查看:37
本文介绍了Bash脚本在同一目录中查找重复的文件名并发送通知电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目的是通过比较同一目录中的所有文件名(abc.xyz,def.csv)来查找任何重复的文件名.如果没有重复的文件名,则将提到的文件路径中的所有那些文件(.csv,.xlsx)移到存档"路径中.

My aim is to find for any duplicates file names by comparing all the file names(abc.xyz , def.csv) in the same Directory. if there aren't any duplicate file names then move all those files(.csv , .xlsx) in the mentioned file path into Archive path.

如果文件名重复,则仅使用修改后的日期时间戳获取这些重复文件名,并向团队发送通知电子邮件,并将其余非重复文件名移至存档文件夹.

If there are duplicate filenames, then fetch the names of those duplicate filenames only with their modified date timestamp and send a notification email to the team and move the remaining non-duplicate filenames to the archive folder.

如您所见,我正在尝试通过以下代码来实现它.

As you can see I am trying to achieve it by the following code.

如果find命令为空,则执行if条件并执行'mv'命令并完全退出脚本,如果它们是重复文件,则退出if条件并通过管道传输重复文件并执行邮件和日期戳操作.

if the find command is empty, then perform the if condition and perform 'mv' command and exit the script entirely, if they are duplicate files, then exit the if condition and pipe the duplicate files and perform the mail and date stamp operation.

但是代码的实际作用是,如果发现或找不到任何重复文件,则发送一封通知电子邮件.

However the code what actually doing is, sending a notification email if it finds or does not find any duplicate files.

如果有重复的文件,则发送一封电子邮件,其中包含重复的文件名和修改名称,如果没有重复的filname,则它将发送文件名为空白,将当前时间作为修改时间.

if there are duplicate files, then send an email with duplicate filenames and modification name , if there is no duplicate filnames, then it is sending the filename as blank and current time as modified time.

当前在归档文件之外没有文件(只有归档文件中的文件,但是归档文件中的所有文件都是唯一的并且看起来不错),因此从技术上讲,它不应该发送任何通知电子邮件.

currently there are no files outside archive(only files inside archive, but all the files inside the archive are unique and looks good) so technically it shouldn't send any notification email.

{
DATE=`date +"%Y-%m-%d"`
dirname=/marketsource/SrcFiles/Target_Shellscript_Autodownload/Airtime_Activation
tempfile=myTempfileName
find $dirname -type f  > $tempfile
cat $tempfile | sed 's_.*/__' | sort |  uniq -d|
while read fileName
do
 grep "$fileName" $tempfile
done
}
if ["$fileName" == ""]; then
         mv /marketsource/SrcFiles/Target_Shellscript_Autodownload/Airtime_Activation/*.xlsx /marketsource/SrcFiles/Target_Shellscript_Autodownload/Airtime_Activation/Archive

         mv /marketsource/SrcFiles/Target_Shellscript_Autodownload/Airtime_Activation/*.csv /marketsource/SrcFiles/Target_Shellscript_Autodownload/Airtime_Activation/Archive
        exit 1

fi | tee '/marketsource/scripts/tj_var.txt' | awk -F"/" '{print $NF}'  | tee '/marketsource/scripts/tj_var.txt' | sort -u | tee '/marketsource/scripts/tj_mail.txt'



DATE=`date +"%Y-%m-%d"`
printf "%s\n" "$(</marketsource/scripts/tj_mail.txt)" | while IFS= read -r filename; do
   mtime=$(stat -c %y "/marketsource/SrcFiles/Target_Shellscript_Autodownload/Airtime_Activation/$filename")
   printf  'Duplicate Filename - %s Uploaded time - %s\n\n' "$filename" "$mtime"
done | mail -s "Duplicate file found ${DATE}" ti@gmail.com

推荐答案

通过比较同一目录中的所有文件名(abc.xyz,def.csv)来查找任何重复的文件名.

find for any duplicates file names by comparing all the file names(abc.xyz , def.csv) in the same Directory.

其.xlsx和.csv扩展名

its .xlsx and .csv extensions

我假设文件名中没有空格

I'm assuming there are no whitespaces in filenames

IFS=$'\n'

duplicates=($(
       find . -maxdepth 1 -type f '(' -name '*.xlsx' -o -name '*.csv' ')' \
           -exec bash -c 'printf "%s %s\n" "$1" "${1%.*}"' -- {} \; |
       sort -k1 |
       uniq -f1 -d |
       cut -d' ' -f2
))
# or simpler:
duplicates=($(
    find . -type f '(' -name '*.xlsx' -o -name '*.csv' ')' |
    sed 's/\.[^\.]*$//' |
    sort |
    uniq -d
))

IFS=$' \t\n'

#如果没有重复的文件名,则将提到的文件路径中的所有那些文件(.csv,.xlsx)移到存档路径中

# if there aren't any duplicate file names then move all those files(.csv , .xlsx) in the mentioned file path into Archive path

if ((${#duplicates[@]} == 0)); then
    find . -type f '(' -name '*.xlsx' -o -name '*.csv' ')' \
         -exec mv -v {} "$the_archive_path" \;

#如果文件名重复,则

# If there are duplicate filenames, then

else

#仅使用修改后的日期时间戳获取这些重复文件名的名称

# fetch the names of those duplicate filenames only with their modified date timestamp

    duplicate_filenames_with_modified_date=$(
       {
          printf "%s.xlsx\n" "${duplicates[@]}"
          printf "%s.csv\n" "${duplicates[@]}"
       } |
       xargs -d$'\n' stat -c '%n %y\n'
    )

#并将通知电子邮件发送给团队,然后

# and send a notification email to the team and

    mail the_team <<<"a notification email"

#将剩余的非重复文件名移至存档文件夹.

# move the remaining non-duplicate filenames to the archive folder.

    find . -maxdepth 1 -type f '(' -name '*.xlsx' -o -name '*.csv' ')' \
           -exec bash -c 'echo "$1" "${1%.*}"' -- {} \; | tee /dev/stderr |
       sort -k2 |
       uniq -f1 -u |
       cut -d' ' -f1 |
       xargs -r -d$'\n' -I{} echo mv -v {} "$the_archive_folder"
fi

这篇关于Bash脚本在同一目录中查找重复的文件名并发送通知电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆