【问题标题】:Is there a way to change directory in the terminal to a directory at a certain "index"?有没有办法将终端中的目录更改为某个“索引”处的目录?
【发布时间】:2014-06-30 13:12:10
【问题描述】:

假设我要更改的所有目录都有长名称,因此每次在终端中输入cd "Lorem ipsum dolor sit amet consectetur adipiscing elit" 很麻烦,但我已经调用了ls 并且知道该目录是第一个目录.有没有办法用cd 0之类的东西改成它? IE。当以与调用 ls 时的结果相同的方式排序时,我可以更改到索引 X 处的任何目录吗?

【问题讨论】:

    标签: linux terminal command


    【解决方案1】:

    没有这样的功能。您可以使用制表符完成:cd Lor<tab> 或编写一个执行您描述的函数:

    cdn() { 
      local n=$1
      for d in */
      do
        (( n-- )) || { cd "$d"; break; }
      done
    
      # Error checking left as an exercise
    }
    

    【讨论】:

      【解决方案2】:

      @that other guy's answer可能是最好的答案,但您也可以使用这样的脚本:

      #!/bin/sh
      
      i=0
      for directory in */ .*/
      do
          i=$(($i+1))
          if [ $1 -eq $i ]
          then
              cd $directory
              break
          fi
      done
      

      脚本遍历所有目录(包括隐藏目录和./../),并在到达您选择cd 进入的索引时停止。然而,有一个警告(这就是为什么@that other guy's answer可能更好):脚本必须使用.source命令运行(例如:. cd-index.sh 3而不是./cd-index.sh 3)。如果您不使用. 运行它,它只会更改脚本中的目录。

      【讨论】:

        猜你喜欢
        • 2018-03-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-11-20
        • 2018-09-05
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多