How to List Files and Folders in Your Directory: A Beginner's Guide to Using ls
?
One of the most basic Linux commands is "ls". It lists file and directory names and attributes, providing information on files, directories, and their attributes.
Syntax for ls command:
ls [/directory/folder/path]
Here are the best examples of using the ls command with different options:
List all files and directories of a current directory:
ls
List all files and directories which includes hidden ones:
ls -a
List files in long format with detailed information:
ls -l
List files in a single column:
ls -1
List files with human-readable file sizes:
ls -lh
List files sorted by modification time, newest first:
ls -lt
List directories only, not their contents:
ls -d */
List all files and directories recursively:
ls -R
List files in reverse order:
ls -r
List files with indicators appended to their names (e.g., / for directories, * for executables):
ls -F
List files without sorting:
ls -f
List files with their inode numbers:
ls -i
List files sorted by size:
ls -S
List files sorted by extension:
ls -X
List files with SELinux security context information:
ls -Z
Here are some common variations and options of the ls command in Linux. You can use more than one at a time, making the command more efficient for your needs.
Option | Long Name | Description |
-a | –all | Lists all files and directories, including hidden ones (with those which start with a dot). |
-A | –almost-all | Lists all files and directories except . and ... |
-b | –escape | Prints non-printable characters in file names as escape sequences. |
-B | –ignore-backups | do not list backup files (those ending with a tilde ~ character). |
-c | used for inode modification time (sorting) | |
-C | Lists files in columns(sorted vertically). | |
-d | –directory | list directories (not their contents) |
-D | –dired | Shows the type of each file (long listing) |
-f | Lists files unsorted (in the order they appear in the directory). | |
-F | –classify | Appends indicators like / to directories and * to executables. |
-g | Similar to -l, but does not display the owner of each file. | |
-G | –no-group | Enables colorized output only if the terminal is used |
-h | –human-readable | Prints file sizes in a human-readable format (e.g., KB, MB, GB). |
-H | –dereference-command-line | Follows symbolic links specified on the command line. |
-i | –inode | Prints the inode number of each file. |
-I | –ignore=PATTERN | Excludes files matching the specified pattern. |
-k | –block-size=1K | Prints file sizes in kilobytes. |
-l | Displays a long listing format with detailed information about files. | |
-L | –dereference | Follows symbolic links and lists information about the target file. |
-m | Lists files as a comma-separated list. | |
-n | –numeric-uid-gid | Similar to -l, but displays user and group IDs numerically. |
-N | –literal | Lists ACLs (Access Control Lists) and SElinux security context. |
-o | Similar to -l, but does not display group information. | |
-p | –indicator-style=slash | Appends a slash / to directories. |
-q | –hide-control-chars | Prints non-printable characters in file names as question marks. |
-Q | –quote-name | Quotes all file names to prevent interpretation of special characters. |
-r | –reverse | Reverses the order of sorting. |
-R | –recursive | Recursively lists subdirectories encountered. |
-s | –size | Displays the size of each file in blocks. |
-S | Sorts files by size, with the largest first. | |
-t | Sorts files by modification time, with the newest first. | |
-T | –tabsize=COLS | Specifies a timeout value for network operations. |
-u | Uses access time for sorting and displaying. (Can be used with -lt & - l) with -lt: sort by and show access time and with -l: show access time and sort by name. | |
-U | Lists files unsorted, the same as -f. | |
-v | Natural sorting of (version) numbers within text. | |
-w | Specifies the width of the output. | |
-x | Lists files horizontally. | |
-X | Sorts files alphabetically by extension. | |
-1 | Forces output to be one entry per line. | |
-Z | Displays SELinux security context information. |
That's it! You should now be familiar with the fundamentals of using the Linux ls command. For future reference, we advise referring to our other Linux command articles.