ls — Linux basic command
Linux / Basic Linux commands / ls.html
A command that displays a list of files and directories in the current directory or a specified path.
Basic Information
| Command Name | ls (list) |
|---|---|
| One-Line Summary | A command that displays a list of files and directories in the current directory or a specified path. |
| Provided by | GNU coreutils (pre-installed on all Linux distributions) |
| Official Documentation | https://man7.org/linux/man-pages/man1/ls.1.html |
| Supported Operating Systems | Linux, macOS, BSD, WSL |
| Installation | Pre-installed (no separate installation required) |
| Category | Basic Linux Command (Tags: file listing, directory, linux, basic) |
What is this command?
ls is an abbreviation for 'list', and it is the most fundamental command that shows what files and subdirectories exist inside a directory. It can be considered the starting point for working in a Linux terminal.
You can add options to display detailed information such as file hidden status, permissions, owner, size, modification time, and more.
What problem does it solve?
- Check what files are present at the current location.
- Quickly understand file permissions, owner, and size.
- View hidden files.
- Explore the directory structure.
Key Features
Basic List Display
Displays files and directories in the current directory showing only their names.
- Use Case: When you need a quick check of what's in your working directory.
- Result Obtained: A list of file and directory names is displayed.
- Limitation: Hidden files (starting with a dot) are not shown.
Detailed Information Display (-l)
Displays detailed information such as permissions, owner, size, and modification time.
- Use Case: When you need to check file permission issues or size.
- Result Obtained: You can view the metadata for each file, one line at a time.
- Limitation: The output can be long, so it's often used with `less`.
Display Hidden Files (-a)
Displays all files, including hidden files starting with a dot (.).
- Use Case: When you need to check configuration files (e.g., .bashrc, .gitignore).
- Result Obtained: You see the complete list, including hidden files.
- Limitation: The list can become cluttered if there are many system hidden files.
Human-Readable Size Units (-h)
Displays file sizes in human-readable units (KB, MB, GB).
- Use Case: When you need to check disk space.
- Result Obtained: Displays sizes in an easy-to-read format, like 8.0M instead of 8388608.
- Limitation: Must be used with the -l option to be effective.
Installation and Execution
Verify Pre-installation
It is included by default in all Linux distributions.
# Check (usually /bin/ls or /usr/bin/ls)
which ls
# Check version
ls --version
When Separate Installation is Required
Only necessary in very minimal environments. It is already present on most systems.
# Debian/Ubuntu (usually already installed)
sudo apt install coreutils
# Alpine Linux
apk add coreutils
Practical Examples
View Basic List
Check the file list of the current directory.
# List current directory
ls
# List a specific directory
ls /etc
# Check multiple paths simultaneously
ls /etc /var/log
Detailed List and Hidden Files
Check permissions, size, and hidden files.
# Detailed info + hidden files + human-readable sizes
ls -lah
# Example output:
# -rw-r--r-- 1 root root 220 Jan 6 2022 .bash_logout
# -rw-r--r-- 1 root root 3.5K Oct 18 14:14 index.html
# drwxr-xr-x 4 root root 4.0K Oct 18 14:14 programs
Sorting and Filtering
Use sorting by size/time and pattern matching.
# Sort by modification time (newest first)
ls -lt
# Sort by name
ls -lX
# Pattern matching (only *.html files)
ls *.html
# Display only directories
ls -d */