Linux tool direct to common directory avoid frequent CD switch directory

Source: Internet
Author: User

Copy the source code to the z.sh file in your user directory and open it with vim. BASHRC This directory, add "source/home/yourname/z.sh" at the end.

Then go back to shell execution: source. bashrc. So this thing will come into effect.

For example, after running CD/HOME/A/B/C/D, Linux enters the D directory and then enters Z

This is the system will save the path/home/a/b/c/d to D, next time you just enter Z D in the shell this can go directly into the directory/HOME/A/B/C/D. Very convenient

# Copyright (c) Rupa Deadwyler under the WTFPL license# maintains a jump-list of the directories you actually use## INSTALL:# * Put something like this in your. BASHRC/.ZSHRC:#./path/to/z.sh# * CD around for a while to build up the DB# * profit!!# * Optionally:# Set $_z_cmd in. BASHRC/.ZSHRC to change the command (default Z).# Set $_z_data in. BASHRC/.ZSHRC to change the datafile (default ~/.z).# Set $_z_no_resolve_symlinks to prevent symlink resolution.# Set $_z_no_prompt_command if you ' re handling Prompt_command yourself.# Set $_z_exclude_dirs to a array of directories to EXCLUDE.# set $_z_owner to your username if you want use Z while sudo with $HOME kept## Use:# * Z Foo # CD to most frecent dir matching foo# * z Foo bar # CD to most frecent dir matching foo and bar# * Z-r Foo # CD to highest ranked dir matching foo# * Z-T Foo # CD to most recently accessed dir matching foo# * z-l foo # list matches instead of CD# * z-c Foo # restrict matches to Subdirs of $PWD[- D "${_z_data:-$HOME/.z}"] && {Echo "Error:z.sh ' s datafile (${_z_data:-$HOME/.z}) is a directory."}_z() {Local datafile="${_z_data:-$HOME/.z}"    # Bail If we don ' t own ~/.z and $_z_owner not set[-Z"$_z_owner"  -A - F "$datafile"  -A! -O"$datafile"] &&return    # Add Entries    if["$"="--add"]; ThenShift# $HOME isn ' t worth matching["$*"="$HOME"] &&return        # don ' t track excluded directory treesLocal exclude forExcludeinch "${_z_exclude_dirs[@]}"; Do             Case "$*" inch "$exclude*")return;;Esac         Done        # Maintain the data fileLocal tempfile="$datafile. $RANDOM"         while ReadLine Do            # only count directories[- D "${line%%\|*}"] &&Echo $line         Done<"$datafile"| Awk-v path="$*"-V now="$ (date +%s)"-F"|" ' BEGIN {Rank[path] = 1 Time[path] = now} $ >= 1                    {# drop ranks below 1 if (= = path) {Rank[$1] = $1                TIME[$1] = now} else {rank[$1] = $ time[$1] = $ $  } Count + = $ ($ = $) END {if (Count > 9000) {# Aging for (x in rank) print X "|" 0.99*RANK[X] "|" Time[x]} else for (x in rank) print X "|" rank[x] "|" time[x]} ' 2>/dev/null >|"$tempfile"        avoid clobbering the datafile in a race condition        if[ $?-ne 0  -A - F "$datafile"]; ThenEnv RM- F "$tempfile"        Else["$_z_owner"] && Chown$_z_owner: $ (id-ng$_z_owner)"$tempfile"ENV MV- F "$tempfile" "$datafile"|| Env RM- F "$tempfile"        fi    # Tab Completion    elif["$"="--complete"  -A - S "$datafile"]; Then         while ReadLine Do[- D "${line%%\|*}"] &&Echo $line         Done<"$datafile"| Awk-v q="$"-F"|" ' BEGIN {if (q = = ToLower (q)) IMatch = 1 q = substr (q, 3) GS  UB ("", ". *", Q)} {if (IMatch) {if (ToLower ($) ~ tolower (q)) print ' Else if ' ($ ~ q) print '} ' 2>/dev/nullElse        # List/go         while["$"]; Do  Case "$" inch--) while["$"]; DoShift Local fnd="$fnd${fnd:+}"; Done;; -*) Local opt=${1:1}; while["$opt"]; Do  Case ${opt:0:1} inchc) Local fnd="^$PWD $fnd";; HEcho "${_z_cmd:-z} [-chlrtx] args">&2;return;; x) sed-i- e "\:^${pwd}|. *:D " "$datafile";; L) Local list=1;; R) Local typ="Rank";; T) Local typ="recent";;Esac; opt=${opt:1}; Done;; *) Local fnd="$fnd${fnd:+}";;Esac; Local last= $; ["$#" -GT 0] && shift; Done["$fnd"  -A "$fnd"!="^$PWD "] || Local list=1        # If we hit enter on a completion just go there         Case "$last" inch            # completions 'll always start with//*) [-Z"$list"  -A - D "$last"] &&CD "$last"&&return;;Esac        # no file yet[- F "$datafile"] ||returnLocalCD        CD="$ (while the read line; Do [-D "${line%%\|*}"] && echo $line done < "$datafile" | Awk-v t= "$ (date +%s)"-V list="$list"-V typ="$typ"-V q="$fnd"-F"|" ' Function frecent (rank, time) {# relate frequency and time DX = T-time if (DX < 3600) return rank * 4 if (DX < 86400) return rank * 2 if (DX <                604800) return RANK/2 return RANK/4} function output (files, out, common) { # list or return the desired directory if (list) {cmd = "sort-n >&2 "For (x in files) {if (files[x]) printf"%-10s%s\n ", files[x], x | CMD} if (common) {printf "%-10s%s\n", "Common:", Common & Gt                    "/dev/stderr"}} else {if (common) out = Common Print out}} function common (matches) {# Find the common root of a List of matches, if it exists for (x in matches) {if (Matches[x] && (!short | | length (x) < Length (short                )) {short = x}} if (short = = "/") return # Use a copy to escape special characters, as we want to return # the original.                Yeah, this escaping is awful. Clean_short = Short Gsub (/[\ (\) \[\]\|]                /, "\\\\&", Clean_short) for (x in matches) if (Matches[x] && x!~ clean_short) return Return short} BEGIN {gsub ("", ". *", q) Hi_rank = Ihi_rank  = -9999999999} {if (Typ = = "Rank") {rank = $ $}                else if (Typ = = "recent") {rank = $3-t} else Rank = Frecent ($ $ $)          if ($ ~ q) {Matches[$1] = Rank      } else if (ToLower ($) ~ tolower (q)) imatches[$1] = Rank if (matches[$1] && matches[$1] > Hi_rank) {Best_match = $ hi_rank = matches[$1]} else if (Imatche S[$1] && imatches[$1] > Ihi_rank) {ibest_match = $ Ihi_rank = imatches            [$]} } END {# Prefer case sensitive if (best_match) {output (MATC HES, Best_match, common (matches))} else if (ibest_match) {output (imatches, IBEST_MATC H, Common (Imatches)}} ')"        [ $? -GT 0] && return ["$CD"] && CD""C""Fi}alias ${_z_cmd:-z}= ' _z 2>&1 ' ["$_z_no_resolve_symlinks" ] || _z_resolve_symlinks= "-P"If Compctl >/dev/null 2>&1; Then # zsh ["$_z_no_prompt_command" ] ||        {# Populate directory list, avoid clobbering any other precmds. if ["$_z_no_resolve_symlinks" ]; Then _z_precmd () {_z--add "${pwd:a}"} else _z_precmd () {_z--add"${pwd:a}"} fi [[-N]${precmd_functions[(R) _z_precmd]}" ]] ||    {precmd_functions[$ (($ #precmd_functions + 1))]=_z_precmd}} _z_zsh_tab_completion () {# tab completion local compl read-l compl reply= (${(f) "$ (_z--c Omplete "$compl") "}"} compctl-u-K _z_zsh_tab_completion _zelif complete >/dev/null 2>&1; Then # Bash # tab Completion complete-o filenames-c ' _z--complete '$COMP _line"' ${_z_cmd:-z} ["$_z_no_prompt_command" ] ||        {# populate directory list. Avoid clobbering other prompt_commands. grep "_z--add"<<<"$PROMPT _command">/dev/null | | {prompt_command= "$PROMPT _command"$ ' \ n ' ' _z--add"$ (Commandpwd ' $_z_resolve_symlinks ' 2>/dev/null)"2>/dev/null; '}} Fi

Linux tool direct to common directory avoid frequent CD switch directory

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.