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!



Recent Posts:




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, Video Tutorials, Code Snippets, How Tos, Blogs, Articles, etc.