Bash ls
- List Directory Contents
The ls
command is used to list the contents of a directory.
The ls
command can display files, directories, and information about them.
Basic Usage
To see what's in the current folder, use ls
:
Example
ls
Cosmere_RPG_Beta_Rules_Preview.pdf images/
my_file.txt report.csv voiceover.wav
Options Overview
The ls
command has a variety of options to customize its output:
-l
- Long listing format-a
- Include hidden files-h
- Human-readable sizes-t
- Sort by modification time-r
- Reverse order while sorting-R
- List subdirectories recursively-S
- Sort by file size-1
- List one file per line-d
- List directories themselves, not their contents-F
- Append indicator (one of */=@|) to entries
Long Listing Format
The -l
option gives you detailed information about files and folders.
It displays information such as:
- file permissions
- number of links
- owner name
- owner group
- file size
- time of last modification
- file or directory name
This format is useful for getting a comprehensive overview of the file attributes.
Example: Long Listing Format
ls -l
total 24232
-rw-r--r-- 1 user 197609 23777028 Jan 15 20:38 Cosmere_RPG_Beta_Rules_Preview.pdf
drwxr-xr-x 1 user 197609 0 Apr 9 07:46 images/
-rw-r--r-- 1 user 197609 890 Apr 9 07:48 my_file.txt
-rw-r--r-- 1 user 197609 305366 Apr 9 07:48 report.csv
-rw-r--r-- 1 user 197609 720974 Apr 9 07:47 voiceover.wav
Include Hidden Files
The -a
option includes hidden files in the listing.
Hidden files in Unix/Linux systems start with a dot (e.g., .bashrc
).
This option is helpful when you need to view or manage configuration files that are not visible by default.
Example: Including Hidden Files
ls -a
./ ../ .my_secret_file Cosmere_RPG_Beta_Rules_Preview.pdf
images/ my_file.txt report.csv voiceover.wav
Human-Readable Sizes
The -h
option makes file sizes easier to read by converting byte counts into kilobytes (K), megabytes (M), gigabytes (G), etc.
This option is particularly useful when you want to quickly assess the size of files and directories without manually converting bytes.
Example: Human-Readable Sizes
ls -lh
total 24M
-rw-r--r-- 1 user 197609 23M Jan 15 20:38 Cosmere_RPG_Beta_Rules_Preview.pdf
drwxr-xr-x 1 user 197609 0 Apr 9 07:51 images/
-rw-r--r-- 1 user 197609 890 Apr 9 07:48 my_file.txt
-rw-r--r-- 1 user 197609 299K Apr 9 07:48 report.csv
-rw-r--r-- 1 user 197609 705K Apr 9 07:47 voiceover.wav
Sorting by Time
The -t
option sorts files and directories by modification time, with the most recently modified files first.
This option is useful when you want to see the most recently updated files first.
Example: Sorting by Time
ls -t
images/ my_file.txt report.csv voiceover.wav
Cosmere_RPG_Beta_Rules_Preview.pdf
Reverse Order
The -r
option reverses the order of the sort.
When used in combination with other options like -t
, it can display the oldest files first.
This option is useful for reversing the default sorting behavior to meet specific needs.
Example: Reverse Order
ls -r
voiceover.wav report.csv my_file.txt images/
Cosmere_RPG_Beta_Rules_Preview.pdf
Recursive Listing
The -R
option lists directories and their contents recursively.
This is useful for viewing the entire directory tree.
Example: Recursive Listing
ls -R
.:
copy_of_my_file.txt Cosmere_RPG_Beta_Rules_Preview.pdf
images/ my_file.txt myfolder/ report.csv voiceover.wav
./images:
1.png 2.png 3.png 4.png
./myfolder:
my_file.txt new_directory/
./myfolder/new_directory:
Sort by Size
The -S
option sorts files by size, with the largest files first.
This is helpful for quickly identifying large files in a directory.
Example: Sort by Size
ls -S
Cosmere_RPG_Beta_Rules_Preview.pdf voiceover.wav report.csv
copy_of_my_file.txt my_file.txt images/ myfolder/
One File per Line
The -1
option lists one file per line, which is useful for scripts or when piping output to other commands.
Example: One File per Line
ls -1
Cosmere_RPG_Beta_Rules_Preview.pdf
images/
my_file.txt
report.csv
voiceover.wav
Directories Only
The -d
option lists directories themselves rather than their contents.
This is useful for seeing directory names without contents.
Example: Directories Only
ls -d */
images// myfolder//
Append Indicator
The -F
option appends an indicator character to entries (e.g., /
for directories, *
for executables).
Example: Append Indicator
ls -F
Cosmere_RPG_Beta_Rules_Preview.pdf images/
my_file.txt report.csv voiceover.wav
Using Multiple Options
You can combine multiple options to create more complex commands.
For example, ls -l -a
will display a detailed listing of all files and directories, including hidden files.
Example
ls -l -a
total 24248
drwxr-xr-x 1 user 197609 0 Apr 9 07:53 ./
drwxr-xr-x 1 user 197609 0 Apr 9 07:42 ../
-rw-r--r-- 1 user 197609 890 Apr 9 07:48 .my_secret_file
-rw-r--r-- 1 user 197609 23777028 Jan 15 20:38 Cosmere_RPG_Beta_Rules_Preview.pdf
drwxr-xr-x 1 user 197609 0 Apr 9 07:51 images/
-rw-r--r-- 1 user 197609 890 Apr 9 07:48 my_file.txt
-rw-r--r-- 1 user 197609 305366 Apr 9 07:48 report.csv
-rw-r--r-- 1 user 197609 720974 Apr 9 07:47 voiceover.wav
You can also combine multiple options without a space between them. For example, ls -la
:
Example
ls -la
total 24248
drwxr-xr-x 1 user 197609 0 Apr 9 07:53 ./
drwxr-xr-x 1 user 197609 0 Apr 9 07:42 ../
-rw-r--r-- 1 user 197609 890 Apr 9 07:48 .my_secret_file
-rw-r--r-- 1 user 197609 23777028 Jan 15 20:38 Cosmere_RPG_Beta_Rules_Preview.pdf
drwxr-xr-x 1 user 197609 0 Apr 9 07:51 images/
-rw-r--r-- 1 user 197609 890 Apr 9 07:48 my_file.txt
-rw-r--r-- 1 user 197609 305366 Apr 9 07:48 report.csv
-rw-r--r-- 1 user 197609 720974 Apr 9 07:47 voiceover.wav