Friday, 2 August 2019

Linux - Ls command

Ls command is used to list the files and subdirectories under a specific directory. It is one among the regularly used commands while doing administration of a server.

Syntax: 
ls
ls -l
ls -ltr

Above given are a couple of options which are most regularly used with ls command.
We will see some ls practical examples below for better understanding of it.

Example-1:
Listing the files and directories:
By typing normal ls command on the terminal window, will list all of the files and subdirectories under a directory.
Sample Usage case,
Assume that we have a software package unzipped on Linux/Unix flavor machine. Now, to see what are the files and sub-directories are available within that we can use this command.

We can take a numerous number of examples for “ls command”, above is just a sample case. Normal “ls command” output is,
[root@sys1 repo]# ls
EFI         media.repo                       RELEASE-NOTES-or-IN.html
EULA        Packages                         RELEASE-NOTES-pa-IN.html
EULA_de     README                           RELEASE-NOTES-pt-BR.html
EULA_en     RELEASE-NOTES-as-IN.html         RELEASE-NOTES-ru-RU.html
EULA_es     RELEASE-NOTES-bn-IN.html         RELEASE-NOTES-si-LK.html

Example-2:
The long listing of files and directories:
From the normal “ls command”, we could see the only list of things available but it won’t show up more details about it. Such as,
Whether it is a file or directory?
Who are the owner & group ownership of that file or directory?
What are the files permissions level and last modified dates? etc
We can see all of the above information from using “ls -l” command option.
Syntax:
ls -l

A sample command output is given below,
[root@sys1 repo]# ls -l
total 3120
-r--r--r--. 1    root   root    11414   Jan  31   2013   TRANS.TBL
dr-xr-xr-x. 3    root   root    4096    Jan  31   2013    Server
dr-xr-xr-x. 3    root   root    4096    Jan  31   2013    ScalableFileSystem
-r--r--r--. 1    root   root    3211    Jan  29   2013    RPM-GPG-KEY-redhat-release
-r--r--r--. 1    root   root    3375    Jan  29   2013    RPM-GPG-KEY-redhat-beta
dr-xr-xr-x. 3    root   root    4096    Jan  31   2013    ResilientStorage
drwxr-xr-x. 2    root   root    4096    Feb   8   13:50   repodata

From “ls -l” command output we can make out,
Starting from left,
The first character explains whether it is a file or directory.
Next, 9 characters explain what permission levels are for the user, primary group and others in sequence.
Next character talks about file inode information.
Who are the primary owner and group owner of the files is explained?
File size and next time/date of file modified information.

Also, try,
ls -m
ls -x

Example-3:
Files with reverse order:
To display the files in reverse chronological order. We can use “-r” option with ls command.
Syntax:
Is-Ir

Note: 
“-r” option should always use with “-l” option for better results.

One can observe that based on the file alphabetical order we can see the files organized. Sample output is given below.
[root@sys1 repo]# ls -l
total 3120
-r--r--r--. 1    root    root      11414     Jan 31    2013   TRANS.TBL
dr-xr-xr-x. 3    root    root       4096     Jan 31    2013   Server
dr-xr-xr-x. 3    root    root       4096     Jan 31    2013   ScalableFileSystem
-r--r--r--. 1    root    root       3211     Jan 29    2013   RPM-GPG-KEY-redhat-release
-r--r--r--. 1    root    root       3375     Jan 29    2013   RPM-GPG-KEY-redhat-beta
dr-xr-xr-x. 3    root    root       4096     Jan 31    2013   ResilientStorage
drwxr-xr-x. 2    root    root       4096     Feb 8     13:50  repodata

Example-4:
Files and directories with the timestamp and reverse timestamp:
For displaying the files and directories based on timestamp, we can use “-t” option with ls command.
Syntax:
Ls -lt

Sample command output is given below,
[root@sys1 repo]# ls -lt
total 3120
drwxr-xr-x. 2        root   root      4096   Feb 8   13:50  repodata
drwx------. 2        root   root     16384   Feb 2   12:01  lost+found
-r--r--r--. 1        root   root     11414   Jan 31  2013   TRANS.TBL
dr-xr-xr-x. 3        root   root      4096   Jan 31  2013   images
dr-xr-xr-x. 2        root   root    253952   Jan 31  2013   Packages
dr-xr-xr-x. 3        root   root      4096   Jan 31  2013   HighAvailability
dr-xr-xr-x. 3        root   root      4096   Jan 31  2013   LoadBalancer

We can combine -r option with -t timestamp, for files and directories in reverse timestamp.
Syntax:
ls -ltr
ls -l -t -r

This is most used in day to day life because it gives the recent modified files and directories information in the top order. Keep note of this for sure.
Sample ls -ltr command output is,
[root@sys1 repo]# ls -ltr
total 3120
drwxr-xr-x. 2    root   root     4096     Feb 8     13:50  repodata
drwx------. 2    root   root    16384     Feb 2     12:01  lost+found
-r--r--r--. 1    root   root    11414     Jan 31    2013   TRANS.TBL
dr-xr-xr-x. 3    root   root     4096     Jan 31    2013   images
dr-xr-xr-x. 2    root   root   253952     Jan 31    2013   Packages
dr-xr-xr-x. 3    root   root     4096     Jan 31    2013   HighAvailability
dr-xr-xr-x. 3    root   root     4096     Jan 31    2013   LoadBalancer

Example-5:
Files and directories with human readable size format:
As we have discussed, ls -l command will display the files and directories size information also but they are in blocks format. We need to convert them into kilo bytes and Mega bytes etc to understand clearly. 
Instead, we can use “-h” option with ls command to display the sizes in human readable format.
Syntax:
ls -lh

sample command output is given below,
[root@sys1 repo]# ls -lh
total 3.1M
dr-xr-xr-x. 3          root    root    4.0K    Jan 31     2013    EFI
lrwxrwxrwx. 1          root    root       7    Jan 31     2013    EULA -> EULA_en
-r--r--r--. 1          root    root      11K   Nov 7      2012    EULA_de
-r--r--r--. 1          root    root     8.6K   Nov 7      2012    EULA_en
-r--r--r--. 1          root    root      11K   Nov 7      2012    EULA_es
-r--r--r--. 1          root    root      11K   Nov 7      2012    EULA_fr
-r--r--r--. 1          root    root      11K   Nov 7      2012    EULA_it
-r--r--r--. 1          root    root      13K   Nov 7      2012    EULA_ja
-r--r--r--. 1          root    root     9.7K   Nov 7      2012    EULA_ko
-r--r--r--. 1          root    root     9.8K   Nov 7      2012    EULA_pt
-r--r--r--. 1          root    root     7.2K   Nov 7      2012    EULA_zh

Let’s remove the -l option and observe the output,
Ls -h command output here….
Similar to above we can try,
ls -s To display file sizes in bytes
ls -k To display file sizes in kilo bytes
ls -m To display file sizes in mega bytes

By using ls –s command we can do this.
[root@sys1 repo]# ls -s
total 3120
4    EFI 92                RELEASE-NOTES-fr-FR.html
0    EULA 80               RELEASE-NOTES-gu-IN.html
12   EULA_de 136           RELEASE-NOTES-hi-IN.html
12   EULA_en 84            RELEASE-NOTES-it-IT.html
12   EULA_es 100           RELEASE-NOTES-ja-JP.html
12   EULA_fr 156           RELEASE-NOTES-kn-IN.html
12   EULA_it 88            RELEASE-NOTES-ko-KR.html
16   EULA_ja 164           RELEASE-NOTES-ml-IN.html
12   EULA_ko 140           RELEASE-NOTES-mr-IN.html

Example -6:
Listing the hidden files and directories:
Regular ls command will not display any hidden files and sub directories under a directory. Let’s first understand,
what is a hidden file or directory? And Why we need that in the first place?
In a day to day activities, we might want to hide critical/important files and directories from the regular user. This is just to avoid any accidental changes to those files etc.
for example, some application specific configuration files.
To hide a file or directory, we need to use “.” Before file/directory name.
Example, .profile, .bash_profile etc
To view these hidden files and directories, we can use “-a” option with ls command.
Syntax:
ls -a
ls -la
ls -lat

Sample command output is given below,
[root@sys1 repo]# ls -a
. lost+found  RELEASE-NOTES-or-IN.html
.. media.repo RELEASE-NOTES-pa-IN.html
EFI Packages  RELEASE-NOTES-pt-BR.html
EULA README   RELEASE-NOTES-ru-RU.html

Example-7:
Listing out inode numbers with files and directories:
If you are new to inode numbers, then visit this, what are inode numbers? How are they associated with files and directories structure?
Syntax:
ls -i ls -li

A usage case,
When do we check inode numbers for files?
Practical command output is given below,
By using ls –I command we can do this
 [root@sys1 repo]# ls -i
128001     EFI 3780           RELEASE-NOTES-fr-FR.html
12     EULA 3781              RELEASE-NOTES-gu-IN.html
13     EULA_de 3782           RELEASE-NOTES-hi-IN.html
14     EULA_en 3783           RELEASE-NOTES-it-IT.html
15     EULA_es 3784           RELEASE-NOTES-ja-JP.html
16     EULA_fr 3785           RELEASE-NOTES-kn-IN.html
17     EULA_it 3786           RELEASE-NOTES-ko-KR.html
18     EULA_ja 3787           RELEASE-NOTES-ml-IN.html
19     EULA_ko 3788           RELEASE-NOTES-mr-IN.html
20     EULA_pt 3789           RELEASE-NOTES-or-IN.html
21     EULA_zh 3790           RELEASE-NOTES-pa-IN.html

0 comments:

Post a Comment