Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sort alphabeticaly using ls command

Status
Not open for further replies.

rkam

Programmer
Mar 15, 2009
3
IN
how to list a files in a directory alphabetically using ls command

Thx
rk
 
Listing files alphabetically is the default. Maybe you could elaborate on your needs?

"Proof that there is intelligent life in Oregon. Well, Life anyway.
 
I have attached an image for the ls command.
When I execute this command there are couple of files not listed alphabetically at the beginning of the list.
My question:
1. How to sort them alphabetically?
2. How to list only directories alphabetically?
3. How to list only files alphabetically?
 
Hi

rkam said:
1. How to sort them alphabetically?
You can not. ( Supposing by "alphabetically" you mean "like the market leader OS does". Otherwise, that list is already sorted alphabetically. )
rkam said:
2. How to list only directories alphabetically?
You can not.
rkam said:
3. How to list only files alphabetically?
You can not. ( Supposing by "only files" you mean "like the market leader OS does". Otherwise, that list already contains only files. )

See [tt]find[/tt], [tt]sort[/tt], [tt]grep[/tt], ( maybe [tt]stat[/tt] on Linux ) and write a script/function/alias for your needs.

Feherke.
 
Hi

Ok, let me try it abit more creatively. Some examples of combining the mentioned commands :
Code:
[gray]# sort by name alphabetically, case insensitive[/gray]
ls | sort -f
ls -l | sort -k 8

[gray]# display only files of type directory[/gray]
ls -l | grep ^d
find . -maxdepth 1 -type d

[gray]# display files of other type than directory[/gray]
ls -l | grep -v ^d

[gray]# list only regular files[/gray]
find . -maxdepth 1 -type f
One possible way to put them together in a function :
Bash:
function myls()
{
  local path="${1:-.}" type="$2"
  find "$path" -maxdepth 1 ${type:+-type $type} | sort -f
}


Feherke.
 
Hi

One thing I found out recently. The GNU version of [tt]ls[/tt] sorts depending on the [tt]LC_ALL[/tt] setting. Actually this is not intended to change the case sensitivity, it is part of internationalization.
Code:
[blue]master #[/blue] LC_ALL=POSIX ls -1
TWO
one
three

[blue]master #[/blue] LC_ALL=en_US ls -1
one
three
TWO

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top