Advertisement
soldier9599

rm all regex files in system

Dec 19th, 2014
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.14 KB | None | 0 0
  1. # WARNING: This script will attempt to delete all files containing the
  2. # string given to it as an argument. Do not run unless you know what
  3. # you're doing. You should have already located every file with this
  4. # string and decided that it can be deleted. This can easily break your
  5. # system, especially if run as root. It creates a backup at ~/rmbackup.
  6. # If run as root, that is /root/rmbackup
  7. #
  8. # There is probably a more concise way to do this with only one loop, but
  9. # I didn't feel like figuring out how to fix the directory structure
  10. # problems for the backup when you combine the loops as is.
  11. #
  12. # sh choosename.sh [regexp]
  13.  
  14. mkdir $HOME/rmbackup
  15. sudo updatedb
  16. while read line; do
  17.     directorystate=$(cd $line 2>&1 >/dev/null | grep "Not a directory")
  18.     if [[ $directorystate ]]; then
  19.         mkdir -p $HOME/rmbackup$line
  20.         rm -rf $HOME/rmbackup$line
  21.         rsync -raAX $line $HOME/rmbackup$line
  22.     else
  23.         mkdir -p $HOME/rmbackup$line
  24.     fi
  25. done <<< "$(locate $@)"
  26. while read line; do
  27.     directorystate=$(cd $line 2>&1 >/dev/null | grep "Not a directory")
  28.     if [[ $directorystate ]]; then
  29.         rm $line
  30.     else
  31.         rm -rf $line
  32.     fi
  33. done <<< "$(locate $@)"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement