Linux LS Command Overview

Linux LS Command is used give the list of the content in a directory. It list out the files and directory inside any directory and by default it prints them by sorting them alphabettically. It is one of the most common, basic and most used command in a linux. It is pretty much useful if we know all options provided by ls command.

Linux LS Command Syntax

ls [options]... [File]...

Linux LS Command Options


-a, --alldo not ignore entries starting with .
-A, --almost-alldo not list implied . and ..
--authorwith -l, print the author of each file
-b, --escapeprint C-style escapes for nongraphic characters
--block-size=SIZEscale sizes by SIZE before printing them. E.g., '--block-size=M' prints sizes in units of 1,048,576 bytes. See SIZE format below.
-B, --ignore-backupsdo not list implied entries ending with ~
-cwith -lt: sort by, and show, ctime (time of last modification of file status information) with -l: show ctime and sort by name otherwise: sort by ctime, newest first
-Clist entries by columns
--color[=WHEN]colorize the output. WHEN defaults to 'always' or can be 'never' or 'auto'. More info below
-d, --directorylist directory entries instead of contents, and do not dereference symbolic links
-D, --diredgenerate output designed for Emacs' dired mode
-fdo not sort, enable -aU, disable -ls --color
-F, --classifyappend indicator (one of */=>@|) to entries
--file-typelikewise, except do not append '*'
--format=WORDacross -x, commas -m, horizontal -x, long -l, single-column -1, verbose -l, vertical -C
--full-timelike -l --time-style=full-iso
-g like -l, but do not list owner
--group-directories-firstgroup directories before files. augment with a --sort option, but any use of --sort=none (-U) disables grouping
-G, --no-groupin a long listing, don't print group names
-h, --human-readablewith -l, print sizes in human readable format (e.g., 1K 234M 2G)
--silikewise, but use powers of 1000 not 1024
-H, --dereference-command-linefollow symbolic links listed on the command line
--dereference-command-line-symlink-to-dirfollow each command line symbolic link that points to a directory
--hide=PATTERNdo not list implied entries matching shell PATTERN (overridden by -a or -A)
--indicator-style=WORD append indicator with style WORD to entry names: none (default), slash (-p), file-type (--file-type), classify (-F)
-i, --inode print the index number of each file
-I, --ignore=PATTERN do not list implied entries matching shell PATTERN
-k, --kibibytes use 1024-byte blocks
-l use a long listing format
-L, --dereference when showing file information for a symbolic link, show information for the file the link references rather than for the link itself
-m fill width with a comma separated list of entries
-n, --numeric-uid-gid like -l, but list numeric user and group IDs
-N, --literal print raw entry names (don't treat e.g. control characters specially)
-o like -l, but do not list group information
-p, --indicator-style=slash append / indicator to directories
-q, --hide-control-chars print ? instead of non graphic characters
--show-control-chars show non graphic characters as-is (default unless program is 'ls' and output is a terminal)
-Q, --quote-name enclose entry names in double quotes
--quoting-style=WORD use quoting style WORD for entry names: literal, locale, shell, shell-always, c, escape
-r, --reverse reverse order while sorting
-R, --recursive list subdirectories recursively
-s, --size print the allocated size of each file, in blocks
-S sort by file size
--sort=WORD sort by WORD instead of name: none -U, extension -X, size -S, time -t, version -v
--time=WORD with -l, show time as WORD instead of modification time: atime -u, access -u, use -u, ctime -c, or status -c; use specified time as sort key if --sort=time
--time-style=STYLE with -l, show times using style STYLE: full-iso, long-iso, iso, locale, +FORMAT. FORMAT is interpreted like 'date'; if FORMAT is FORMAT1FORMAT2, FOR- MAT1 applies to non-recent files and FORMAT2 to recent files; if STYLE is prefixed with 'posix-', STYLE takes effect only outside the POSIX locale
-t sort by modification time, newest first
-T, --tabsize=COLS assume tab stops at each COLS instead of 8
-u with -lt: sort by, and show, access time with -l: show access time and sort by name otherwise: sort by access time
-U do not sort; list entries in directory order
-v natural sort of (version) numbers within text
-w, --width=COLS assume screen width instead of current value
-x list entries by lines instead of by columns
-X sort alphabetically by entry extension
-Z, --context print any SELinux security context of each file
-1 list one file per line
--help display this help and exit
--version output version information and exit


SIZE is an integer and optional unit (example: 10M is 10*1024*1024). Units are K, M, G, T, P, E, Z, Y (powers of 1024) or KB, MB, ... (powers of 1000).

Using color to distinguish file types is disabled both by default and with --color=never. With --color=auto, ls emits color codes only when standard output is connected to a terminal. The LS_COLORS environment variable can change the settings. Use the dircolors command to set it.

Exit status:


0 if OK,
1 if minor problems (e.g., cannot access subdirectory),
2 if serious trouble (e.g., cannot access command-line argument).


Linux LS Command Example

Let us look closer to Linux LS Command with the help of examples

List all the files and directories in current directory

Simply to list all the files and directory use 'ls' command.

ankur$ ls
bindexample.php    curl_test.php    examples.php    mult

List hidden files

Command used to see all the files and directories including hidden files are "ls -a"

"-a" parameter is used to return all the files and directories.

ankur$ ls -a
.        ..        .iamahiddenfile    bindexample.php    curl_test.php    examples.php    mult

Files starting with "." are represented as hidden files in linux

List file and directories inside current directory with details as well

Command used to fetch files with details is "ls -l"

"-l" parameter is used to return all the files and directories with their details.

ankur$ ls -l
total 24
-rw-r--r-- 1 ankur staff 61 Apr 29 18:52 bindexample.php
-rw-r--r-- 1 ankur staff 486 Apr 20 14:46 curl_test.php
-rw-r--r-- 1 ankur staff 70 Apr 29 18:51 examples.php
drwxr-xr-x 2 ankur staff 68 Apr 29 18:51 mult

List files and directory with details in human readable form.

"-lh" parameter is used to show details in humar readable form

ankur$ ls -lh
total 24
-rw-r--r-- 1 ankur staff 61B Apr 29 18:52 bindexample.php
-rw-r--r-- 1 ankur staff 486B Apr 20 14:46 curl_test.php
-rw-r--r-- 1 ankur staff 70B Apr 29 18:51 examples.php
drwxr-xr-x 2 ankur staff 68B Apr 29 18:51 mult

List all the files and directory inside the current directory recursively.

"-R" parameter is used to recursively list all the files and directory.

ankur$ ls -R
bindexample.php    curl_test.php    examples.php    mult

./mult:
user_details.php

List all the files and directory in reverse order

"-r" parameter is used to list files and directory in reverse order.

ankur$ ls -r
mult        examples.php    curl_test.php    bindexample.php

Sort the files on the basis of file size

"-lS" combination of ls is used to sort files on the basis of file size.

ankur$ ls -lS
total 24
-rw-r--r-- 1 ankur staff 486 Apr 20 14:46 curl_test.php
drwxr-xr-x 3 ankur staff 102 Apr 29 20:07 mult
-rw-r--r-- 1 ankur staff 70 Apr 29 18:51 examples.php
-rw-r--r-- 1 ankur staff 61 Apr 29 18:52 bindexample.php

List files with inode number

"-i" will be used to show inode number together with file names.

ankur$ ls -i
18139335 bindexample.php    17565574 curl_test.php        18139313 examples.php        18139253 mult

List by showing latest modified file or directory as last

"-ltr" combination of this will show the list of files and directory on the basis of modified date.

ankur$ ls -ltr
total 24
-rw-r--r-- 1 ankur staff 486 Apr 20 14:46 curl_test.php
-rw-r--r-- 1 ankur staff 70 Apr 29 18:51 examples.php
-rw-r--r-- 1 ankur staff 61 Apr 29 18:52 bindexample.php
drwxr-xr-x 3 ankur staff 102 Apr 29 20:07 mult

Check version of ls command

"--version" parameter is used to check the version

ankur$ ls --version
ls (GNU coreutils) 8.21
Copyright (C) 2013 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later http://gnu.org/licenses/gpl.html 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.

Display UID and GID of files

"-n" parameter is used to display UID and GID of files

ankur$ ls -n
total 24
-rw-r--r-- 1 502 20 61 Apr 29 18:52 bindexample.php
-rw-r--r-- 1 502 20 486 Apr 20 14:46 curl_test.php
-rw-r--r-- 1 502 20 70 Apr 29 18:51 examples.php
drwxr-xr-x 3 502 20 102 Apr 29 20:07 mult

Display information about the directory

"-ld" parameter will show the information about the directory.

ankur$ ls -ld test
drwxr-xr-x 7 ankur staff 238 Apr 29 18:56 test

Display information about the files inside a directory

"-l" parameter shows the information about the list of the files under the directory.

ankur$ ls -l test
total 24
-rw-r--r-- 1 ankur staff 61 Apr 29 18:52 bindexample.php
-rw-r--r-- 1 ankur staff 486 Apr 20 14:46 curl_test.php
-rw-r--r-- 1 ankur staff 70 Apr 29 18:51 examples.php
drwxr-xr-x 3 ankur staff 102 Apr 29 20:07 mult



If you want to add more comments to the article or you see any thing incorrect please write a comment below and we will surely get back to you.



Trending Articles


Incorrect table definition there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause

Web Server vs Application Server Difference Explained

Infix To Postfix Conversion Using Stack in Javascript

SQL Overview

Binary Search Tree Insert node Implementation in Javascript

Evaluate Postfix Expressions Using Stack In Javascript

Linked List Size or Count in Javascript

MYSQL Dump - How to take mysql dump or export mysql data