Sunday, June 12, 2022

ls command with examples

ls command with examples

ls command with examples



if you want learn to Basic Unix Commands in 1 Hour, here is the link

Basic Unix Commands in 1 Hour

if you want learn Unix/Linux Commands in detail, here is the link

Learn Unix/Linux Commands in detail

Also keep visiting my blog to learn more

unixtechworld.blogspot.com

ls command is one of the most widely used command on unix,linux platform system, as name suggests ls is used for listing files and directories, but apart from listing it gives a lot of information, lets explore this command.





1. use 'ls' without anything ls you will get a list of files and directories


neeraj@ubuntu:~/unixtechworld_Workshop$ ls
Hello_World_Programs Java.txt Linux_Distro.txt Python.txt

2. Using 'ls' with variety of wildcards makes it very useful, in below example we have listed
a) files/directories names starting with 'J' ( J followed by * )
b) files/directories names ending with 'txt' ( * followed by J )
c) files/directories names ending with 'txt' and 'o' coming anywhere before that ( *o*txt )


neeraj@ubuntu:~/unixtechworld_Workshop$ ls J*
Java.txt
neeraj@ubuntu:~/unixtechworld_Workshop$ ls *txt
Java.txt Linux_Distro.txt Python.txt
neeraj@ubuntu:~/unixtechworld_Workshop$ ls *o*txt
Linux_Distro.txt Python.txt
neeraj@ubuntu:~/unixtechworld_Workshop$

3. ls has a very useful option ( or argument ) called "-l" , l is for long listing, look carefully at below example, each column of output gives you following information
(I) permssions and file type ( plain file / directory etc )
(II) number of hard links
(III) owner
(IV) group
(V) file size
(VI) , (VII) and (VIII) Date, timeamp of file
(IX) filename


neeraj@ubuntu:~/unixtechworld_Workshop$ ls -l
total 16
drwxrwxr-x 2 neeraj neeraj 4096 May 27 11:37 Hello_World_Programs
-rw-rw-r-- 1 neeraj neeraj 61 Mar 6 12:08 Java.txt
-rw-rw-r-- 1 neeraj neeraj 64 May 27 11:30 Linux_Distro.txt
-rw-rw-r-- 1 neeraj neeraj 29 Mar 6 12:07 Python.txt

4. You can '-t' with '-l' to sort file list with time stamp ( modification time )


neeraj@ubuntu:~/unixtechworld_Workshop$ ls -lt
total 16
drwxrwxr-x 2 neeraj neeraj 4096 May 27 11:37 Hello_World_Programs
-rw-rw-r-- 1 neeraj neeraj 64 May 27 11:30 Linux_Distro.txt
-rw-rw-r-- 1 neeraj neeraj 61 Mar 6 12:08 Java.txt
-rw-rw-r-- 1 neeraj neeraj 29 Mar 6 12:07 Python.txt

5. Adding '-r' along with '-l' can help you sort files in reverse order ( old files first )


neeraj@ubuntu:~/unixtechworld_Workshop$ ls -ltr
total 16
-rw-rw-r-- 1 neeraj neeraj 29 Mar 6 12:07 Python.txt
-rw-rw-r-- 1 neeraj neeraj 61 Mar 6 12:08 Java.txt
-rw-rw-r-- 1 neeraj neeraj 64 May 27 11:30 Linux_Distro.txt
drwxrwxr-x 2 neeraj neeraj 4096 May 27 11:37 Hello_World_Programs

6. using '-h' option along with '-l' will give you file size in human readable format ( notice size 4.0 k )


neeraj@ubuntu:~/unixtechworld_Workshop$ ls -lh
total 20K
drwxrwxr-x 2 neeraj neeraj 4.0K May 29 06:08 Hello_World_Programs
-rw-rw-r-- 1 neeraj neeraj 61 Mar 6 12:08 Java.txt
-rw-rw-r-- 1 neeraj neeraj 64 May 27 11:30 Linux_Distro.txt
-rw-rw-r-- 1 neeraj neeraj 29 May 29 05:37 MyFirstFile.txt
-rw-rw-r-- 1 neeraj neeraj 29 Mar 6 12:07 Python.txt

7. using '-d' can help you list directories

neeraj@ubuntu:~/unixtechworld_Workshop$ ls -d Hello_World_Programs
Hello_World_Programs

8. using '-R' can help you list directories recursively


neeraj@ubuntu:~/unixtechworld_Workshop$ ls -R Hello_World_Programs
Hello_World_Programs:
JavaScript.txt Kotlin.txt

9. you can use '-a' ( all )option to list hidden files, notice '.HideMe' file


neeraj@ubuntu:~/unixtechworld_Workshop$ ls -la
total 28
drwxrwxr-x 4 neeraj neeraj 4096 May 27 12:31 .
drwxr-xr-x 16 neeraj neeraj 4096 Mar 6 12:05 ..
drwxrwxr-x 2 neeraj neeraj 4096 May 27 11:37 Hello_World_Programs
drwxrwxr-x 2 neeraj neeraj 4096 May 27 12:31 .HideMe
-rw-rw-r-- 1 neeraj neeraj 61 Mar 6 12:08 Java.txt
-rw-rw-r-- 1 neeraj neeraj 64 May 27 11:30 Linux_Distro.txt
-rw-rw-r-- 1 neeraj neeraj 29 Mar 6 12:07 Python.txt

10. you can use '-A' ( almost all )option to list hidden files except current and parent directory ( . and .. )


neeraj@ubuntu:~/unixtechworld_Workshop$ ls -lA
total 20 drwxrwxr-x 2 neeraj neeraj 4096 May 27 11:37 Hello_World_Programs
drwxrwxr-x 2 neeraj neeraj 4096 May 27 12:31 .HideMe
-rw-rw-r-- 1 neeraj neeraj 61 Mar 6 12:08 Java.txt
-rw-rw-r-- 1 neeraj neeraj 64 May 27 11:30 Linux_Distro.txt
-rw-rw-r-- 1 neeraj neeraj 29 Mar 6 12:07 Python.txt

11. you can use '-S' option to sort output based on file size

neeraj@ubuntu:~/unixtechworld_Workshop$ ls -lS
total 16
drwxrwxr-x 2 neeraj neeraj 4096 May 27 11:37 Hello_World_Programs
-rw-rw-r-- 1 neeraj neeraj 64 May 27 11:30 Linux_Distro.txt
-rw-rw-r-- 1 neeraj neeraj 61 Mar 6 12:08 Java.txt
-rw-rw-r-- 1 neeraj neeraj 29 Mar 6 12:07 Python.txt

12. You can use '-f' option to print file list without any sorting

neeraj@ubuntu:~/unixtechworld_Workshop$ ls -f
. Linux_Distro.txt .. .HideMe Hello_World_Programs Java.txt Python.txt

13. here is an example of ls with wild card mask, we have lsited any filename which is 8 character long

neeraj@ubuntu:~/unixtechworld_Workshop$ ls ????????
Java.txt

14. In below example we have listed any filename which starts by any character followed by any one of three characters 'x','y','z' and after that any number of characters

neeraj@ubuntu:~/unixtechworld_Workshop$ ls ?[x-z]*
Python.txt

15. you can also list the content of current directory by using 'ls .' , since . ( single dot represent current directory )

neeraj@ubuntu:~/unixtechworld_Workshop$ ls .
Hello_World_Programs Java.txt Linux_Distro.txt Python.txt

16. you can list the content of parent directory by using 'ls ..' , since .. ( double dot represent current directory )

neeraj@ubuntu:~/unixtechworld_Workshop$ ls ..
Desktop Documents Downloads Music Pictures Public Templates unixtechworld_Workshop Videos WorkShop

17. you can list the content of child directory by using 'ls */*', this will include all child directories which are in same hierarchy

neeraj@ubuntu:~/unixtechworld_Workshop$ ls */*
Hello_World_Programs/JavaScript.txt Hello_World_Programs/Kotlin.txt

18. if you want to list conent of specific child directory ( for example 'Hello_World_Programs' ) you can do it like below

neeraj@ubuntu:~/unixtechworld_Workshop$ ls Hello_World_Programs/*
Hello_World_Programs/JavaScript.txt Hello_World_Programs/Kotlin.txt





if you want learn to Basic Unix Commands in 1 Hour, here is the link

Basic Unix Commands in 1 Hour

if you want learn Unix/Linux Commands in detail, here is the link

Learn Unix/Linux Commands in detail

Also keep visiting my blog to learn more

unixtechworld.blogspot.com

No comments:

Post a Comment