linux 快速 cd ../../.......
在 ~/.bash_rc 內加入以下 function
即可在新開的 terminal 中, 用 .. N 快速切換到祖先目錄.
FYI: cd - 可以來回切換前後目錄.
toN() # toN num default
{
test ${1} || return ${2}
if [[ ${1} = *[[:digit:]]* ]] ; then
return ${1}
else
return "0"
fi
}
..() {
toN "$1" "1"
num=$?
if [[ $num -gt 0 ]] ; then
seq=`seq $num`
next=`printf '../%.0s' {$seq}`
cd $next
else
echo "Usage: .. [1..N]"
echo " to change to previous N level directory"
echo "*** ${BASH_SOURCE} ${FUNCNAME}():${LINENO} th line ***"
fi
}
1 個意見:
function backtrace () {
local msg=""
local depth=${#FUNCNAME[@]}
for ((i=1; i<$depth; i++)); do
local func="${FUNCNAME[$i]}"
local line="${BASH_LINENO[$((i-1))]}"
local src="${BASH_SOURCE[$((i-1))]}"
msg="${msg}\nat: $func(), line $line, $src "
done
echo -e $msg 1>&2
}
function trace_top_caller () {
local func="${FUNCNAME[1]}"
local line="${BASH_LINENO[0]}"
local src="${BASH_SOURCE[0]}"
echo " called from: $func(), line $line, $src" 1>&2;
}
function debug_option() {
for var in "$@"
do
if [[ "$var" = "--debug" ]] ;then echo "1"; return 1;fi
done
echo "0"; return 0
}
# --------------------
# toN default num
function toN() {
# backtrace
[[ ${#} -ge 2 ]] || { echo "${1}"; return ${1}; }
[[ ${2} =~ [[:digit:]]{${#2}} ]] || { echo "0"; return 0; }
echo "${2}" ; return ${2}
}
function ..() {
#set -x
# set -o errtrace
# trap 'trace_top_caller' ERR
local debug=$(debug_option "$@")
if [[ $debug = "1" ]] ;then echo "debug=$debug" ;fi
local num=$(toN 1 ${1})
# echo $num
if [[ $num -ge 1 ]] ;then
seq=$(seq $num)
next=$(printf '../%.0s' {$seq})
cd $next
else
echo "Usage: ${FUNCNAME} [1..N]"
echo " to change to previous N level directory"
if [[ $debug = "1" ]] ;then
backtrace
fi
fi
#set +x
}
張貼留言
訂閱 張貼留言 [Atom]
<< 首頁