Question

I know it's a dumb question: how can I ls some directory (files+directory inside) and return something like : 0 for files, 1 for directory, 2 for simlink

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

I need to pass the result over ssh and I think to have as small as possible traffic. Maybe this isn't the smartest option, any idea? ls or find -printf

Was it helpful?

Solution

ls -F marks directories with a trailing slash. Then use sed to convert the slash into ",1" and to get rid of the other marks (for symlinks etc) or replace them by a suffix of your choice. Finally append ",0" to everything which does not end in ",1" (or any other suffix you added)

This suffixes directories with ",1" and everything else with ",0"

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

OTHER TIPS

Another suggestion:

#!/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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top