Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting file names by length of the file name

Tags:

unix

sorting

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

Any help will be highly appreciated. Thanks in Advance

like image 999
Zama Ques Avatar asked Mar 26 '12 07:03

Zama Ques


People also ask

How do I sort files by length?

With right-click sort Another way to sort files in a given folder is by right-clicking on the body of the folder. A pop-up will appear. Here, click on “Sort by -> Size.” The files will be sorted accordingly.

How do I sort files by file names?

Icon view. To sort files in a different order, click the view options button in the toolbar and choose By Name, By Size, By Type, By Modification Date, or By Access Date. As an example, if you select By Name, the files will be sorted by their names, in alphabetical order. See Ways of sorting files for other options.

How do I identify long file names?

Use SearchMyFiles Utility Tool to Search Files with Long Names or Text. The SearchMyFiles tool by NirSoft is an effective third-party application that allows you to easily search files when their name or path length is longer than a certain number of characters.

What is the length of a file name?

Every operating system has a limit to how many characters can be used in a file name that is around 255 characters long. When determining the length of a file, both the file name and the file extension are used together to get the total length. For example, the file "myfile. txt" is ten characters long.


2 Answers

The simplest way is just:

$ ls | perl -e 'print sort { length($b) <=> length($a) } <>'
like image 66
tchrist Avatar answered Oct 01 '22 20:10

tchrist


You can do like this

for i in `ls`; do LEN=`expr length $i`; echo $LEN $i; done | sort -n
like image 30
Raghuram Avatar answered Oct 01 '22 22:10

Raghuram