ls command in UNIX


ls [OPTION]... [FILE]...

Introduction

List information about elements (in the current directory, by default).

Options

Here are some of the options that you will use the most:

  • -a, --all: do not ignore entries starting with . (single dot)
  • -b, --escape: print C-style escapes for non-graphic character (like spaces)
  • -d, --directory: list directories themselves, not their contents
  • -F, - --classify: append indicator (one of */=>@|) to entries
  • -h, --human-readable: with -l and -s, print sizes like 1K, 234M etc.
  • --si: likewise, but use powers of 1000 not 1024
  • -i, --inode: print the index number of each file
  • -l: use a long listing format
  • -n, --numeric-uid-gid: like -l, but list numeric user and group IDs
  • -r, --reverse: reverse order while sorting
  • -R, --recursive: list subdirectories recursively
  • -s, --size: print the allocated size of each file, in blocks
  • -t: sort by modification time, the newest first
  • -X: sort alphabetically by entry extension

Examples

If you specify a directory name as parameter, you’ll get the ls of that directory without having to change directory (cd) into it:

cd Desktop/

ls
arduino-arduinoide.desktop  fritzing.AppImage  Matlab.desktop
blog                        homeworks          dog

pwd
/home/pit/Desktop

Without changing directory:

ls Desktop/
arduino-arduinoide.desktop  fritzing.AppImage  Matlab.desktop
blog                        homeworks          dog

pwd
/home/pit

The long listing format is usually set as an alias (we’ll talk about aliases in a dedicated blog post).

cat .bashrc
# ...
alias ll='ls -alF'
# ...

# $ls -alF Desktop/
ll Desktop/
drwxr-xr-x  6 pit pit     4096 mar 13 15:12 ./
drwxr-xr-x 47 pit pit     4096 mar 14 00:11 ../
-rwxr-xr-x  1 pit pit      364 ott 26 11:57 arduino-arduinoide.desktop*
drwxrwxr-x  2 pit pit     4096 mar 11 18:12 blog/
drwxrwxr-x  3 pit pit     4096 mar  8 15:56 dog/
# ...

Other options, like reverse or recursive, are very easy to use:

cd Desktop/dog && ls
hello-world  hello-world.c  subfolder

ls -r # ls --reverse
subfolder  hello-world.c  hello-world

ls -t
subfolder  hello-world  hello-world.c

ls -R # ls --recursive
.:
hello-world  hello-world.c  subfolder

./subfolder:
file.py  file.txt  hard-link.txt

ls -Rt # ls -tR
.:
subfolder  hello-world  hello-world.c

./subfolder:
file.txt  hard-link.txt  file.py

Please Note: && between two commands means that the second command wait for the first to finish before start its execution.

Quotes

Manual reference: