-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbashDirB.symlink
147 lines (132 loc) · 4.46 KB
/
bashDirB.symlink
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
# Directory Bookmarks for BASH (c) 2009, Ira Chayut, Version 090927
#
# 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, reproduce, 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/bashDirB. Ira can
# be reached at [email protected].
#
# By default DirB will have the shell echo the current working directory out
# to the title bars of Xterm windows. To disable this behavior, comment
# out the next line.
#PS1="\[\033]0;\w\007\]\t \!> "
# If the repository of bookmarks does not exist, create it
if [ ! -e ~/.DirB ]
then
mkdir ~/.DirB
fi
# "s" - Save bookmark
function s () {
if [ -n "$2" ]
then
# build the bookmark file with the contents "$CD directory_path"
( echo '$CD ' \"$2\" > ~/.DirB/"$1" ;) > /dev/null 2>&1
else
# build the bookmark file with the contents "$CD directory_path"
( 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 bash: DirB: ~/.DirB/"$1" could not be created >&2
false
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
if [ -f ~/.DirB/"$1" -a ${1:0:1} != "." ]
then
# update the bookmark's timestamp and then execute it
touch ~/.DirB/"$1" ;
CD=cd source ~/.DirB/"$1" ;
# else just do a "cd" to the argument, usually a directory path of "-"
else
cd "$1"
fi
fi
}
# "p" - Push a bookmark
function p () {
# 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.
# if no argument given, then just pushd and print out the directory stack
if [ -z "$1" ]
then
pushd > /dev/null && dirs -p
# if $1 is a dash, then just do a "popd" and print out the directory stack
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
if [ -f ~/.DirB/"$1" -a "${1:0:1}" != "." ]
then
touch ~/.DirB/$1 ;
CD=pushd source ~/.DirB/$1 > /dev/null && dirs -p ;
# else just do a "pushd" and print out the directory stack
else
pushd "$1" > /dev/null && dirs -p
fi
fi
}
# "sl" - Saved bookmark Listing
function sl () {
# 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 [ "$1" == "-l" ]
then
shift
( cd ~/.DirB ;
ls -lt $* |
sed -e 's/ */ /g' -e '/^total/d' \
-e 's/^\(... \)\([0-9] \)/\1 \2/' |
cut -d ' ' -s -f6- | sed -e '/ [0-9] /s// &/' | less -FX ; )
# else print the short form of the bookmarks in reverse time order
else
( cd ~/.DirB ; ls -xt $* ; )
fi
}
# "r" - Remove a saved bookmark
function r () {
# if the bookmark file exists, remove it
if [ -e ~/.DirB/"$1" ]
then
rm ~/.DirB/"$1"
# if the bookmark file does not exist, complain and exit with a failing code
else
echo bash: DirB: ~/.DirB/"$1" does not exist >&2
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 [ -e ~/.DirB/"$1" ]
then
sed -e 's/\$CD //' -e 's/\\//g' ~/.DirB/"$1"
# if the bookmark does not exists, complain and exit with a failing code
else
echo bash: DirB: ~/.DirB/"$1" does not exist >&2
false
fi
}