Skip to content

Click on each book below to review & buy on Amazon.

As an Amazon Associate, I earn from qualifying purchases.


LPI Linux Essentials Exam 010-160 - Topic 2.3 - Listing Files & Directories

The ls command is a fundamental utility in Linux used for listing files and directories in a specified location. It provides various options to display different attributes of files, including permissions and ownership. Understanding how to interpret the output of ls is essential for managing and navigating the file system effectively.

Viewing Permissions and Ownership

When running the ls -l command, the permissions and ownership of files and directories are displayed in the following format:

-rwxr-xr-x  1 user group   4096 May 10 12:34 file.txt
drwxr-xr-x  2 user group   4096 May 10 12:34 directory/

Let's break down the output:

  • The first column represents the permissions of the file or directory. It consists of ten characters, divided into four sections:

    • The first character indicates the type of the entry. - represents a regular file, d represents a directory, and other characters may indicate special types such as symbolic links.
    • The next nine characters are divided into three sets of three characters each. Each set represents the permissions for the owner, group, and others, respectively. The characters can be one of the following:
      • r: Read permission
      • w: Write permission
      • x: Execute permission
      • -: No permission
  • The second column indicates the number of hard links to the file or directory.

  • The third and fourth columns represent the owner and group of the file or directory, respectively.
  • The fifth column specifies the size of the file or the size occupied by the directory.
  • The sixth column displays the date and time of the last modification.
  • The last column indicates the name of the file or directory.

Commonly Used Options

Here are some of the commonly used options explained for the ls command:

  • List files and directories with detailed permissions and ownership (-l option):

    ls -l
    

    This command displays the long format listing, including permissions, ownership, size, and modification date.

  • List files and directories with human-readable file sizes (-h option):

    ls -lh
    

    This command provides a more readable output by displaying file sizes in a human-friendly format.

  • List all files and directories, including hidden ones (-a option):

    ls -a
    

    This command shows all entries, including those starting with a dot (which denotes hidden files or directories).

  • Sort files and directories by modification time (-t option):

    ls -lt
    

    This command lists the files and directories with the most recently modified ones appearing first.

  • Sort files and directories by modification time, in reverse order (-r option):

    ls -ltr
    

    This command lists the files and directories with the most recently modified ones appearing last.

  • Recursively list files and directories, including subdirectories (-R option):

    ls -R
    

    This command displays all files and directories in the specified location and its subdirectories.

Exercises

Exercise: List hidden files and directories:

Create a mix of normal and hidden files and directories.

Hidden files and directories are always proceeded with a . character.

touch /tmp/.hidden_file
touch /tmp/nonhidden_file
mkdir -p /tmp/.hidden_dir/
mkdir -p /tmp/nonhidden_dir/

Use the following command to list any files and directories in the /tmp directory that contain the word hidden:

ls -l /tmp/ | grep hidden

The output will resemble the following, but notice how how the hidden files and directories are, well, hidden from display:

drwxrwxr-x 2 user1 user1 4096 Oct 16 07:40 nonhidden_dir
-rw-rw-r-- 1 user1 user1    0 Oct 16 07:39 nonhidden_file

To view the hidden files and directories as well, use the -a option with the ls command:

ls -la /tmp | grep hidden

This time the output contains the .hidden_dir direcotry and .hidden_file file.

drwxrwxr-x  2 user1 user1 4096 Oct 16 07:39 .hidden_dir
-rw-rw-r--  1 user1 user1    0 Oct 16 07:39 .hidden_file
drwxrwxr-x  2 user1 user1 4096 Oct 16 07:40 nonhidden_dir
-rw-rw-r--  1 user1 user1    0 Oct 16 07:39 nonhidden_file

Exercise: Perform a recursive listing on the /usr/share/doc directory:

The -R option to the ls command will ensure a recursive listing into all sub-directories occurs:

ls -lR /usr/share/doc

The output is too long to fully share, but you will see many directories get displayed along with the contents of those directories. As an example from my Ubuntu 22.04 system, the following is the last two directory listings from my output:

/usr/share/doc/zlib1g-dev/examples:
total 288
-rw-r--r-- 1 root root 10785 Oct 14  2022 crc32_test.c
-rw-r--r-- 1 root root 24338 Aug 19  2012 enough.c
-rw-r--r-- 1 root root 16888 Jan  1  2017 example.c
-rw-r--r-- 1 root root  8594 Jun 13  2005 fitblk.c
-rw-r--r-- 1 root root 25943 Oct 30  2016 gun.c
-rw-r--r-- 1 root root 16977 Oct 12  2012 gzappend.c
-rw-r--r-- 1 root root 14132 Aug 14  2012 gzjoin.c
-rw-r--r-- 1 root root 41479 Jan  1  2017 gzlog.c
-rw-r--r-- 1 root root  4557 Aug 19  2012 gzlog.h
-rw-r--r-- 1 root root 24731 Oct 14  2022 infcover.c
-rw-r--r-- 1 root root 15851 Oct 14  2022 minigzip.c
-rw-r--r-- 1 root root  1817 Jan 17  2010 README.examples
-rw-r--r-- 1 root root 29824 Dec 11  2005 zlib_how.html
-rw-r--r-- 1 root root  6323 Dec 11  2005 zpipe.c
-rw-r--r-- 1 root root 15438 Oct 30  2016 zran.c

/usr/share/doc/zstd:
total 36
-rw-r--r-- 1 root root 1891 Mar 24  2022 changelog.Debian.gz
-rw-r--r-- 1 root root  244 Dec 19  2020 CODE_OF_CONDUCT.md
-rw-r--r-- 1 root root 9335 Dec 19  2020 CONTRIBUTING.md.gz
-rw-r--r-- 1 root root 5268 Oct  6  2021 copyright
-rw-r--r-- 1 root root 4068 Dec 19  2020 README.md.gz
-rw-r--r-- 1 root root 1859 Dec 19  2020 TESTING.md

Conclusion

The ls command is an indispensable tool in the Linux operating system, enabling users to list files and directories in a specified location while providing valuable insights into permissions and ownership. When you execute ls -l, the command reveals detailed information, including file type, permissions, hard links, owner, group, size, modification date, and name. To enhance your file management skills, you can employ various options, such as ls -lh for human-readable file sizes, ls -a to display hidden files, and ls -t to sort entries by modification time. Moreover, the -R option enables a recursive listing, exploring subdirectories.


Support DTV Linux

Click on each book below to review & buy on Amazon. As an Amazon Associate, I earn from qualifying purchases.

NordVPN ®: Elevate your online privacy and security. Grab our Special Offer to safeguard your data on public Wi-Fi and secure your devices. I may earn a commission on purchases made through this link.