2013-05-01 61 views
0

我知道这是一个愚蠢的问题:我怎么能LS某些目录(文件+目录内),并返回类似:0的文件,1目录,2 simlinkLinux的自定义列表目录

<name_dir>,1 
<name_dir>,1 
<name_file>,0 
<name_file>,0 
<name_file>,0 
<name_file>,0 

我需要通过ssh传递结果,我认为尽可能小的流量。 也许这不是最聪明的选择,任何想法? ls或find -printf

+1

我将会用C编写自定义LS程序,使用stat系统调用。看[man stat](http://linux.die.net/man/2/stat) – user2035147 2013-05-01 12:21:57

回答

1

ls -F用尾部斜杠标记目录。然后使用sed将斜杠转换为“,1”并去除其他标记(用于符号链接等)或将其替换为您选择的后缀。最后的“0" ,附加到一切不结束” 1" (或您添加的任何其他后缀)

此后缀使用目录“ 1" 以及其他一切与” 0"

ls -F | sed ' 
    s/[\*\@\|]$//; 
    s/=>$//; 
    s/\/$/,1/; 
    /,1$/! s/$/,0/' 
0

另一个建议:

#!/bin/sh 
for f in `ls -1 --group-directories-first`; do 
    if [ -d $f ]; then echo "$f,1"; 
    elif [ -L $f ]; then echo "$f,2"; 
    else echo "$f,0"; fi 
done