As we may already know, ls and dir are the two commands available on Linux for listing directory content, with the former being more popular and in most cases, preferred by users.
When listing directory contents, the results can be sorted based on several criteria such as alphabetical order of filenames, modification time, access time, version and file size. Sorting using each of these file properties can be enabled by using a specific flag.
Linux Basic ls Commands
1. Running ls command without appending any argument will list current working directory contents.
2. To list contents of any directory, for example /etc directory use:
$ ls /etc
3. A directory always contains a few hidden files (at least two), therefore, to show all files in a directory, use the -a or --all flag:
$ ls -a
4. You can as well print detailed information about each file in the ls output, such as the file permissions, number of links, owner’s name and group owner, file size, time of last modification and the file/directory name.
This is activated by the -l option, which means a long listing format as:
$ ls -l
Sort Files Based on Time and Date
5. To list files in a directory and sort them last modified date and time, make use of the -t option as in the command below:
$ ls -lt
6. If you want reverse sorting files based on date and time, you can use the -r option to work like so:
$ ls -ltr