Untitled Document
ls Command

ls command is used for listing files and directories from adirectory . The command is used frequently and has very useful options for listing files information .Lets see some most important options that we need during listing files .

Example 1 -

Run the ls command without any option. The command will display only the list of files and its sub-directories from current directory . By default files with numerals are displayed first , second uppercase and then lowercase .


Example 2 - Display the listing of files with more details ( option -l )

Sometimes , just dispalying a list of filenames isn't enough , we need to know more about these files . For that to happen , ls has to be used with an option -l .
   ls -l   

Example 3 - Listing all the .txt files form a directory

We have seen one special character of regular expression * and we will use this with ls to get above result . So our command will be like -
  ls -l *.txt 

Example 4 - Displaying ls output in Multiple Colummns -

By default ls prints its output in single column , when we have several files , its better to display the filenames in multiple columns , to do this run ls with -x option.
   ls -lx

Example 5 - How to identify Executable and directories from ls output

So far we have seen that output of ls doesn't give us idea whether the file is executable or a directory file . ls have an option -F which puts * and / as type indicators . * indicates file is executable and / refers to a directory . Now lets combine the option with abbove command and see the result .

   ls -lxF

Example 6 - Displaying / listing hidden files from a directory

ls doesn't normally show all files in a directory . There are certain hidden files ( filenames begining with . ) which are not shown by ls . The -a option lists all hidden files as well .

   ls -laxF

Example 7 - Displaying / Listing files in reverse order .

We can display the file listing in reverse order , use -r ( reverse) option to sort files in reverse order .

   ls -lr

Example 8 - Sorting files with last modification time

Sometime we just want to see the recently added files or log files to current directory . The files can be listed based on the modification time use -t option and files will be sorted with last modification time . The below command sort files in reverse order based on modification time i.e file having latest modification time will be dispalyed at the end ( last ).

 ls -lrt 

Example 9 - Sorting files with last access time

As dscussed in above example like modification time we can sort files with last access time as well . Use -u option with ls .

   ls -lut 

Example 10 - Listing Directory Contents

To lsit the contents from directory run ls command with above disccussed options along with directory name as an argument . To display the contents from my_progs and basic_example directories , run ls command as -

   ls my_progs basic_example

Example 11 - Recursive Listing of Directory Contents ( -R)

The -R ( recursive ) option lists all the files and sub-directories in a directory tree.

   ls -xR my_progs basic_example
Untitled Document