【问题标题】:How do you change directory to a location that you just moved files into in bash?如何将目录更改为您刚刚在 bash 中将文件移动到的位置?
【发布时间】:2015-07-08 16:43:33
【问题描述】:

如果我说mv Folder/ ~/Documents/Folder1/Folder2

现在我想cd ~/Documents/Folder1/Folder2

那么,这两个可以归结为一行吗?

【问题讨论】:

  • 你可以编写一个函数来为你做这件事。您可以使用 readline 快捷方式来避免再次输入所有内容(<alt>-. 将自动插入上一行的最后一个参数,再次按下它会在您的历史记录中返回更远的位置)。

标签: bash shell unix terminal


【解决方案1】:

就这么简单:

mv Folder/ ~/Documents/Folder1/Folder2
cd !$

在 bash 中(交互式)!$ 代表最后一个命令的最后一个参数

【讨论】:

  • 是的!我知道这个命令。我的意思是要一个班轮。
【解决方案2】:

在脚本中你只是重复使用同一个目录,在交互式会话中你可以按ESC+.

【讨论】:

    【解决方案3】:

    这个问题我也确实有一段时间了,尤其是对于这种常见的情况:

    $ mkdir folder
    $ cd folder
    

    我已经为这个特殊情况做了一个小别名:

    function mkcd()
    {
        mkdir "$1" && cd "$1"
    }
    
    $ mkcd folder
    

    但是有两个问题让我对这个别名感到“沮丧”:

    • 这是一个更大问题的特例 (mkdir);
    • 这不是“直观的”:一般来说,在我意识到我需要它的那一刻,我已经输入了:$ mkdir folder;所以,为了使用我的别名,我必须回退一个字,然后删除 mkdir 的最后 3 个字符,然后替换为 cd,然后......然后我放弃了,因为输入 @987654326 更快@...

    所以当我读到你的问题时,它让我想写一些更好的东西,你可以在行尾添加一些东西(之前不必考虑它),它可以与任何其他命令一起使用,而不仅仅是 @ 987654327@.

    这是一个函数(我称之为cdd),您只需在行尾附加("<cmd> ; cdd")即可启动当前命令,然后将当前路径更改为最后一个参数command 是目录的路径。

    示例:

    /etc $ cp fstab /tmp; cdd            # --> current dir = /tmp
    /tmp $ ...
    

    另一个:

    ~ $ mkdir "some proj/src" -p; cdd    # --> current dir = some proj/src
    ~/some proj/src $ ...
    

    它不是完美没有错误,而且相当棘手,但效果很好(欢迎任何评论):

    代码:

    # Change the current path to the last directory used as an argument inside a
    # bash command line.
    #
    # Bash only.
    # This function MUST be used at the end of a single command line, like this:
    #    $ <any command> ; cdd
    # <any command> is any command with any argument(s), including at least one path
    # to an existing directory.
    # It handles dir names with spaces and quoted dir names
    #
    # Ex. :
    #    /etc $ cp fstab /tmp; cdd            # --> current dir = /tmp
    #    /tmp $ ...
    #
    #    ~ $ mkdir "some proj/src" -p; cdd    # --> current dir = ~/some proj/src
    #    ~/some proj/src $ ...
    function cdd()
    {
        local  last_com  stripped  args  nb_args  arg  last_dir  i
    
        # Get the current command from the history:
        last_com="$(history|tail -n1|sed -n 's/^\s*[0-9]*\s*//p')"
    
        # IMPORTANT: if you want to change to name of the function, you have to
        # change it as well in the regex just below:
        # Strip the call of 'cdd' function at the end:
        stripped="$(echo "$last_com"|sed -n 's/;\s*cdd\s*$//p')"
    
        # Split the command arguments:
        eval "args=( $stripped )"
    
        nb_args=${#args[@]}
        [[ $nb_args == 0 ]] && return
    
        # Look for the last directory used as an argument:
        for (( i = nb_args - 1; i != 0; i-- )); do
            arg="${args[$i]}"
            if [[ -d $arg ]]; then
                last_dir="$arg"
                break
            fi
        done
    
        # If found, change current directory:
        [[ -n $last_dir ]] && cd "$last_dir"
    }
    

    【讨论】:

    • 哇!你的信念让我大吃一惊,兄弟! :) 我将您的脚本添加到我的 .bash_profile 中,但它不起作用。我错过了什么吗? source .bash_profile 编译就够了吧?
    • 大声笑 :) 我对这个问题有一种动力的冲动...... :) 它对我有用;也许最好把它放在 ~/.bashrc 中,然后再次运行你的 bash 控制台,然后像解释的那样使用它:mv file dest; cdd。有什么问题?是没找到函数,还是有错误提示?
    猜你喜欢
    • 2016-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-13
    • 1970-01-01
    • 2011-06-07
    • 2015-11-09
    • 2016-05-11
    相关资源
    最近更新 更多