Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # author: markus tornow
- # date: Fri Jul 9 23:44:50 CEST 2010
- # name: move_old
- # path: ~/bin
- #### #### #### ####
- # usage: $0 *<file-ending-pattern_0> *<file-ending-pattern_n>
- # Don't forget the * in front of the file-endings you add :-=
- clear; date; echo
- # VARIABLES
- ###################
- EXCLUDE="$0"
- OLD_DIR=old-$(date +%y-%m-%d)
- OLD="old"
- INDEX=0
- # Check if all parameters are given
- # minimum is one in the form of *<pattern>
- ###########################################
- if [ "$#" -eq 0 ]
- then
- echo "
- Usage is: "$0" *<pattern_0> *<pattern_n>
- With pattern being what you want to move to old
- "
- fi
- ####################################################
- # Check for a backup directory, called old_timestamp
- # Avoid an error by omitting its creation in case it
- # exists
- # VARs
- #####################################################
- if [[ -d $OLD/$OLD_DIR ]]
- then
- mv "$OLD/$OLD_DIR" "$OLD/$OLD_DIR-$INDEX"
- ((INDEX+=1))
- mkdir "$OLD_DIR"
- else
- mkdir "$OLD_DIR"
- fi
- if ! [[ -d $OLD ]]
- then
- mkdir "$OLD"
- fi
- ###########################################
- # Create a FUNKTION,
- # to double-check with a dry run (with echo)
- # if all is cool
- ############################################
- function ask_for {
- echo -n "
- do you want to move on (yes or no): "
- read answer
- if [[ $answer = yes ]] || [[ $answer = y ]]
- then
- echo "
- ok
- "
- else
- echo "
- check whats wrong
- "
- exit 1
- fi
- }
- ####################
- # dry run
- ####################
- for pattern in "$@"
- do
- if ! [[ $pattern =~ $EXCLUDE ]] || ! [[ $pattern = OLD ]]
- then
- echo "$pattern"
- fi
- done
- ask_for
- # in case user answer no
- # nothing will be moved
- #####################
- # The final command
- #####################
- for pattern in "$@"
- do
- if ! [[ $pattern =~ $EXCLUDE ]]
- then
- mv $pattern $OLD_DIR
- fi
- done
- mv $OLD_DIR $OLD
- exit 0
Add Comment
Please, Sign In to add comment