Bash ls Command - List Directory Contents
Using the ls Command
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
copy_of_my_file.txt images myfolder voiceover.wav
Cosmere_RPG_Beta_Rules_Preview.pdf my_file.txt report.csv
In a terminal, ls shows the names in columns to save space, as in the examples on this page. When the output goes to a file, another command or the Try it editor, it shows one name per line.
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 24244
-rw-r--r-- 1 user user 890 Apr 9 07:50 copy_of_my_file.txt
-rw-r--r-- 1 user user 23777028 Jan 15 20:38 Cosmere_RPG_Beta_Rules_Preview.pdf
drwxr-xr-x 2 user user 4096 Apr 9 07:51 images
-rw-r--r-- 1 user user 890 Apr 9 07:48 my_file.txt
drwxr-xr-x 3 user user 4096 Apr 9 07:52 myfolder
-rw-r--r-- 1 user user 305366 Apr 9 07:48 report.csv
-rw-r--r-- 1 user user 720974 Apr 9 07:47 voiceover.wav
Lines that start with d are directories. The total line is the disk space used, counted in 1K blocks.
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
. images report.csv
.. my_file.txt voiceover.wav
copy_of_my_file.txt myfolder
Cosmere_RPG_Beta_Rules_Preview.pdf .my_secret_file
. is the current directory and .. is the parent directory.
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 user 890 Apr 9 07:50 copy_of_my_file.txt
-rw-r--r-- 1 user user 23M Jan 15 20:38 Cosmere_RPG_Beta_Rules_Preview.pdf
drwxr-xr-x 2 user user 4.0K Apr 9 07:51 images
-rw-r--r-- 1 user user 890 Apr 9 07:48 my_file.txt
drwxr-xr-x 3 user user 4.0K Apr 9 07:52 myfolder
-rw-r--r-- 1 user user 299K Apr 9 07:48 report.csv
-rw-r--r-- 1 user user 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
myfolder my_file.txt Cosmere_RPG_Beta_Rules_Preview.pdf
images report.csv
copy_of_my_file.txt voiceover.wav
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 my_file.txt copy_of_my_file.txt
report.csv images
myfolder 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 images myfolder voiceover.wav
Cosmere_RPG_Beta_Rules_Preview.pdf my_file.txt report.csv
./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 copy_of_my_file.txt report.csv
voiceover.wav 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.
In a terminal, it turns the columns into a list with one name on each line.
Example: One File per Line
ls -1
copy_of_my_file.txt
Cosmere_RPG_Beta_Rules_Preview.pdf
images
my_file.txt
myfolder
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.
Append Indicator
The -F option appends an indicator character to entries (e.g., / for directories, * for executables).
Example: Append Indicator
ls -F
copy_of_my_file.txt images/ myfolder/ voiceover.wav
Cosmere_RPG_Beta_Rules_Preview.pdf my_file.txt report.csv
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 24256
drwxr-xr-x 4 user user 4096 Apr 9 07:53 .
drwxr-x--- 5 user user 4096 Apr 9 07:42 ..
-rw-r--r-- 1 user user 890 Apr 9 07:50 copy_of_my_file.txt
-rw-r--r-- 1 user user 23777028 Jan 15 20:38 Cosmere_RPG_Beta_Rules_Preview.pdf
drwxr-xr-x 2 user user 4096 Apr 9 07:51 images
-rw-r--r-- 1 user user 890 Apr 9 07:48 my_file.txt
drwxr-xr-x 3 user user 4096 Apr 9 07:52 myfolder
-rw-r--r-- 1 user user 890 Apr 9 07:48 .my_secret_file
-rw-r--r-- 1 user user 305366 Apr 9 07:48 report.csv
-rw-r--r-- 1 user user 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 24256
drwxr-xr-x 4 user user 4096 Apr 9 07:53 .
drwxr-x--- 5 user user 4096 Apr 9 07:42 ..
-rw-r--r-- 1 user user 890 Apr 9 07:50 copy_of_my_file.txt
-rw-r--r-- 1 user user 23777028 Jan 15 20:38 Cosmere_RPG_Beta_Rules_Preview.pdf
drwxr-xr-x 2 user user 4096 Apr 9 07:51 images
-rw-r--r-- 1 user user 890 Apr 9 07:48 my_file.txt
drwxr-xr-x 3 user user 4096 Apr 9 07:52 myfolder
-rw-r--r-- 1 user user 890 Apr 9 07:48 .my_secret_file
-rw-r--r-- 1 user user 305366 Apr 9 07:48 report.csv
-rw-r--r-- 1 user user 720974 Apr 9 07:47 voiceover.wav