Sort file names by file name length

ls displays the files available in the directory. I want the file names to be displayed depending on the length of the file name.

Any help would be greatly appreciated. Thanks at Advance

+7
source share
4 answers

You can do it as follows:

for i in `ls`; do LEN=`expr length $i`; echo $LEN $i; done | sort -n 
+8
source

The easiest way:

 $ ls | perl -e 'print sort { length($b) <=> length($a) } <>' 
+4
source

make test files:

 mkdir -p test; cd test touch short-file-name medium-file-name loooong-file-name 

script:

 ls |awk '{print length($0)"\t"$0}' |sort -n |cut --complement -f1 

output:

 short-file-name medium-file-name loooong-file-name 
+2
source
 for i in *; do printf "%d\t%s\n" "${#i}" "$i"; done | sort -n | cut -f2- 
0
source

All Articles