Home > database >  How do I count files that deleted?
How do I count files that deleted?

Time:12-24

I want to make below script to count files that deleted. I tried a lot of methods with variables, but I didn't make something good. I need some help from users here.

Can anynone help to do?

Here is code:

#!/usr/bin/env bash


user="username"



    if find /home/$user/.config/chromium &>/dev/null ; then 

        #Cookies
        if find /home/$user/.config/chromium/Default/ &>/dev/null -type f -name "Cookies" ; then
            find /home/$user/.config/chromium/Default/ -type f -name "Cookies" -exec rm -vf {} \;
    
        fi

        find /home/$user/.config/chromium/Default/ -type f -name "Cookies-journal" -exec rm -vf {} \; 
        find /home/$user/.config/chromium/Default -type f -name "QuotaManager" -exec rm -vf {} \; 
        find /home/$user/.config/chromium/Default -type f -name "QuotaManager-journal" -exec rm -vf {} \; 
        find /home/$user/.config/chromium/Default -type f -name "Preferences" -exec rm -vf {} \; 
        find /home/$user/.config/chromium/Default/Local*Storage/leveldb -type f -name "*" -exec rm -vf {} \;
        #Form history
        find /home/$user/.config/chromium/Default -type f -name "Web Data" -exec rm -vf {} \;
        #History
        find /home/$user/.config/chromium/Default -type f -name "History" -exec rm -vf {} \;
        find /home/$user/.config/chromium/Default -type f -name "Favicons" -exec rm -vf {} \;
        find /home/$user/.config/chromium -type f -name "chrome_shutdown_ms.txt" -exec rm -vf {} \; 
        find /home/$user/.config/chromium/Default -type f -name "History-journal" -exec rm -vf {} \; 
        find /home/$user/.config/chromium/Default -type f -name "History Provider Cache" -exec rm -vf {} \; 
        find /home/$user/.config/chromium/Default -type f -name "Network Action Predictor" -exec rm -vf {} \; 
        find /home/$user/.config/chromium/Default -type f -name "Network Action Predictor-journal" -exec rm -vf {} \; 
        find /home/$user/.config/chromium/Default -type f -name "Shortcuts" -exec rm -vf {} \; 
        find /home/$user/.config/chromium/Default -type f -name "Shortcuts-journal" -exec rm -vf {} \; 
        find /home/$user/.config/chromium/Default -type f -name "Top Sites" -exec rm -vf {} \;
        find /home/$user/.config/chromium/Default -type f -name "Top Sites-journal" -exec rm -vf {} \; 
        find /home/$user/.config/chromium/Default -type f -name "Visited Links" -exec rm -vf {} \; 
        find /home/$user/.config/chromium/Default -type f -name "QuotaManager" -exec rm -vf {} \; 
        find /home/$user/.config/chromium/Default -type f -name "QuotaManager-journal" -exec rm -vf {} \;
        find /home/$user/.config/chromium/Default/Session*Storage -type f -name "*" -exec rm -vf {} \; 
        
        find /home/$user/.config/chromium/Default -type f -name "Preferences" -exec rm -vf {} \;
        
        find /home/$user/.config/chromium/Default -type f -name "DownloadMetadata" -exec rm -vf {} \;

    fi

CodePudding user response:

Just print the count of how many files you're going to remove before you remove them:

#!/usr/bin/env bash
readarray -td '' files < <(find whatever -print0)
printf 'Removing %d files\n' "${#files[@]}"
rm -f "${files[@]}"

Just change find whatever -print0 to whatever command you like that'll output a list of NUL-separated files.

Here's a complete script using the above:

$ cat tst.sh
#!/usr/bin/env bash

readarray -td '' files < <(
    find . -type f -name 'file_[0-9].txt' -print0
    find . -type f -name 'foo*' -print0
    find . -type f -name 'bar*' -print0
)

printf 'Removing %d files:\n' "${#files[@]}"
printf '\t%s\n' "${files[@]}"

read -p 'OK? (Y/N) ' rsp
[[ "$rsp" =~ [yY] ]] && rm -f "${files[@]}"

For example:

$ ls file_[0-9]* foo* bar*
bar.txt  file_1.txt  file_2.txt  file_3.txt  foo.txt

$ ./tst.sh
Removing 5 files:
        ./file_1.txt
        ./file_2.txt
        ./file_3.txt
        ./foo.txt
        ./bar.txt
OK? (Y/N) y

$ ls file_[0-9]* foo* bar*
ls: cannot access 'file_[0-9]*': No such file or directory
ls: cannot access 'foo*': No such file or directory
ls: cannot access 'bar*': No such file or directory

CodePudding user response:

Just a sample, but you might want to go that way:

for i in "QuotaManager" "QuotaManager-journal" "Preferences" # [ ... ]
do
        echo "$i: removed $( find /home/$user/.config/chromium/Default/ -type f -name "$i" -exec rm -vf {} \; | wc -l ) file(s)"
done
  •  Tags:  
  • bash
  • Related