简体   繁体   中英

Bash | Copy files to multiple folders and delete them script

I have to copy some files (100 kbs/file) from one folder X to folder Y and B after that I have to delete the files not the folders. Easy until now but folder X will almost continously receive files and I might think that there can be a case when the script will first copy 5 files to X then 8 to Y and delete 10 because there are files coming continously.

I have tested with 2000 files and everything worked great. But I have to be 100% sure that I don't lose any file.

This is my script:

cp -R -v /$RootPath/MAPADMIN/$Client/Inbox/* /$RootPath/$Client/Inbox/ >> log.txt
cp -R -v /$RootPath/MAPADMIN/$Client/Inbox/* /$RootPath/Backup/$Client/Inbox >> log.txt
find /$RootPath/MAPADMIN/$Client/Inbox/ -maxdepth 2 -type f -delete >> log.txt

This script is PERFECT for my situation but I have to be 100% sure that I have 0% lost files. Is there someone that have tested something like this so a longer time or someone who can help me improve it.

Thank you!

I suggest to use in this case rsync it's more reliable.

rsync  -arv /$RootPath/MAPADMIN/$Client/Inbox/ $RootPath/$Client/Inbox 2>&1 >> log.txt
rsync  -arv /$RootPath/MAPADMIN/$Client/Inbox/ /$RootPath/Backup/$Client/Inbox 2>&1 >> log.txt
find /$RootPath/MAPADMIN/$Client/Inbox/ -maxdepth 2 -type f -delete >> log.txt

I updated like this:

files=(*.*)

if [ "$files" != "*.*" ]
then

for file in "${files[@]}"; do

        cp "/$RootPath/$1/Outbox/$folder$file" /$RootPath/MAPADMIN/$1/Outbox/$folder
        mv "/$RootPath/$1/Outbox/$folder$file" /$RootPath/Backup/$1/Outbox/$folder
done
fi

I am searching for every folder into that dir, and then in every folder I am searching for every file and I am going one by one. 3 months left and almost 50,000 files were moved using the script above with 0% loss.

I hope it will be helpful for someone else.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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