/
mvdl
executable file
·162 lines (153 loc) · 5.14 KB
/
mvdl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!/bin/bash
function helptext() {
cat <<EOF
# ------------------------------------------------------------------
#
# Move most recent file in pre-defined folder to the current folder
#
# I made this program so I could move my just-downloaded file
# into my current working directory.
#
# Usage:
#
# ${PROGNAME} [ARGUMENT]
#
# Arguments:
#
# help, -?, --help - Display this help text.
# undo, -u, --undo - Undo last action performed by ${PROGNAME}
# history, -h, --history - Display the ${PROGNAME} history
#
# External programs req'd:
# bash
#
# Author: Joe Miragliuolo
#
# ------------------------------------------------------------------
EOF
} # end of helptext
# TO DO:
# - handle if file already exists at target? (maybe a "mv --interactive" call
# or something?)
# - maybe make it so you have to type a destination, so that executing it
# without args cause a usage message? (SAFER!) (don't forget that if you
# do this, you'll need to change the arg handling to deal with just a
# "-w" arg or something for the "what file?" option.)
# Constants
declare -r PROGNAME=$(basename $0)
# Variables
MVDL_HIST_FLAG=1 # 0 = don't keep history, 1 = keep history
MVDL_HIST_DIR=$HOME # Where to keep the history file
MVDL_HIST_LINES=10 # Default number of history items saved
MVDL_HIST_FILE=.mvdl_history # Default filename of history file
DLFILEPATH=$HOME/Downloads
# ------------------------------------------------------------------
# Program starts here
# ------------------------------------------------------------------
RECENTDLFILE="$(ls -pct $DLFILEPATH/ | grep -v /$ | head -n1)"
if [ $# -eq 0 ]; then
# if no params, then start the moving process...
if [ -z "$RECENTDLFILE" ]; then
echo "ERROR: There does not appear to be any file to move in $DLFILEPATH/"
exit 1
fi
echo "\"$RECENTDLFILE\" is the most recent file in $DLFILEPATH/"
echo -n "Move to this directory? [y/N] "
read -s -n 1 a
if [[ $a != [Yy] ]]; then
echo "n"
echo "Not going to move file."
exit 1
else
echo "y"
if [ -f "./$RECENTDLFILE" ]; then
echo "ERROR: No move occurred. A file by that name already exists in current dir."
exit 1
else
echo "Moving $RECENTDLFILE to this directory."
#echo "mv \"$DLFILEPATH/$RECENTDLFILE\" ." # DEBUG
mv "$DLFILEPATH/$RECENTDLFILE" .
if [ $MVDL_HIST_FLAG -eq 1 ]; then
if [ -f "$MVDL_HIST_DIR/$MVDL_HIST_FILE" ]; then
# Save last section of message log file.
tail -n $MVDL_HIST_LINES "$MVDL_HIST_DIR/$MVDL_HIST_FILE" > "$MVDL_HIST_DIR/$MVDL_HIST_FILE.temp"
# Becomes new log directory.
mv $MVDL_HIST_DIR/$MVDL_HIST_FILE.temp $MVDL_HIST_DIR/$MVDL_HIST_FILE
fi
echo -e "$(date +%s)\t$DLFILEPATH/$RECENTDLFILE\t=>\t$PWD/$RECENTDLFILE" >> "$MVDL_HIST_DIR/$MVDL_HIST_FILE"
fi
exit
fi
fi
else
case $1 in
-\?|--help|help)
# otherwise, if the param is --help, then
# show the helptext and exit nicely
helptext
exit
;;
-u|--undo|undo)
# otherwise, if the param is -u, then
# undo last mv and exit nicely
#undo
echo "Undo last move:"
FROMFILE=$(tail -1 "$MVDL_HIST_DIR/$MVDL_HIST_FILE" | awk '{print $2}') # where it came from
TOFILE=$(tail -1 "$MVDL_HIST_DIR/$MVDL_HIST_FILE" | awk '{print $4}') # where it was moved to
echo "\"$(basename $FROMFILE)\" was moved to \"$(dirname $TOFILE)/\""
echo -n "Would you like to move it back to \"$(dirname $FROMFILE)\"? [Y/n] "
read -s -n 1 a
case $a in
n|N)
# n and N do no
echo "n"
echo "Not going to move file."
exit 1
;;
y|Y|"")
# y Y and <enter> to do yes
echo "y"
if [ -f "$TOFILE" ]; then # file exists
if [ -f "$FROMFILE" ]; then # file already exists in the dir we're going to
echo "ERROR: a file by that name already exists in the dir we're moving it to."
exit 1
else
#move it
#echo "mv \"$TOFILE\" \"$(dirname $FROMFILE)\"" # DEBUG
echo "Moving file back."
mv "$TOFILE" "$(dirname $FROMFILE)"
exit
fi
else
echo "ERROR: file no longer exists where mvdl put it."
exit 1
fi
exit
;;
*)
# everything else doesn't do anything (effectively a no)
echo ""
echo "Not a valid answer. Not going to move file."
exit 1
;;
esac
#echo "mv \"$TOFILE\" \"$(dirname $FROMFILE)\""
;;
-h|--history|history)
# otherwise, if the param is -h, then
# show the history and exit nicely
echo "Show the history:"
if [ -f "$MVDL_HIST_DIR/$MVDL_HIST_FILE" ]; then
cat "$MVDL_HIST_DIR/$MVDL_HIST_FILE"
fi
exit
;;
*)
# invalid argument
echo "ERROR: Invalid argument. Run \"mvdl --help\" to see possible arguments."
exit 1
;;
esac
fi
exit
# end of script