【问题标题】:How to automatically list directory contents after a CD command?如何在 CD 命令后自动列出目录内容?
【发布时间】:2010-08-17 14:23:50
【问题描述】:

我想创建一个新的 unix 命令以在 CD 命令之后自动列出目录的内容。到目前为止,我已经尝试了几件事:

alias cdls='cd $1 | ls -l'

function cdls(){ cd $1; ls -l;}

两者都执行列表,但不更改工作目录。

谢谢。

【问题讨论】:

    标签: unix command-line


    【解决方案1】:
    Do automatically an ls after every successfull cd:
    
    function cd {
        builtin cd "$@" && ls
    }
    Go up n levels:
    
    # Usage .. [n]
    function .. (){
        local arg=${1:-1};
        local dir=""
        while [ $arg -gt 0 ]; do
            dir="../$dir"
            arg=$(($arg - 1));
        done
        cd $dir #>&/dev/null
    }
    

    【讨论】:

      【解决方案2】:

      使用 zsh。谷歌zsh {your distribution} 获取说明。

      【讨论】:

      • 我在windows上试过zsh,不行。它成功执行列表,但不更改工作目录。
      • 用function cdls()... 再次测试,它成功了!感谢您的帮助。
      【解决方案3】:

      不妨试试:

      别名 cdls='cd $1 && ls -l'

      不过(在 zsh 中),您的示例对我有用。

      【讨论】:

        【解决方案4】:

        为什么不直接使用cd dir/dir/ ; ls?

        【讨论】:

        • 因为我不想输入 '; ls -l' 在每张 cd 之后。
        • 哈,很公平。在这种情况下查看 zsh。
        猜你喜欢
        • 1970-01-01
        • 2022-06-17
        • 2019-01-11
        • 1970-01-01
        • 2019-02-23
        • 2015-07-05
        • 2014-08-02
        • 2015-01-01
        • 2019-01-28
        相关资源
        最近更新 更多