In this article we will be seeing how to sort Files and Directory in a Linux system. We will be doing with a single command ls and with different flags.
The ls command with no flag lists the files and directory on the current working directory. With l alone the directory will end with /.
$ ls
Listing the files and directories in Ascending order:
The default command itself list the files and directory in an ascending order,
$ ls
ljunix@ljunix:~$ ls 2.png Documents emoj.conf Games image1.jpg mozilla.pdf Pictures Templates test.sh Videos Desktop Downloads example.php host.txt image-nic.png Music snap Test.conf test.txt
$ l
ljunix@ljunix:~$ l 2.png Documents/ emoj.conf Games/ image1.jpg mozilla.pdf Pictures/ Templates/ test.sh Videos/ Desktop/ Downloads/ example.php host.txt image-nic.png Music/ snap/ Test.conf test.txt
Listing hidden files:
Some files maybe hidden in a directory. These files are started with a period. To list such files we gonna use a flag -a along with ls. ( ll command do the same thing).
$ ls -a
ljunix@ljunix:~$ ls -a . 2.png Documents emoj.conf Games image1.jpg mozilla.pdf Pictures Templates test.sh test.txt .. Desktop Downloads example.php host.txt image-nic.png Music snap Test.conf .test.sh.swp Videos
To list the hidden files with their properties,
$ ls -la
ljunix@ljunix:~$ ls -la total 60 drwxr-xr-x 11 ljunix ljunix 4096 Nov 11 11:09 ./ drwxrwxrwt 14 root root 20480 Nov 11 11:11 ../ -rw-r–r– 1 ljunix ljunix 0 Nov 11 11:05 2.png drwxr-xr-x 2 ljunix ljunix 4096 Nov 11 10:45 Desktop/ drwxr-xr-x 2 ljunix ljunix 4096 Nov 11 10:45 Documents/ drwxr-xr-x 2 ljunix ljunix 4096 Nov 11 10:45 Downloads/ -rw-r–r– 1 ljunix ljunix 0 Nov 11 11:06 emoj.conf -rw-r–r– 1 ljunix ljunix 0 Nov 11 11:05 example.php drwxr-xr-x 2 ljunix ljunix 4096 Nov 11 10:46 Games/ -rw-r–r– 1 ljunix ljunix 0 Nov 11 11:05 host.txt -rw-r–r– 1 ljunix ljunix 0 Nov 11 11:04 image1.jpg -rw-r–r– 1 ljunix ljunix 0 Nov 11 11:05 image-nic.png -rw-r–r– 1 ljunix ljunix 0 Nov 11 10:47 mozilla.pdf drwxr-xr-x 2 ljunix ljunix 4096 Nov 11 10:46 Music/ drwxr-xr-x 2 ljunix ljunix 4096 Nov 11 10:46 Pictures/ drwxr-xr-x 2 ljunix ljunix 4096 Nov 11 10:46 snap/ drwxr-xr-x 2 ljunix ljunix 4096 Nov 11 10:46 Templates/ -rw-r–r– 1 ljunix ljunix 0 Nov 11 10:47 Test.conf -rw-r–r– 1 ljunix ljunix 0 Nov 11 10:47 test.sh -rw-r–r– 1 ljunix ljunix 0 Nov 11 11:09 .test.sh.swp -rw-r–r– 1 ljunix ljunix 0 Nov 11 10:47 test.txt drwxr-xr-x 2 ljunix ljunix 4096 Nov 11 10:46 Videos/ -rw——- 1 root root 11627 Nov 10 09:01 .viminfo -rw——- 1 ljunix ljunix 50 Nov 11 10:24 .Xauthority -rw——- 1 ljunix ljunix 3377 Nov 11 10:24 .xsession-errors -rw——- 1 ljunix ljunix 3377 Nov 10 21:03 .xsession-errors.old
Listing files with properties:
To view the files permission with modified date we can use the flag -l . This will list the files with file mode, User and group, File-size (in bytes), Modified time, Filename.
$ls -l
ljunix@ljunix:~$ ls -la total 60 -rw-r–r– 1 ljunix ljunix 0 Nov 11 11:05 2.png drwxr-xr-x 2 ljunix ljunix 4096 Nov 11 10:45 Desktop/ drwxr-xr-x 2 ljunix ljunix 4096 Nov 11 10:45 Documents/ drwxr-xr-x 2 ljunix ljunix 4096 Nov 11 10:45 Downloads/ -rw-r–r– 1 ljunix ljunix 0 Nov 11 11:06 emoj.conf -rw-r–r– 1 ljunix ljunix 0 Nov 11 11:05 example.php drwxr-xr-x 2 ljunix ljunix 4096 Nov 11 10:46 Games/ -rw-r–r– 1 ljunix ljunix 0 Nov 11 11:05 host.txt -rw-r–r– 1 ljunix ljunix 0 Nov 11 11:04 image1.jpg -rw-r–r– 1 ljunix ljunix 0 Nov 11 11:05 image-nic.png -rw-r–r– 1 ljunix ljunix 0 Nov 11 10:47 mozilla.pdf drwxr-xr-x 2 ljunix ljunix 4096 Nov 11 10:46 Music/ drwxr-xr-x 2 ljunix ljunix 4096 Nov 11 10:46 Pictures/ drwxr-xr-x 2 ljunix ljunix 4096 Nov 11 10:46 snap/ drwxr-xr-x 2 ljunix ljunix 4096 Nov 11 10:46 Templates/ -rw-r–r– 1 ljunix ljunix 0 Nov 11 10:47 Test.conf -rw-r–r– 1 ljunix ljunix 0 Nov 11 10:47 test.sh -rw-r–r– 1 ljunix ljunix 0 Nov 11 10:47 test.txt drwxr-xr-x 2 ljunix ljunix 4096 Nov 11 10:46 Videos/
Listing the last accessed file first:
The flag with -t sorts the last accessed files.
$ ls -t
ljunix@ljunix:~$ ls -t emoj.conf 2.png example.php mozilla.pdf test.sh Games Pictures Templates Documents Desktop host.txt image-nic.png image1.jpg Test.conf test.txt Music snap Videos Downloads
To list the last accessed file with the accessed time use -lt flag.
Listing the file size:
To list the file and directory with its size use -sh ( -s alone do the trick but it list the size in bytes. For human understanding we can use -h along with that).
$ ls -sh
ljunix@ljunix:~$ ls -sh 32K 2.png 11M Documents 4.0K emoj.conf 2.5G Games 32K image1.jpg 4.0K mozilla.pdf 1.6G Pictures 28K Templates 4.0K test.sh 1.6G Videos 15M Desktop 36K Downloads 4.0K example.php 4.0K host.txt 28K image-nic.png 3.6G Music 36K snap 4.0K Test.conf 4.0K test.txt
Listing the files in reverse order:
We can use flag -r to list the files in reverse order.
$ ls -r
ljunix@ljunix:~$ ls -r Videos test.sh Templates Pictures mozilla.pdf image1.jpg Games emoj.conf Documents 2.png test.txt Test.conf snap Music image-nic.png host.txt example.php Downloads Desktop
Finding the number of files in a directory:
To find the number of files in a directory we can use word count command wc .
$ ls | wc -l
ljunix@ljunix:~$ ls | wc -l 19
Listing particular files:
For listing particular file either by its file type or name we can use the grep command. We will see more about grep on upcoming article.
$ ls | grep txt
ljunix@ljunix:~$ ls | grep txt host.txt test.txt
$ ls | grep D
ljunix@ljunix:~$ ls | grep D Desktop Documents Downloads
Hope this article helps you. Feel free to ask if you have any questions.

Leave a comment