Table of Contents
Basic `ls` Usage
The standard command to list files and directories is ls.
- List items in the current directory:
ls- List a specific path:
ls /etc
ls /var/log
ls shows only filenames by default, and typically omits “hidden” files (names starting with .).
Showing Hidden Files
In Linux, filenames beginning with . are treated as hidden (for example .bashrc, .config).
To include hidden files:
ls -a
ls -a /home/userCommon variations:
ls -A— like-abut excludes.and...
Long Listings with Details
To see more information (permissions, owner, size, date) use the long format:
ls -l
ls -l /etcThis adds one line per entry with multiple columns (you will learn what each means in the permissions/users chapters):
Example output (for illustration):
-rw-r--r-- 1 user user 1234 Dec 1 10:00 notes.txt
drwxr-xr-x 2 user user 4096 Dec 2 09:30 projects
Combine with -a or -A:
ls -la
ls -lAHuman-Readable Sizes
To view file sizes in KB/MB/GB instead of raw bytes:
ls -lh
ls -lh /var/logYou can combine multiple options:
ls -lah # long, all (including hidden), human‑readableIndicating File Types
To quickly see what is a directory, executable, symlink, etc.:
ls -FThis adds markers:
/after directories*after executable files@after symbolic links
You can also combine:
ls -lFThis gives a detailed listing with type indicators.
Sorting Output
By default, ls sorts entries alphabetically. You can change the sort order.
Common sorting options:
- Sort by modification time (newest first):
ls -lt- Reverse the sort order (oldest first with
-t, or reverse alphabetically without it):
ls -ltr
ls -lr- Sort by file size (largest first):
ls -lS
These options can be combined with others (like -h and -a):
ls -lhS
ls -lahrRecursively Listing Subdirectories
To list the contents of directories and all their subdirectories:
ls -R
ls -R /etcThis prints a tree-like listing of each directory and its contents.
Use with caution in very large directory trees; it can produce a lot of output.
Coloring Output
Most modern distributions configure ls to use colors automatically, so different file types appear in different colors. If not, you can request it explicitly:
ls --color=auto
ls --color=alwaysauto—color when output is to a terminal.always—force colors even when piping to other commands or files (can be messy).
Often this behavior is set through an alias (for example, alias ls='ls --color=auto'), which you’ll see when learning shell configuration.
Listing a Single File or Directory
You can use ls to check details for a specific path:
ls -l myfile.txt
ls -ld mydirectoryNote the difference:
ls -l mydirectorylists the contents ofmydirectory.ls -ld mydirectoryshows information about the directory itself (permissions, owner, etc.) without listing its contents.
Useful Combinations to Practice
Try these combinations and observe the differences:
- Basic overview:
ls
ls -l- Include hidden files:
ls -a
ls -la- Focus on size and time:
ls -lhS # biggest files first
ls -lt # newest modified first
ls -ltr # oldest modified first- See structure:
ls -R
ls -lR
Practice running these in different directories like your home directory, /etc, and /var/log to get comfortable reading the output.