ls Study Notes

ls (list)

It displays files and directories. Default Behavior if you run:

ls
  • lists the current directory
  • sorts alphabetically
  • hides files beginning with .
  • outputs in columns if displayed in a terminal

SECTION 1 - Which Files Are Listed

-a, --all

Show all files including hidden files.

ls -a

Special entries

  • . current directory
  • .. parent directory

-A, --almost-all

Like -a, but excludes . and ..


-B, --ignore-backups

Hide backup files ending with ~

This option is equivalent to --ignore='*~' --ignore='.*~'


-d, --directory

List just the names of directories, as with other types of files, rather than listing their contents.


-R, --recursive

List the contents of all directories recursively.


-L, --dereference

When showing file information for a symbolic link, show information for the file the link references rather than the link itself. However, even with this option, ls still prints the name of the link itself, not the name of the file that the link points to.


SECTION 2 - What Information Is Listed

-l, --format=long, --format=verbose

Produce long format. In addition to the name of each file, print the file type, file mode bits, number of hard links, owner name, group name, size, and timestamp

ls -l

Example:

-rw-r--r-- 1 user group 1200 May 8 12:30 file.txt
Field Meaning
- file type
rw-r--r-- permissions
1 hard link count
user owner
group group
1200 file size
May 8 12:30 timestamp
file.txt file name

File Type Characters

First character indicates file type. The file type is one of the following characters:

Character Meaning
- regular file
d directory
l symbolic link
c character device
b block device
p FIFO/pipe
s socket

Permission Bits

Grouped into:

Group Meaning
rwx owner
r-x group
r-- others

Permission meanings:

Symbol Meaning
r read
w write
x execute

Special Permission Characters

Symbol Meaning
s setuid/setgid with execute
S setuid/setgid without execute
t sticky bit with execute
T sticky bit without execute

-h, --human-readable

Append a size letter to each size, such as M for mebibytes. Powers of 1024 are used, not 1000


--si

Human-readable using powers of 1000.

ls --si

-i, --inode

Print the inode number (also called the file serial number and index number) of each file to the left of the file name. (This number uniquely identifies each file within a particular file system)


-s, --size

Print the file system allocation of each file to the left of the file name. This is the amount of file system space used by the file, which is usually a bit more than the file’s size, but it can be less if the file has holes.


-n

Show numeric UID/GID.


-o

Long format without group.


-g

Long format without owner.


--full-time

Show complete timestamps.


SECTION 3 - Sorting

-t

Sort by modification timestamp (mtime) by default, newest first.


-r

Reverse sorting.


-S

Sort by size. Largest first.


-X

Sort by extension.


-v

Version sort.


-U

No sorting.


--group-directories-first

Directories appear before files.


SECTION 4 - Output Formatting

-1

One entry per line.


-C

Columns (default on terminal).


-x

Horizontal sorting.


-m

Comma-separated output.


--color

Enable colors.

ls --color=auto

Modes:

Mode Meaning
none no colors
auto only in terminal
always always

-F

Append indicators.

Examples:

Symbol Meaning
/ directory
* executable
@ symbolic link
= socket

-p

Append / only for directories.