Linux ls
Command Manual
Overview​
The ls
command lists directory contents. It's one of the most fundamental commands in Linux/Unix systems, displaying files and directories in the specified location.
Basic syntax:
ls [OPTION]... [FILE]...
Argument Utilities​
Basic Options​
-l
: Long format listing with detailed information-a
: Shows all files including hidden ones (starting with .)-h
: Human-readable file sizes (KB, MB, GB)-R
: Recursive listing (includes subdirectories)-d
: Lists directories themselves, not their contents--color=auto
: Colorized output for different file types
Sorting Options​
-t
: Sort by modification time (newest first)-S
: Sort by file size (largest first)-r
: Reverse the sort order-X
: Sort alphabetically by file extension-U
: Do not sort (list in directory order)
List & Filter by Filenames​
-
Using
ls
with grep:ls | grep code
This lists all files/directories and filters for those containing "code"
-
Using wildcards directly with ls:
ls *code*
This lists all files/directories with "code" anywhere in the filename
-
For case-insensitive search:
ls | grep -i code
This finds files containing "code", "Code", "CODE", etc.
-
With detailed listing:
ls -l *code*
Shows detailed information for all files containing "code"
-
Including hidden files:
ls -a | grep code
or
ls -la *code*
-
Using find for more powerful filtering:
find . -name "*code*" -type f
This finds all files (not directories) containing "code" in current directory and subdirectories
The simplest approach for most cases is just ls *code*
, which will display all files and directories in the current location with "code" in their name.
List Filtering by Name/Type/Size/Date​
Filtering by Name​
ls file*
: Lists all files starting with "file"ls *.txt
: Lists all files with .txt extensionls [abc]*
: Lists all files starting with a, b, or c
Filtering by Type​
ls -F
: Appends indicators to entries (/ for directories, * for executables)ls -p
: Appends / to directoriesfind . -type f -maxdepth 1 | xargs ls -l
: List only regular filesfind . -type d -maxdepth 1 | xargs ls -ld
: List only directories
Filtering by Size​
ls -lS
: Lists files sorted by size (largest first)find . -size +10M -type f -exec ls -lh {} \;
: List files larger than 10MBfind . -size -1M -type f -exec ls -lh {} \;
: List files smaller than 1MB
Filtering by Date​
ls -lt
: Lists files sorted by modification time (newest first)ls -ltr
: Lists files sorted by modification time (oldest first)find . -mtime -7 -type f -exec ls -l {} \;
: List files modified in the last 7 daysfind . -mtime +30 -type f -exec ls -l {} \;
: List files not modified in the last 30 days
Other Utilities for Best Practice​
Output Formatting​
ls -1
: One file per linels -m
: Comma-separated list of entriesls --group-directories-first
: List directories before files
Combining Options​
ls -lahS
: Most common combination (long format, all files, human-readable sizes, sorted by size)ls -ltr
: See files in order of modification (oldest first)
Aliases​
Add these to your ~/.bashrc
or ~/.bash_aliases
:
alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF'
alias lt='ls -ltr'
alias lsize='ls -lahS'
Integration with Other Commands​
ls | grep pattern
: Filter ls output for a specific patternls -l | sort -k5 -n
: Sort ls output by file size numericallyls -la | awk '$5 > 1000000'
: Show files larger than 1MB
Security Considerations​
- Use
ls -la
when checking permissions - Check for hidden files with
ls -a
when auditing directories - Use
ls -i
to show inode numbers (helpful for finding hard links)
Would you like me to explain or break down any of these commands or options in more detail?