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,
- Command to know the Available Memory on Linux System
- How to install curl on Alpine Linux
- How to backup a file in Linux/Unix
- Install Java Runtime Environment (Oracle or open JRE) on Ubuntu
- What is the Default Admin user and Password for Jenkins
- How to tar.gz a directory or folder Command
- Copy entire directory using Terminal Command [Linux, Mac, Bash]
- Fix: bash: ipconfig: command not found on Linux
- Command to check Last Login or Reboot History of Users and TTYs
- Linux: Create a New User and Password and Login Example
- ls command to list only directories
- bash: cls: command not found
- How to exit from nano command
- Installing and using unzip Command to unzip a zip file using Terminal
- What does apt-get update command does?
- ls command: sort files by name alphabetically A-Z or Z-A [Linux/Unix/macOS/Bash]
- How to remove or uninstall Java from Ubuntu using apt-get
- scp: ssh: connect to host xxxx port 22: Connection refused Error
- Sort ls command by last modified date and time
- Create Nested Directories using mkdir Command
- How to Exit a File in Terminal (Bash/Zsh)
- Command to know the installed Debian version?
- How to connect to SSH port other than default 22
- How to save a file in Nano Editor and Exit
- Install OpenSSL on Linux/Ubuntu
More Posts:
- How to create alias in macOS - MacOS
- Sharepoint Server 2016 installation Prerequisites with download links - SharePoint
- Multiple ways to Convert tabs with spaces in Notepad++ - NotepadPlusPlus
- How to Auto Fill Down in Excel for Mac - MacOS
- Save webpage as pdf in Google Chrome for Mac OS X - Mac-OS-X
- Nano Undo/Redo Keyboard Shortcuts - Linux
- How to Save Jupyter Notebook as PDF - Python
- Mac: How to Open Android Studio from Terminal - MacOS