【问题标题】:List absolute path of folders for a given path - shell列出给定路径的文件夹的绝对路径 - shell
【发布时间】:2017-10-22 00:33:40
【问题描述】:

我想使用 shell 脚本列出给定路径中所有文件夹的绝对路径。

我试过的是:

ls -l /var/www/temp

但我找不到用于列出绝对路径的ls 命令选项。

我在 StackOverflow 中发现了一个相关问题:how to list full paths of folders inside a directory in linux?

但我需要的是在一个命令本身中,我需要列出给定路径(此路径不同)中的所有文件夹及其绝对路径。

谁能帮我做这件事?提前致谢。

【问题讨论】:

  • 在标记为与您链接的问题重复的问题中,this anwser 不是已经完成了您需要的操作吗?
  • @Dymen1..在那个问题解决方案中,我可以在哪里指定路径?
  • @DecentDabbler..用那个答案试过了。但它也在列出文件。我只需要文件夹。
  • 你确定吗?它对我很好。你有没有在你的环境中定义了额外选项的ls 别名?如果是这样,请执行which ls 并使用该输出(例如/bin/ls)作为命令:/bin/ls -d /your/path/*

标签: php shell unix exec filelist


【解决方案1】:

这会有所帮助,但它不使用“ls”

您可以使用 find 替换 pwd。

   find /var/www/temp -type d

但请注意,这将在屏幕上输出列表。

【讨论】:

    【解决方案2】:

    shell函数怎么样?

    dabspath () {
    if [ -d "$1" ]
    then
        cd "$1"
        find "$PWD" -type d
        cd "$OLDPWD"
    else
        echo "$0: $1: No such directory"
    fi
    }
    

    用法:dabspath foo

    如果 foo 是相对于当前工作目录的目录,那么它将打印 foo 和任何子目录的绝对路径。

    【讨论】:

      【解决方案3】:

      for D in `find . -maxdepth 1 -type d`; do echo $PWD${D#.}; done

      它是如何工作的:

      1. 首先,bash 将在反引号中运行 find 命令,并将反引号中的部分替换为 find 返回的内容,即工作目录的所有直接子目录的名称。
      2. 接下来,for 循环将遍历所有子目录,对于存储在变量 D 中的每个子目录,它将打印工作目录 ($PWD) 的路径,然后是子目录 D,从中删除前缀“ ."。这样,绝对路径会按预期打印。

      注意:由于每个目录都有一个指向自身的硬链接(“.”子目录),它还会打印工作目录的路径。

      【讨论】:

        【解决方案4】:

        此脚本列出了所有目录,或者可选地,所有由find-type T 选项识别的任何类型。默认情况下,如果没有提供参数,它会列出当前目录中的所有目录。要列出绝对路径,请将绝对路径作为目标目录传递。

        #!/bin/bash
        
        # usage: ${0} [-type [fd]] [-l] <directory>
        
        _TYPE="d" # if f, list only files, if d, list only directories
        let _long=0
        let _typeflag=0
        
        # collect dirs and files
        DIRS=( ) ; FILS=( )
        for A in "$@" ; do
          if [ $_typeflag -eq 1 ]; then
            _TYPE=$A
            let _typeflag=0
          elif [ -d "$A" ]; then
            DIRS=( ${DIRS[@]} "$A" )
          elif [ -f "$A" ]; then
            FILS=( ${FILS[@]} "$A" )
          else
            case "$A" in
            "-type") let _typeflag=1 ;;
            "-l") let _long=1 ;;
            *) echo "not a directory: [$A]" 1>&2
              exit 1
              ;;
            esac
          fi
        done
        
        # list files in current dir, if nothing specified
        [ ${#DIRS[@]} -eq 0 ] && DIRS=( "$(pwd -P)" )
        
        if [ $_long -eq 0 ]; then
          find ${DIRS[@]} -maxdepth 1 -type $_TYPE | while read F ; do
            if [[ "$F" != "." && "$F" != ".." ]]; then
              echo "\"$F\""
            fi
          done | xargs ls -ltrad --time-style=long-iso | sed 's#.*:[0-9][0-9] ##'
        else
          find ${DIRS[@]} -maxdepth 1 -type $_TYPE | while read F ; do
            echo "\"$F\""
          done | xargs ls -ltrad --time-style=long-iso
        fi
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2017-01-17
          • 1970-01-01
          • 2013-08-28
          • 2018-09-30
          • 1970-01-01
          • 2011-05-09
          • 2012-07-09
          相关资源
          最近更新 更多