The ‘ls’ command in GNU/Linux & Unix is a fundamental tool for listing the contents of directories. It provides valuable insights into file and directory information, including names, sizes, permissions, and modification times. This cheat sheet serves as a quick reference guide for utilizing the various options of the ‘ls’ command.
Syntax
ls [options] [file(s) or directory(s)]
Basic Usage
ls: List the contents of the current directory
ls /path/to/directory: List the contents of a specific directory
Common Options
ls -a: List all files, including hidden files. Hidden files are the files whose name starts with a dot (.)
ls -l: Long listing format. Provides detailed information about each file or directory, including permissions, number of links, owner, group, size, and modification time.
: Long listing format and displaying file sizes in human-readable format (e.g., 1K, 2M, 3G) for better readability.ls-lh
: Sort files by size, displaying the largest ones first.ls-S
: Sort files by modification time, showing the most recently modified files first.ls-t
: Reverse the order of the sort, displaying files in reverse order.ls-r
: Recursively list subdirectories encountered. Lists the contents of subdirectories as well.ls-R
: List one file per line. Prints each file on a new line, making the output easier to read.ls-1
: List directories themselves, not their contents. Useful when you want to only list directories.ls-d
: Enable colorized output. Differentiates file types and provides color highlighting for better visualization.ls-G
: Print the index number of each file. Displays the inode number for each file or directory.ls-i
: Append indicator characters to entries. Appends a character to indicate the type of each entry (e.g., a slash for directories, an asterisk for executable files).ls-F
: Fill width with a comma-separated list of entries. Prints the file names as a comma-separated list with the specified width.ls-m
: Display all non-printable characters as ‘?’. Replaces any non-printable characters in the file names with question marks.ls-q
: Likels-g-l, but do not list owner. Similar to the-loption, but does not display the owner’s name.
: Likels-o-l, but do not list group information. Similar to the-loption, but does not display the group information.
: Print the allocated size of each file, in blocks. Displays the size of each file in blocks.ls-s
: Use time of file creation, instead of last modification. Displays the creation time of each file or directory.ls-u
: Use time of last status change, instead of last modification. Displays the time of the last status change of each file.ls-c
: Follow symbolic links like normal files. Lists the information of the target file or directory that the symbolic link points to.ls-L
: Append a slash (‘/’) to directories. Appends a slash to the end of each directory name for easier identification.ls-p