Linux ls Command

Basic ls Command Examples in Linux

basic_ls_command_examples_in_linux

ls command is one of the most frequently used command in Linux.

We use ls command daily basis and frequently even though we may not aware and never use all the ls option available.

  1. List Files using ls with no option

    ls with no option list files and directories in bare format where we won't be able to view details like file types, size, modified date and time, permission and links etc.

    # ls
    
    dos      file10   file3    file6    file8    hello.c
    file1    file2    file4    file7    file9
    
  2. List Files With option -l

    Here, ls -l (-l is character not one) shows file or directory, size, modified date and time, file or folder name and owner of file and its permission.

    # ls -l
    
    total 8
    drwxr-xr-x    3 root     root           163 Aug 21  2011 dos
    -rw-r--r--    1 root     root             0 Feb  2 22:39 file1
    -rw-r--r--    1 root     root             0 Feb  2 22:56 file10
    -rw-r--r--    1 root     root             0 Feb  2 22:39 file2
    -rw-r--r--    1 root     root             0 Feb  2 22:39 file3
    -rw-r--r--    1 root     root             0 Feb  2 22:56 file4
    -rw-r--r--    1 root     root             0 Feb  2 22:56 file6
    -rw-r--r--    1 root     root             0 Feb  2 22:56 file7
    -rw-r--r--    1 root     root             0 Feb  2 22:56 file8
    -rw-r--r--    1 root     root             0 Feb  2 22:56 file9
    -rw-r--r--    1 root     root           242 Jul 15  2017 hello.c
    
  3. View Hidden Files

    List all files including hidden file starting with ‘.‘.

    # ls -a
    
    .            .xsession    file10       file4        file8
    ..           dos          file2        file6        file9
    .Xauthority  file1        file3        file7        hello.c
    
  4. List Files with Human Readable Format with option -lh

    With combination of -lh option, shows sizes in human readable format.

    # ls -lh
    
    total 8
    drwxr-xr-x    3 root     root         163 Aug 21  2011 dos
    -rw-r--r--    1 root     root           0 Feb  2 22:39 file1
    -rw-r--r--    1 root     root           0 Feb  2 22:56 file10
    -rw-r--r--    1 root     root           0 Feb  2 22:39 file2
    -rw-r--r--    1 root     root           0 Feb  2 22:39 file3
    -rw-r--r--    1 root     root           0 Feb  2 22:56 file4
    -rw-r--r--    1 root     root           0 Feb  2 22:56 file6
    -rw-r--r--    1 root     root           0 Feb  2 22:56 file7
    -rw-r--r--    1 root     root           0 Feb  2 22:56 file8
    -rw-r--r--    1 root     root           0 Feb  2 22:56 file9
    -rw-r--r--    1 root     root         242 Jul 15  2017 hello.c
    
  5. List Files and Directories with ‘/’ Character at the end

    Using -F option with ls command, will add the ‘/’ Character at the end each directory.

    # ls -F
    
    dir1/    dos/     file10   file3    file6    file8    hello.c
    dir2/    file1    file2    file4    file7    file9
    
  6. List Files in Reverse Order

    The following command with ls -r option display files and directories in reverse order.

    # ls -r
    
    hello.c  file8    file6    file3    file10   dos      dir1
    file9    file7    file4    file2    file1    dir2
    
  7. Recursively list Sub-Directories

    ls -R option will list very long listing directory trees. See an example of output of the command.

    # ls -R
    
    .:
    dir1     dos      file10   file3    file6    file8    hello.c
    dir2     file1    file2    file4    file7    file9
     
    ./dir1:
     
    ./dir2:
     
    ./dos:
    asm-1.9     asm.com     debug.com   hpigot.com  hpigot.s
     
    ./dos/asm-1.9:
    Changelog    display.s    expr.s       lister.s     readme.txt   symtab.i
    asm.s        dos.i        input.s      message.s    support.s    symtab.s
    direct.s     equ.s        license.txt  output.s     symbols.s
    
  8. Reverse Output Order

    With combination of -ltr will shows latest modification file or directory date as last.

    # ls -ltr
    
    total 16
    drwxr-xr-x    3 root     root           163 Aug 21  2011 dos
    -rw-r--r--    1 root     root           242 Jul 15  2017 hello.c
    -rw-r--r--    1 root     root             0 Feb  2 22:39 file3
    -rw-r--r--    1 root     root             0 Feb  2 22:39 file2
    -rw-r--r--    1 root     root             0 Feb  2 22:39 file1
    -rw-r--r--    1 root     root             0 Feb  2 22:56 file9
    -rw-r--r--    1 root     root             0 Feb  2 22:56 file8
    -rw-r--r--    1 root     root             0 Feb  2 22:56 file7
    -rw-r--r--    1 root     root             0 Feb  2 22:56 file6
    -rw-r--r--    1 root     root             0 Feb  2 22:56 file4
    -rw-r--r--    1 root     root             0 Feb  2 22:56 file10
    drwxr-xr-x    2 root     root            37 Feb  2 23:03 dir2
    drwxr-xr-x    2 root     root            37 Feb  2 23:03 dir1
    
  9. Sort Files by File Size

    With combination of -lS displays file size in order, will display big in size first.

    # ls -lS
    
    total 16
    -rw-r--r--    1 root     root           242 Jul 15  2017 hello.c
    drwxr-xr-x    3 root     root           163 Aug 21  2011 dos
    drwxr-xr-x    2 root     root            37 Feb  2 23:03 dir1
    drwxr-xr-x    2 root     root            37 Feb  2 23:03 dir2
    -rw-r--r--    1 root     root             0 Feb  2 22:39 file1
    -rw-r--r--    1 root     root             0 Feb  2 22:56 file10
    -rw-r--r--    1 root     root             0 Feb  2 22:39 file2
    -rw-r--r--    1 root     root             0 Feb  2 22:39 file3
    -rw-r--r--    1 root     root             0 Feb  2 22:56 file4
    -rw-r--r--    1 root     root             0 Feb  2 22:56 file6
    -rw-r--r--    1 root     root             0 Feb  2 22:56 file7
    -rw-r--r--    1 root     root             0 Feb  2 22:56 file8
    -rw-r--r--    1 root     root             0 Feb  2 22:56 file9
    
  10. Display Inode number of File or Directory

    We can see some number printed before file / directory name. With -i options list file / directory with inode number.

    # ls -i
    
      11596 dir1       11595 file10     11591 file6        668 hello.c
      11597 dir2       11588 file2      11592 file7
         644 dos        11589 file3      11593 file8
      11587 file1      11590 file4      11594 file9
      
  11. Shows version of ls command

    Check version of ls command.

    # ls --version
    
    ls (GNU coreutils) 8.4
    Copyright (C) 2010 Free Software Foundation, Inc.
    License GPLv3+: GNU GPL version 3 or later .
    This is free software: you are free to change and redistribute it.
    There is NO WARRANTY, to the extent permitted by law.
    Written by Richard M. Stallman and David MacKenzie.
  12. Show Help Page

    List help page of ls command with their option.

    # ls --help
    
    Usage: ls [OPTION]... [FILE]...
  13. List Directory Information

    With ls -l command list files under directory /tmp. Wherein with -ld parameters displays information of /tmp directory.

    # ls -l /tmp
    total 4
    drwxr-xr-x    2 root     root           831 Jul  8  2017 fontconfig
    # ls -ld /tmp/
    
    drwxrwxrwt    3 root     root            64 Aug  5  2017 /tmp/
    
  14. Display UID and GID of Files

    To display UID and GID of files and directories. use option -n with ls command.

    # ls -n
    
    total 20
    drwxr-xr-x    2 0        0               37 Feb  2 23:03 dir1
    drwxr-xr-x    2 0        0               37 Feb  2 23:03 dir2
    drwxr-xr-x    3 0        0              163 Aug 21  2011 dos
    -rw-r--r--    1 0        0                0 Feb  2 22:39 file1
    -rw-r--r--    1 0        0                0 Feb  2 22:56 file10
    -rw-r--r--    1 0        0                0 Feb  2 22:39 file2
    -rw-r--r--    1 0        0                0 Feb  2 22:39 file3
    -rw-r--r--    1 0        0                0 Feb  2 22:56 file4
    -rw-r--r--    1 0        0                0 Feb  2 22:56 file6
    -rw-r--r--    1 0        0                0 Feb  2 22:56 file7
    -rw-r--r--    1 0        0                0 Feb  2 22:56 file8
    -rw-r--r--    1 0        0                0 Feb  2 22:56 file9
    -rw-r--r--    1 0        0              242 Jul 15  2017 hello.c
    drwxr-xr-x    2 0        0               81 Feb  2 23:13 tmp
    
  15. ls command and its Aliases

    We have made alias for ls command, when we execute ls command it’ll take -l option by default and display long listing as mentioned earlier.

    # alias ls="ls -l"

    Note: We can see number of alias available in your system with below alias command and same can be unalias as shown below example.

    # alias
    
    alias cp='cp -i'
    alias l='ls -d * --color=auto'
    alias ll='ls -l --color=auto'
    alias ls='ls --color=auto'
    alias mv='mv -i'
    alias rm='rm -i'
    alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'

    To remove an alias previously defined, just use the unalias command.

    # unalias ls


If you like dEexams.com and would like to contribute, you can also write your article here or mail your article to admin@deexams.com . See your article appearing on the dEexams.com main page and help others to learn.


Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


Python if , elif and else

Python Conditions and If statements

  • 0
Python for beginners

Learning Python Part 1

  • 3
Struct Alignment and Padding

Struct Alignment and Padding in C++ And C

  • 0
Friend function

Friend function C++

  • 0
Pointers

C++ Pointers

  • 0
Structures

C++ Structures

  • 0
Types of Inheritance in C++

Inheritance and access specifiers C++

  • 0
Java date pattern

Java Date Pattern Syntax

  • 0
Java Date and Calendar

Java Date formats

  • 0
JAVA Data Type

Data types in Java

  • 0
Java unreachable code

Unreachable Code Error in Java

  • 0

Post Comment

Comments(0)

WEB TECHNOLOGY

Articles

×

Forgot Password

Please enter your email address below and we will send you information to change your password.