-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdirb.sh
186 lines (171 loc) · 5.75 KB
/
dirb.sh
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# Directory Bookmarks for BASH (c) 2009-2010 Ira Chayut, Version 101216
#
# To use, save this file as ~/.bashDirB and add the following line to ~/.bashrc:
#
# source ~/.bashDirB
#
# DirB and its implementation in this file are the product of and
# copyrighted by Ira Chayut. You are granted a non-exclusive, royalty-free
# license to use, reporduce, modify and create derivative works from DirB;
# providing that credit is given to DirB as a source material.
#
# The lastest version is available from: http://www.dirb.info/. Ira can
# be reached at [email protected].
#
# Remember old saved bookmarks as DB_<bookmark>:
# Thanks for the suggestion to Jason Priebe ([email protected])
[ -e ~/.bashDirB_envvars ] && source ~/.bashDirB_envvars
# If the repository of bookmarks does not exist, create it
[ ! -e ~/.DirB ] && mkdir ~/.DirB
function h() { # Thanks to Manuel Soriano [email protected]
cat <<\EOF
OPERATIONS
s Save a directory bookmark
g go to a bookmark or named directory
p push a bookmark/directory onto the dir stack
r remove saved bookmark
d display bookmarked directory path
sl print the list of directory bookmarks
EXAMPLES
s xyz current directory is saved as xyz
s xyz ../../dir0/dir1 save relative directory as xyz
s xyz dir notes here save directory with comments
g xyz go to bookmark xyz or to path xyz
p xyz go to bookmark/directory xyz
and remember into the directory stack
p swap to two bookmarks
p +n rotate nth entry from top to top
p -n rotate nth entry from bottm to top
r xyz remove named bookmark
d xyz display directory name of bookmark xyz
sl -l long list
sl -p path list
sl "*x" list all of all bookmarks *x
sl -l "*x" long list all of all bookmarks *x
EOF
}
# "s" - Save bookmark
function s() {
# build the bookmark file with the contents "$CD directory_path"
if [ -n "$2" ]; then
( echo '$CD ' \"$2\" > ~/.DirB/"$1" ;) > /dev/null 2>&1
else
( echo -n '$CD ' > ~/.DirB/"$1" ;
pwd \
| sed "s/ /\\\\ /g" >> ~/.DirB/"$1" ; ) > /dev/null 2>&1
fi
# if the bookmark could not be created, print an error message and
# exit with a failing return code
if [ $? != 0 ]; then
echo >&2 "bash: DirB: ~/.DirB/$1 could not be created"
false
fi
echo export db_$1=\'$(d "$1")\' >> ~/.bashDirB_envvars
. ~/.bashDirB_envvars
NAME="$1"
shift; shift
if [ -n "$1" ]; then
( echo "echo $*" >> ~/.DirB/"$NAME" ;) > /dev/null 2>&1
fi
}
# "g" - Go to bookmark
function g() {
# if no arguments, then just go to the home directory
if [ -z "$1" ]; then
cd
else
# if $1 is in ~/.DirB and does not begin with ".", then go to it
# update the bookmark's timestamp and then execute it
# else just do a "cd" to the argument, usually a directory path of "-"
if [ -f ~/.DirB/"$1" -a ${1:0:1} != "." ]; then
touch ~/.DirB/"$1" ;
CD=cd source ~/.DirB/"$1" ;
else
cd "$1"
fi
fi
}
# "p" - Push a bookmark
# Note, the author's preference is to list the directory stack in a single
# column. Thus, the standard behavior of "pushd" and "popd" have been
# replaced by discarding the normal output of these commands and using a
# "dirs -p" after each one.
function p() {
# if no argument given, then just pushd and print out the directory stack
# if $1 is a dash, then just do a "popd" and print out the directory stack
if [ -z "$1" ]; then
pushd > /dev/null && dirs -p
elif [ "$1" == "-" ]; then
popd > /dev/null
dirs -p
else
# if $1 is in ~/.DirB and does not begin with ".", then go to it
# and then print out the directory stack
# else just do a "pushd" and print out the directory stack
if [ -f ~/.DirB/"$1" -a "${1:0:1}" != "." ]; then
touch ~/.DirB/$1 ;
CD=pushd source ~/.DirB/$1 > /dev/null \
&& dirs -p ;
else
pushd "$1" > /dev/null && dirs -p
fi
fi
}
# "sl" - Saved bookmark Listing
# if the "-l" argument is given, then do a long listing, passing any
# remaining arguments to "ls", printing in reverse time order. Pass the
# output to "less" to page the output if longer than a screen in length.
#
# if the "-p" argument is given, list the directory bookmarks and the paths
# that they point to. (Thanks to Francis Hulin-Hubard [email protected])
function sl() {
if [ "$1" == "-l" ]; then
shift
( cd ~/.DirB ;
ls -lt -- $* 2>/dev/null \
| sed -e 's/ */ /g' \
-e '/^total/d' \
-e 's/^\(... \)\([0-9] \)/\1 \2/' \
| cut -d ' ' -s -f6- \
| sed -e '/ [0-9] /s// &/' \
| less -FX ; \
)
elif [ "$1" == "-p" ]; then
shift
( cd ~/.DirB ; \
for i in $(ls -- $* 2>/dev/null); do echo "$i: $(d $i)"; done \
) \
| less -FX
else
( cd ~/.DirB ; ls -xt $* ; )
fi
}
# "r" - Remove a saved bookmark
function r() {
# if the bookmark file exists, remove it
# if the bookmark file does not exist, complain and exit with a failing code
if [ -e ~/.DirB/"$1" ]; then
rm ~/.DirB/"$1"
sed \
-i_bak ~/.bashDirB_envvars \
-e "/export db_$1=/d" > /dev/null 2>&1
else
echo >&2 "bash: DirB: ~/.DirB/$1 does not exist"
false
fi
}
# "d" - Display (or Dereference) a saved bookmark
# to use: cd "$(d xxx)"
function d() {
# if the bookmark exists, then extract its directory path and print it
# if the bookmark does not exists, complain and exit with a failing code
if [ -e ~/.DirB/"$1" ]; then
head -1 ~/.DirB/"$1" \
| sed \
-e 's/\$CD //' \
-e 's/\\//g'
else
echo >&2 "bash: DirB: ~/.DirB/$1 does not exist"
false
fi
}