ls Linux Command
Lists files and directories in the current directory.
What does ls do?
Lists files and directories in the current directory.
Syntax
ls [options] [path]Practical example
ls -la /home/userQuestions about ls
What does ls -la mean?
The `-l` flag shows a detailed long listing format with permissions, owner, size, and date. The `-a` flag shows all files including hidden ones starting with a dot.
How do I list files in a specific directory?
Use `ls /path/to/directory`. For example, `ls /var/log` lists files in the log directory without changing your current location.
How do I sort files by size?
Use `ls -lhS` to sort by file size in human-readable format (KB, MB, GB). The `-S` flag sorts by size, largest first.
How do I show file permissions in ls?
Use `ls -l` to see permissions in the first column. The format is like `-rwxr-xr--` where r=read, w=write, x=execute for owner, group, and others.
How do I list only directories?
Use `ls -d */` to list only directories, or `ls -l | grep '^d'` for a detailed listing of just directories.
What does ls -lh show?
The `-h` flag makes file sizes human-readable (e.g., 1.5K, 2.3M) instead of showing raw byte counts. Combine with `-l` for a detailed readable listing.
How do I color-code ls output?
Most distributions alias `ls` with color by default. If not, use `ls --color=auto` or add `alias ls='ls --color=auto'` to your `.bashrc`.
How do I see hidden files with ls?
Use `ls -a` to show all files including hidden ones (files starting with `.`). Use `ls -A` to show hidden files except `.` and `..`.
Related Navigation Commands
pwdPrints the current working directory (full path from root).
cdChanges the current working directory to the specified path.
dirsDisplays the directory stack (list of directories saved with pushd).
cd ..Moves up one directory level (to the parent directory).
popdReturns to the directory saved by the last pushd command.
cd ~Changes to the current user's home directory.