Sunday, 29 January 2012


ls command is used to list the contents off the writes to standard output
all  the information of the files, directories or specifically  of the parameters passed to it.
You can display these contents in the alphabetically sorted order, or list only particular  files  in
Combination with other command. There are many options with ls which you  may hardly use in normal cases.
Here ,only few frequently used options are discussed.

Example 1)
To simply display the names of all the files in a directory.
/home/script-user$ ls

Example  2)
To display all the files that  have a common  pattern.
/home/script-user/items $ ls table_*.log

Example 3)
 To  get a long listing of files which contains all the details such as file size, permissions,
Time stamp, user details sorted by the modification time, use ls as follows.

/home/script-user $ ls  –lrt   
-rw-r--r--    1  script-user  Tools    56344   Dec 16     14:37 Joke-and-fun.xt
-rw-r--r--    1  script-user  Tools   105977 Dec 16     15:52
 drw-r--r--  2  script-user  Tools      256      Dec 18      21:01 Mail-file
-rw-r--r--    1  script-user  Tools      740      Dec 20      01:03 Rollercoaster
-rw-r--r--    1  script-user  Tools       58        Dec 24     00:40 Jackson_note.tar

The first field here shows file permissions, which are discussed in chmod,
2nd field is the type of element,(1 for file 2 for directory..etc)
3rd field gives the owner of the element(file,directory)
4th field gives the group which the user belongs to.
5th field gives the size.
6th,7th and 8th field-modification times.
9 th field gives the name of the element.

You can get any field of the output of ls –lrt using awk.
For eg: To  get the name of the element from the output of ls –lrt, use awk as follows
/home/script-user $ ls  -lrt  |  awk  ‘{print  $9}’
Example 4)
To display only the directories in a path, use the output of  ls –lrt  as follows.
/home/script-user $ ls  -lrt  |  grep “^d”
 drw-r--r--  2  script-user  Tools       256      Dec 18      21:01 Mail-file

The  ‘^’  character represents  a starting character of a line( a ‘$’ represents end), in fact  it’s not physically seen, but an  interpreted character.
In  this case grep searched for the  lines starting with  a  ‘d’ which stands for directories.

Example 5)
 you want to list the directories whose name has a common pattern, use ls  –ld  <pattern>
home/script-user/ebooks $ls   -ld  j*
drwxr-xr-x    2 script-user  Tools        256      Nov  9 12:10  j2me
drwxr-xr-x    2 script-user  Tools    20480     Nov  9 12:10  java
drwxr-xr-x    2 script-user  Tools     4096      Nov  9 12:10  java scripts

Suppose you want to view all the files of the directory  java  use  ls –lrt  java instead of doing  both cd java and ls –lrt.

Example  6)
To  make  a case insensitive list on a filename parameter, use ls along with grep as follows.
home/script-user/docs$ ls   report*

home/script-user/docs$ls  |  grep  –i   report

The above command is equivalent to  ls  *report*  *Report*   *REPORT*  *Report*………..and so on till all the combination of the word report in Ucase and Lcase.

Example 7)
To  list all the directories in a directory, and its subdirectories, use ls –R
In this case ls lists all the files inside the directories  starting with j recursively in all its subdirectories.
home/script-user/ebooks $ ls  -R  j*
j2me - the complete reference.pdf  j2me---the-complete-reference.htm

java:<----------------------------     directory
 java The J2EE Tutorial (Addison Wesley & Sun, 2002).pdf
(Addison Wesley 2002) - Java Network Programming and Distributed Computing.pdf
(Ebook) Java - Borland Jbuilder - Developing Database Applications - Inprise.pdf
(OReilly) Java Network Programming ,2nd Ed.pdf
(OReilly) Java Server Pages ,2nd Ed.pdf

java scripts:

[Java Script] O'Reilly- JavaScript The Definitive Guide.pdf

java scripts/newdir:<-------------  subdirectory
[Java Script] O'Reilly - JavaScript The Definitive Guide 2ed.pdf

Example  8)
ls  by default does not show hidden files and directories. To show hidden files use –a option.
Eg: files like  .profile, .sh_history etc are hidden files. if  have not used –a option you wont be
 able to see these files.
In all the directories , there are 2 common hidden directories  .(a dot)  which represents the current directory and  ..( 2 dots) represents the parent of the current directory

No comments:

Post a Comment