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!
More Posts related to Linux,
- [Fix] Linux - bash: useradd: command not found
- Calculate days between dates using dateutils ddiff command
- pwd Command - Print Working Directory
- Rename a directory using Linux/Unix command
- How to go to the End of File in Nano Editor
- How to tar.gz a directory or folder Command
- How to remove/delete a directory in Linux/macOs
- How to Display content of a file in Terminal Screen?
- ls command sort by file size [Linix/UNIX/macOS/bash]
- How to Copy Entire Directory to another Directory in Linux
- 3 ways to clear screen on Linux Terminal
- Command to check Last Login or Reboot History of Users and TTYs
- How to check uptime of Linux/Unix/macOS system/server using console command?
- How to backup a file in Linux/Unix
- Command to know the installed Debian version?
- Check SSH/OpenSSH version Command
- 3 Commands to stop Nginx Server
- Linux: Create a New User and Password and Login Example
- How to install npm on Ubuntu
- How to List All Users in Linux
- How to Get the List of Shells on Linux
- Fix - ssh-copy-id no identities found error
- Installing and using unzip Command to unzip a zip file using Terminal
- scp: ssh: connect to host xxxx port 22: Connection refused Error
- zsh hello world example
More Posts:
- Convert JSON to XML using Java Jackson Library - Java
- How to: Docker Remove Image - Docker
- Duplicate id @+id/textView1, already defined earlier in this layout Android Error - Android
- How to add hint text in bootstrap input text field and text area - Bootstrap
- How to reset eclipse layout - Android
- 9 Border to DIV Element in HTML Examples - Html
- Keyboard shortcuts to format Source code in Microsoft Visual Studio Code IDE - Microsoft
- How to Sync Microsoft Teams Calendar with Mac Calendar - Microsoft