-
Notifications
You must be signed in to change notification settings - Fork 0
/
cdhistory.sh
executable file
·61 lines (56 loc) · 1.78 KB
/
cdhistory.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
##############################################################################################
#
# Name: cdhistory.sh
#
# Gives a list of last reached absolute paths
#
# Usage: See Readme
#
# Author: Floris Vedder
#
# Date: 2017/08/12
#############################################################################################
#!/bin/sh
pathoptions=''
IFS=$'\n'
# Fetch all absolute paths from your history
history_paths=$(history | grep -o '/Volumes/[^.]*\|~/[^.]*' \
| tr -cd 'A-Za-z0-9_-\\/~\n\r\ ' | sed -e 's/\(\/\)*$//g' | sort -u | tail -50)
for history_path in $history_paths ; do
#check if it are indeed directories before adding them to the options
#first make sure the ~ works in the script so replace it with the real absolute path
if [[ $history_path == ~* ]]; then
absolute_path="${history_path/#\~/$HOME}"
else
absolute_path="$history_path"
fi
#no we can do the check to see if it's a directory
if [ -d "$absolute_path" ] && [ $history_path != "~" ]; then
# except for the first option we will add an enter \n bfore adding a path to the list pathoptions to get a nice list
if [ -z "$pathoptions" ]; then
pathoptions=$(printf "$history_path")
else
pathoptions=$(printf "$pathoptions\n$history_path")
fi
fi
done
# create a option list
PS3='Please enter your choice: '
options=( $pathoptions )
select path in "${options[@]}"
do
# replace the tilde for an absolute path when it starts with the tilde
if [[ $path == ~* ]]; then
path="${path/#\~/$HOME}"
fi
# inform the user with a correct feedback
if [ ! -d "$path" ]; then
echo "sorry, this choice is not a directory (anymore)"
break
else
echo "you're directory will be changed to: $path"
fi
# change directory to the chosen path
cd $path
break
done