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
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).
Comments