Cheatsheet & Examples: ls
ls is a command-line utility used to list the contents of a directory. It displays files and directories, providing information about them based on the options used.
Listing Files and Directories in the Current Directory
Example Usage:
ls
What it does: Lists the files and directories in the current working directory, displaying only their names, one per line (if standard output is a terminal).
Command-line Arguments Explained:
- None used in this example.
Listing Files and Directories in a Specific Directory
Example Usage:
ls /path/to/directory
What it does: Lists the files and directories located in the specified directory path.
Command-line Arguments Explained:
/path/to/directory: The absolute or relative path to the directory you want to list the contents of.
Listing Files and Directories with Detailed Information
Example Usage:
ls -l
What it does: Lists files and directories with detailed information, including permissions, number of hard links, owner, group, size, modification date, and filename.
Command-line Arguments Explained:
-l: Enables the long listing format, showing detailed information.
Listing Files and Directories with Hidden Files
Example Usage:
ls -a
What it does:
Lists all files and directories, including hidden files and directories (those starting with a dot .).
Command-line Arguments Explained:
-a: Displays all files, including hidden files.
Listing Files and Directories in Human-Readable Sizes
Example Usage:
ls -lh
What it does: Lists files and directories with detailed information, showing file sizes in a human-readable format (e.g., KB, MB, GB).
Command-line Arguments Explained:
-l: Enables the long listing format, showing detailed information.-h: Displays file sizes in a human-readable format.
Listing Files and Directories, Sorted by Modification Time
Example Usage:
ls -t
What it does: Lists files and directories, sorted by their last modification time, with the most recently modified files appearing first.
Command-line Arguments Explained:
-t: Sorts by modification time (most recent first).
Listing Files and Directories, Sorted by Reverse Modification Time
Example Usage:
ls -rt
What it does: Lists files and directories, sorted by their last modification time, with the least recently modified files appearing first.
Command-line Arguments Explained:
-r: Reverses the order of the sort.-t: Sorts by modification time (most recent first).
Listing Files and Directories Recursively
Example Usage:
ls -R
What it does: Lists the contents of directories and their subdirectories, recursively.
Command-line Arguments Explained:
-R: Lists subdirectories recursively.
Listing Files and Directories, Showing File Type Indicators
Example Usage:
ls -F
What it does:
Lists files and directories, appending characters to indicate file types (e.g., / for directories, * for executables, @ for symbolic links).
Command-line Arguments Explained:
-F: Appends file type indicators.
Listing Files and Directories, Listing Directories First
Example Usage:
ls -ld */
What it does: Lists only the directories within the current directory, displaying their detailed information.
Command-line Arguments Explained:
-l: Enables the long listing format, showing detailed information.-d: Displays the directories themselves, not their contents.*/: A wildcard matching any directory.
Combining Options
Example Usage:
ls -la
What it does: Lists all files and directories, including hidden files, with detailed information.
Command-line Arguments Explained:
-l: Enables the long listing format, showing detailed information.-a: Displays all files, including hidden files.

