-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathccd
More file actions
88 lines (65 loc) · 1.91 KB
/
ccd
File metadata and controls
88 lines (65 loc) · 1.91 KB
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
#!/bin/bash
# sysinfo_page - An improved version of cd. Finds all instances of a directory to change into. If it is unique, it changes to the directory. Otherwise, it lists options for the user.
# !!!This script should be sourced!!!
h_info() {
echo "Searches for a directory named 'goal_dir' up to 'depth' subdirectories deep from the current working directory. Does not include directories with white space or special characters."
echo
echo " Syntax: ccd [-h] goal_dir depth"
echo
echo " Examples - assuming sourcing is done"
echo " $ ccd Desktop 2"
echo " $ ccd -h"
echo
echo " Examples assuming sourcing is not done"
echo " $ source ccd Desktop 2"
echo " $ . ccd -h"
}
################ CCD Below ###############
goal_dir=$1
depth=$2
matches=()
if [ "$depth" = "" ]; then
depth=5;
fi
if [ "$goal_dir" = "-h" ]; then
h_info
return
fi
for dir in $(find $(pwd) -maxdepth "${depth}" -type d)
do
if [[ -d $dir ]]; then
if [ "$(basename "$dir")" = "${goal_dir}" ]; then
matches+=("$dir")
fi
fi
done
len=${#matches[@]}
if [ $len -eq 1 ]; then
cd "${matches[0]}"
elif [ $len -eq 0 ]; then
echo "No matches found within $depth subdirectories"
else
for i in $(seq 0 "$(($len - 1))");
do
echo $i - "${matches[$i]}"
done
choice="a"
nre='^[0-9]+$'
until [[ $choice =~ $nre ]] &&
[ "$choice" -lt "$len" ] &&
[ "$choice" -ge "0" ];
do
#echo
echo -n "Multiple matches found. Please choose one (r to refresh options): "
IFS=' ' read -ra line
choice="${line[0]}"
if [[ "$choice" =~ "r" ]] && [ ${#choice} -eq 1 ] && ! [[ $choice =~ $nre ]]; then
echo
for i in $(seq 0 "$(($len - 1))");
do
echo $i - "${matches[$i]}"
done
fi
done
cd "${matches[$choice]}"
fi