Advertisement
Nattack

Untitled

Feb 28th, 2018
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.71 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. # Is the same as rmContainsAny, except that it:
  4. # Deletes all files in the current directory that contain ALL of the patterns passed as command line arguments.
  5.  
  6. # WARNING: be careful that you do not accidentally delete your work! Avoid this by making hardlinks to your files from another directory.
  7. # If no arguments are given, display program usage and exit with status 3
  8. # Deletes all files in the current directory that contain ANY of the patterns passed as command line arguments.
  9. # Filenames may contain spaces, and patterns may contain spaces.
  10. # Display a message as each file is deleted.
  11. # If no files match, exit with status 1
  12. # If a file cannot be deleted, exit with status 2
  13. # Otherwise exit with status 0
  14.  
  15. files=""
  16. exitValue=0
  17.  
  18. if [ $# -eq 0 ]; then
  19.     echo "Usage: enter regex as a string, separated by spaces. compound regex with \"\"."
  20.     exitValue=3
  21. else
  22.     #ls -1 doesnt seem to work here because strings apparently don't support newlines. I'm sure there's some good reason for that, but it's pretty dumb for everything I'm trying to do.
  23.     found=`ls -1 --file-type`
  24.     for regex in "$@"; do
  25.         #cut the values out of found
  26.         files=$(for file in `echo "$found"`; do
  27.             echo $file | grep $regex;
  28.         done)
  29.         found=$files
  30.     done
  31.     files=$found
  32.    
  33.     if [ -z "$files" ]; then
  34.         exitValue=1
  35.     else
  36.         #sort the file list and uniq it
  37.         for value in `echo "$files"`; do
  38.             echo $value
  39.         done | sort -u | while read line; do
  40.             #and then do whatever we want with it
  41.             rm $line && \
  42.             echo "deleting $line" || \
  43.             exitValue=2
  44.         done
  45.     fi
  46. fi
  47.  
  48. exit $exitValue
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement