Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/sh
- # Is the same as rmContainsAny, except that it:
- # Deletes all files in the current directory that contain ALL of the patterns passed as command line arguments.
- # WARNING: be careful that you do not accidentally delete your work! Avoid this by making hardlinks to your files from another directory.
- # If no arguments are given, display program usage and exit with status 3
- # Deletes all files in the current directory that contain ANY of the patterns passed as command line arguments.
- # Filenames may contain spaces, and patterns may contain spaces.
- # Display a message as each file is deleted.
- # If no files match, exit with status 1
- # If a file cannot be deleted, exit with status 2
- # Otherwise exit with status 0
- files=""
- exitValue=0
- if [ $# -eq 0 ]; then
- echo "Usage: enter regex as a string, separated by spaces. compound regex with \"\"."
- exitValue=3
- else
- #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.
- found=`ls -1 --file-type`
- for regex in "$@"; do
- #cut the values out of found
- files=$(for file in `echo "$found"`; do
- echo $file | grep $regex;
- done)
- found=$files
- done
- files=$found
- if [ -z "$files" ]; then
- exitValue=1
- else
- #sort the file list and uniq it
- for value in `echo "$files"`; do
- echo $value
- done | sort -u | while read line; do
- #and then do whatever we want with it
- rm $line && \
- echo "deleting $line" || \
- exitValue=2
- done
- fi
- fi
- exit $exitValue
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement