ls command: sort files by name alphabetically A-Z or Z-A [Linux/Unix/macOS/Bash]


LS Command Sorting
LS Command Sorting

If you are looking for sorting your ls command output on terminal by file names in ascending or descending order (Linux, Unix, macOS or Bash terminal), you would need to make use of sort command with ls

Syntax for ls sorting:
ls -l | sort -> sorts in alphabetical order A-Z
ls -l | sort -r -> sorts in alphabetically reverse order Z-A
Examples:

ls command without any sorting

$ ls -l
total 0
-rw-r--r-- 1 linuxBox 197121 0 Aug 22 15:24 china.txt
-rw-r--r-- 1 linuxBox 197121 0 Aug 22 15:24 denmark.txt
-rw-r--r-- 1 linuxBox 197121 0 Aug 22 15:24 germany.txt
-rw-r--r-- 1 linuxBox 197121 0 Aug 22 15:24 india.txt
-rw-r--r-- 1 linuxBox 197121 0 Aug 22 15:24 japan.txt
-rw-r--r-- 1 linuxBox 197121 0 Aug 22 15:24 java.txt
-rw-r--r-- 1 linuxBox 197121 0 Aug 22 15:24 newzeland.txt
-rw-r--r-- 1 linuxBox 197121 0 Aug 22 15:24 php.txt
-rw-r--r-- 1 linuxBox 197121 0 Aug 22 15:24 poland.txt
-rw-r--r-- 1 linuxBox 197121 0 Aug 22 15:24 sweden.txt
-rw-r--r-- 1 linuxBox 197121 0 Aug 22 15:24 uk.txt
-rw-r--r-- 1 linuxBox 197121 0 Aug 22 15:24 usa.txt

ls command with alphabetical order sorting (acending)

$ ls -l | sort
-rw-r--r-- 1 linuxBox 197121 0 Aug 22 15:24 china.txt
-rw-r--r-- 1 linuxBox 197121 0 Aug 22 15:24 denmark.txt
-rw-r--r-- 1 linuxBox 197121 0 Aug 22 15:24 germany.txt
-rw-r--r-- 1 linuxBox 197121 0 Aug 22 15:24 india.txt
-rw-r--r-- 1 linuxBox 197121 0 Aug 22 15:24 japan.txt
-rw-r--r-- 1 linuxBox 197121 0 Aug 22 15:24 java.txt
-rw-r--r-- 1 linuxBox 197121 0 Aug 22 15:24 newzeland.txt
-rw-r--r-- 1 linuxBox 197121 0 Aug 22 15:24 php.txt
-rw-r--r-- 1 linuxBox 197121 0 Aug 22 15:24 poland.txt
-rw-r--r-- 1 linuxBox 197121 0 Aug 22 15:24 sweden.txt
-rw-r--r-- 1 linuxBox 197121 0 Aug 22 15:24 uk.txt
-rw-r--r-- 1 linuxBox 197121 0 Aug 22 15:24 usa.txt
total 0

ls command with alphabetical reverse order sorting (descending)

$ ls -l | sort -r
total 0
-rw-r--r-- 1 linuxBox 197121 0 Aug 22 15:24 usa.txt
-rw-r--r-- 1 linuxBox 197121 0 Aug 22 15:24 uk.txt
-rw-r--r-- 1 linuxBox 197121 0 Aug 22 15:24 sweden.txt
-rw-r--r-- 1 linuxBox 197121 0 Aug 22 15:24 poland.txt
-rw-r--r-- 1 linuxBox 197121 0 Aug 22 15:24 php.txt
-rw-r--r-- 1 linuxBox 197121 0 Aug 22 15:24 newzeland.txt
-rw-r--r-- 1 linuxBox 197121 0 Aug 22 15:24 java.txt
-rw-r--r-- 1 linuxBox 197121 0 Aug 22 15:24 japan.txt
-rw-r--r-- 1 linuxBox 197121 0 Aug 22 15:24 india.txt
-rw-r--r-- 1 linuxBox 197121 0 Aug 22 15:24 germany.txt
-rw-r--r-- 1 linuxBox 197121 0 Aug 22 15:24 denmark.txt
-rw-r--r-- 1 linuxBox 197121 0 Aug 22 15:24 china.txt

Comments:

  • This was really useful - Thanks
    anonymous 12 Sep 2020 11:09:22 GMT
  • Further comments disabled!
Advertisements
Try Out Code2care Dev Tools:

Advertisements



Android Java Linux Microsoft Google Python macOS Notepad++ Microsoft Teams CSS PHP SharePoint Html Linux C Programs Bootstrap jQuery Sublime Android Studio Facebook Eclipse WhatsApp News MySQL Json HowTos JavaScript FTP S3


Advertisements


Code2care is an initiative to publish and share varied knowledge in programming and technical areas gathered during day-to-day learnings and development activities.

Students and Software Developers can leverage this portal to find solutions to their various queries without re-inventing the wheel by referring to our easy to understand posts. Technical posts might include Learnings, Tutorials, Trouble-Shooting steps, Video Tutorials, Code Snippets, How Tos, Blogs, Articles, etc.
🎉 We are celebrating the 10th years of Code2care! Thank you for all your support! We hope we made a difference.
We strong support Gender Equality & Diversity.

We stand in solidarity with Ukraine - Make a donation to UNHCR - https://donate.unhcr.org/in/en-in/ukraine-emergency