在Ubuntu上快速切换工作目录


因为最近用Linux用的比较多,而且经常会在各种层次的目录之间进行切换,所以想提高提高效率,之前其实看到过这篇文章,但那时只是尝试了一下,并没有深层次的运用在工作中,所以也就忘了,不过后来在逛“团子大神”的blog时发现了这篇文章,而且他还写了个自动化部署以及增强版的mark-directory,但是我看了一下原文的内容,超级简单而且易于理解,所以还是选择了原始版本,自己动手的乐趣还是多些(不过期间也遇到了一些比较郁闷的问题,后来用文本导入的方式就没出现问题,不知道是哪一步的特殊字符出了问题?)

实现方法也很简单,将下面的内容存入文本文件mark_dir.txt:


export MARKPATH=$HOME/.marks

function jump {
cd -P "$MARKPATH/$1" 2>/dev/null || echo "No such mark: $1"
}
function mark {
mkdir -p "$MARKPATH"; ln -s "$(pwd)" "$MARKPATH/$1"
}
function unmark {
rm -i "$MARKPATH/$1"
}
function marks {
ls -l "$MARKPATH" | sed 's/ / /g' | cut -d' ' -f9- | sed 's/ -/t-/g' && echo
}
_completemarks() {
local curw=${COMP_WORDS[COMP_CWORD]}
local wordlist=$(find $MARKPATH -type l -printf "%fn")
COMPREPLY=($(compgen -W '${wordlist[@]}' -- "$curw"))
return 0
}

complete -F _completemarks jump unmark

直接复制粘贴就行,但是要注意换行符的格式为“n”而不是Windows下的“nr”。用WinScp将文件传输到VPS上,然后添加到$HOME/.bashrc文件的末尾(# cat mark_dir.txt >>~/.bashrc),然后用source命令使修改立即生效(# source ~/.bashrc),即可。

使用方法:

切换到一个深层次的目录,运行命令:# mark tmp

即可将为当前目录创建一个名为tmp的软链接,位于~/.mark/目录下,当你在别的目录时,可以使用命令:# jump tmp 快速跳转到这个目录(支持Tab补全)。

这都可以作为Ubuntu系统安装之后的必备“软件”添加到todolist里面去了。

当然,你也可以试试团子大神的mark-directory,一键式自动部署,还贴心的加了个卸载功能(删除对应目录以及在~/.bashrc中的内容)。

原文链接:
,

《 “在Ubuntu上快速切换工作目录” 》 有 4 条评论

  1. 在Linux系统上创建软链接
    `
    $ ln -s (源文件或目录) (目标文件或目录)

    软链接:
    1.软链接,以路径的形式存在,类似于Windows操作系统中的快捷方式
    2.软链接可以 跨文件系统,硬链接不可以
    3.软链接可以对一个不存在的文件名进行链接
    4.软链接可以对目录进行链接
    硬链接:
    1.硬链接,以文件副本的形式存在,但不占用实际空间
    2.不允许给目录创建硬链接
    3.硬链接只有在同一个文件系统中才能创建
    `
    http://www.cnblogs.com/peida/archive/2012/12/11/2812294.html
    http://www.cnblogs.com/xiaochaohuashengmi/archive/2011/10/05/2199534.html

  2. Linux下如何更新软链接的目标地址?
    https://serverfault.com/questions/147787/how-to-update-a-symbolic-link-target-ln-f-s-not-working
    `
    ln -sf (源目录) (目标目录) #不起作用
    ln -nsf (源目录) (目标目录) #起作用!!!

    分析过程:使用 strace 命令查看执行过程中的实际系统调用情况
    分析结果:
     -n, –no-dereference
         treat destination that is a symlink to a directory as if it were a normal file
         将目标是目录的符号链接作为一个普通文件进行处理(如何存在就先删除然后再创建)

     -f, –force
         remove existing destination files
    `

  3. A cd command that learns – easily navigate directories from the command line
    https://github.com/wting/autojump#installation
    `
    Jump To A Directory That Contains foo:
    $ j foo

    Jump To A Child Directory:
    Sometimes it’s convenient to jump to a child directory (sub-directory of current directory) rather than typing out the full name.
    $ jc bar

    Open File Manager To Directories (instead of jumping):
    Instead of jumping to a directory, you can open a file explorer window (Mac Finder, Windows Explorer, GNOME Nautilus, etc.) to the directory instead.
    $ jo music

    macOS install
    $ brew install autojump
    `

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注