Another most commonly-used commands in Linux. Use this command to list information of files and directories such as the file permissions, owners, and timestamp to name a few. By default, it will list files current directory when it’s used without passing any arguments.
General syntax for
$ ls [OPTION...] [FILE...]
Display hidden files
This option will display the entire list of directories, including hidden files. Hidden files start with a dot (.)
Example: Let’s display all hidden files of the ExampleDir directory.
Sorting by file size
ls -S command to sort files and directories by size in descending order, and then print them to the terminal.
Example: Sorting all the files and directories of the NewDir directory.
Display files in long format
-l option displays the contents of the directory in a more detailed way. Displaying the file’s owner and group, last modified time, and more.
Example: Displaying the detailed information about the files in the NewDir directory.
Sort by date and time
This command sorts files by last modified time. The most recently edited files will appear at the top of the output, making them easy to find.
Example: Let’s display and sort by time the files from the ExampleDir directory.
Display only directories
ls -d */
Use this command to list the sub-directories excluding all other files.
Example: Listing the directories of the NewFolder directory.
List files and save results to a file
ls > [filename]
The ls > [filename] command allows you to save the output of the preceding command to a file.
Example: Saving the output of the
ls -d command to the file result.txt.
List file owners with their ID
Example: Let’s display the UID and GID of the files in the NewDir directory.