The Linux operating system contains a number of basic commands to help you navigate the file system. Probably one of the very first commands you will learn and use is the ls command. The ls command is used to list directory contents.
There are a number of different options and operands that can be used with the ls command. Depending on the options and operands, the output and display of contents of a directory will vary.
The following is a list of the most basic ls command options and operands that are frequently used.
The ls command without any options or operands will list the contents of the current directory.
To list the contents of any directory anywhere on the filesystem agnostic from the current directory, you need to specify the absolute path of the directory as an operand for the ls command. The absolute path is the complete path starting from the root directory (/).
The ls command with the directory name called ‘/bin’ as the operand, displays the content of the ‘bin’ directory. The ‘/bin’ directory is a standard sub-directory of the root directory and contains the essential binary files for booting and shells like bash.
$ ls /bin
To list the contents of the a sub-directory of the current working directory, you need to specify the relative path of the directory as an operand of for the ls command.
The ls command with the directory name called ‘dir1/’ as the operand, displays the content of a sub-directory called ‘dir1’ from the current directory. If the sub-directory is not found within the current directory, the ls command will display an error saying “No such file or directory”.
$ ls dir1/
In Linux operating systems, any file or directory that starts with a dot ‘.’ character, will be treated as hidden. The ls command does not display hidden files or directories unless the -a flag / option is used.
$ ls -a
The behavior of the flag/option ‘-A’ where the ‘A’ is capitalized, is the same as using the flag/option ‘-a’ with a lower case ‘a’, except that the current directory ‘.’ and parent directory ‘..’ is not listed.
$ ls -A
The ls command by default only displays the name of the files and directories. To display more detailed information about the file and directory content, the ‘-l’ option is used with the ls command. This will list the content in the long format. The command displays the permission, user, group, size, timestamp and name of all the entries that match the listing criteria. A total sum for all the file sizes is output on a line before the long listing.
$ ls -l
The ls command with the option ‘-l’ displays detailed information about the files and directories including the size. The ‘-h’ option used with the ‘-l’ option, use unit suffixes: Byte, Kilobyte, Megabyte, Gigabyte, Terabyte and Petabyte in order to reduce the number of digits to three or less using base 2 for sizes.
$ ls -lh
The ls command by default only list the names of files and directories and it can be very difficult to differentiate between the different types of files and directories. The ‘-F’ option displays a slash (
/’) immediately after each pathname that is a directory, an asterisk (*’) after each that is executable, an at sign (
@’) after each symbolic link, an equals sign (=’) after each socket, a percent sign (
%’) after each whiteout, and a vertical bar (|’) after each that is a FIFO.
$ ls -F
The ls command by default list the content of the directory in lexicographical order (generalization of alphabetical order). It is possible to reverse the lexicographical order by making use of the ‘-r’ option as part of the command. If the ‘-r’ option is used with other options like ‘-t’ or ‘-S’, it will reverse the order of the sort to get reverse lexicographical order or the oldest entries first or largest files last.
$ ls -r
The ls command by default only list the content of the current directory. Any subdirectories within the current directory will be listed, but their content will not be listed. The ‘-R’ option will recursively list the sub-directories encountered.
$ ls -R
To sort the content of a directory based on modified time, the ‘-t- option is added to the ls command. This will sort the output by time modified (most recently modified first) before sorting the content by lexicographical order.
$ ls -t
An inode is a data structure that describes a filesystem object such as a file or directory. Each inode stores the attributes and disk block locations of the file or directory. Attributes include metadata like times of last change, access, modification as well as owner and permission data. To list the inode number of a directory, the ‘-i’ option is added to the command.
$ ls -i
By combining a number of options, it is possible to list the content of a directory in detail (long format) and sort the entries by modification date (latest first) in reverse order (latest modification last).
$ ls -ltr
To list the content of a directory in detail and sort it according to size, more than one option should be used as part of the ls command. The ‘-l’ option will list the content in detail, the ‘-S’ option will sort according to size and the ‘-h’ option will use unit suffixes for bytes, kilobytes, etc..
$ ls -lSh
The different options and operands can be combined in a single command to perform various tasks. The ‘-F’ option will indicate the type of entry (directory, executable, pipe, etc..) of the bin directory located under the root directory.
$ ls -F /bin
Congratulations! You have successfully worked through the 15 ‘ls’ command examples in Linux. Follow me on any of the different social media platforms and feel free to leave comments.