2

我想知道如何以这种格式列出目录。普通列表结构:

#find . -follow -type f | sed "s/.//"

/files/test1/test2/file1.txt
/files/test1/test2/file2.txt
/files/test1/test2/file3.txt
/files/test1/file1.txt
/files/test1/file2.txt
/files/test1/file3.txt
/files/file1.txt
/files/file2.txt
/files/file3.txt
/file1.txt
/file2.txt
/file3.txt

我想列出如下:

/files/test1/test2/file1.txt
file2.txt
file3.txt
/files/test1/file1.txt
file2.txt
file3.txt
/files/file1.txt
file2.txt
file3.txt
/file1.txt
file2.txt
file3.txt

像这样:

 ls -R1 | sed -e 's/://' -e 's/.//'

但想要上面解释的结构!

4

2 回答 2

3
... | while read path; do 
    dir=$(dirname "$path")
    if [[ $dir = $prev ]]; then 
        echo $(basename "$path")
    else 
        echo $path
        prev=$dir
    fi
done
于 2012-05-14T19:45:46.393 回答
1

有趣的测验!

设置示例目录和文件

mkdir -p files/test1/test2 && for P in . files files/test1 files/test1/test2; do touch "$P"/file{1,2,3}.txt; done

作为一个单行,尝试

find . -type d | sort -r | while read P; do printf "${P#.*}"/; ls -v | while read F; do echo ${F##*/}; done; done

输出就像你的要求

/files/test1/test2/file1.txt
file2.txt
file3.txt

/files/test1/file1.txt
file2.txt
file3.txt

/files/file1.txt
file2.txt
file3.txt

/file1.txt
file2.txt
file3.txt

解决这个问题很有趣。

于 2012-05-15T09:15:17.120 回答