| 1 | #!/bin/bash |
| 2 | # badname.sh |
| 3 | # Delete filenames in current directory containing bad characters. |
| 4 | |
| 5 | for filename in * |
| 6 | do |
| 7 | badname=`echo "$filename" | sed -n /[\+\{\;\"\\\=\?~\(\)\<\>\&\*\|\$]/p` |
| 8 | # badname=`echo "$filename" | sed -n '/[+{;"\=?~()<>&*|$]/p'` also works. |
| 9 | # Deletes files containing these nasties: + { ; " \ = ? ~ ( ) < > & * | $ |
| 10 | # |
| 11 | rm $badname 2>/dev/null |
| 12 | # ^^^^^^^^^^^ Error messages deep-sixed. |
| 13 | done |
| 14 | |
| 15 | # Now, take care of files containing all manner of whitespace. |
| 16 | find . -name "* *" -exec rm -f {} \; |
| 17 | # The path name of the file that _find_ finds replaces the "{}". |
| 18 | # The '\' ensures that the ';' is interpreted literally, as end of command. |
| 19 | |
| 20 | exit 0 |
| 21 | |
| 22 | #--------------------------------------------------------------------- |
| 23 | # Commands below this line will not execute because of _exit_ command. |
| 24 | |
| 25 | # An alternative to the above script: |
| 26 | find . -name '*[+{;"\\=?~()<>&*|$ ]*' -maxdepth 0 \ |
| 27 | -exec rm -f '{}' \; |
| 28 | # The "-maxdepth 0" option ensures that _find_ will not search |
| 29 | #+ subdirectories below $PWD. |
| 30 | |
| 31 | # (Thanks, S.C.) |