Ls Command
List Directory Contents
The ls command is a fundamental utility in Unix-like
operating systems (Linux, macOS) used to list information about
files and directories within a specified directory. It's one of the
most frequently used commands for navigating and understanding the
file system.
Common Ls Command Options
Here are some of the most useful options for the
ls command:
# To display everything in <dir>, excluding hidden files:
ls <dir>
# To display everything in <dir>, including hidden files:
ls -a <dir>
# To display all files, along with the size (with unit suffixes) and timestamp:
ls -lh <dir>
# To display files, sorted by size:
ls -S <dir>
# To display directories only:
ls -d */ <dir>
# To display directories only, include hidden:
ls -d .*/ */ <dir>
# To display all files sorted by changed date, most recent first:
ls -ltc
# To display files sorted by create time:
ls -lt
# To display files in a single column:
ls -1
# To show ACLs (MacOS):
# see also `cheat chmod` for `/bin/chmod` options for ACLs
/bin/ls -le
# To show all the subtree files (Recursive Mode):
ls -R
Understanding Ls Output
The output of ls can vary depending on the options
used. The -l option provides a long listing format,
which includes details such as file permissions, number of links,
owner, group, size, and last modification date. Understanding these
details is crucial for managing files effectively.
Advanced Ls Usage
For more complex scenarios, you can combine multiple options. For
instance, ls -lart will list all files (including
hidden ones due to -a), in long format
(-l), sorted by modification time in reverse order
(-r), and by access time (-t). This allows
for highly customized file listings tailored to specific needs.