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,
- Rename a directory using Linux/Unix command
- ls command to list only directories
- How to Restart or Reload Nginx Server Service on Linux
- 3 ways to clear screen on Linux Terminal
- ls command: sort files by name alphabetically A-Z or Z-A [Linux/Unix/macOS/Bash]
- Copy entire directory using Terminal Command [Linux, Mac, Bash]
- Fix: sudo: unable to open Read-only file system
- Create Hidden File or Directory using Shell Command
- Command to know the installed Debian version?
- The Date Command and its usage [Linux - Unix - macOS]
- Fix - bash: man: command not found
- How to tar.gz a directory or folder Command
- How to Display content of a file in Terminal Screen?
- How to change bash terminal prompt string and color
- Sort ls command by last modified date and time
- Execute .bin and .run file Ubuntu Linux
- zsh hello world example
- How to check uptime of Linux/Unix/macOS system/server using console command?
- [Fix] Linux - bash: useradd: command not found
- Command to check Last Login or Reboot History of Users and TTYs
- How to install and Configure sar sysstat tools in Ubuntu Linux
- How to use SCP Command to Copy Directory
- Linux Remove or Delete Files and Directories using Terminal Commands
- How to connect to SSH port other than default 22
- Install OpenSSL on Linux/Ubuntu
More Posts:
- Jupyter Notebook: 500 Internal Server Error - nbconvert failed: xelatex not found on PATH - Python
- Convert String Date to Date Object in Java - Java
- How to turn off Stage Manager - macOS Ventura - MacOS
- Read a file line by line in Python Program - Python
- JDBCTemplate Querying Examples with Spring Boot 3 - Java
- Display ls command file sizes in KB (kilobytes) MB (megabytes) or GB (gigabytes) [Linux/macOS] - MacOS
- Change Title text for Android Activity using java code - Android
- Java JDBC Connection with Database using SSL (https) URL - Java