Skip to content

Navigating the Linux Filesystem A Short Guide to Listing Files and Directories

Published: at 06:06 AM

Introduction

Navigating the Linux filesystem is a foundational skill for any Linux user, and mastering the art of listing files and directories is a key component of this journey. In this short guide, we will explore various commands and techniques for effectively listing files and directories in Linux, focusing on the versatile ls and tree commands. Whether you’re a novice user or a seasoned Linux veteran, this guide will equip you with the knowledge and skills to navigate the filesystem with confidence.

Table of contents

Open Table of contents

Understanding the ls Command

The ls command is the cornerstone of file and directory listing in Linux. Let’s explore its various options and capabilities:

Basic Usage

The most basic usage of the ls command is to list files and directories in the current directory:

ls

To list files in other directories, add path as a parameter:

ls /

This output lists the contents of the root directory (/):

bin   dev  home  lib32  lost+found  mnt  proc  run   srv  tmp  var
boot  etc  lib   lib64  media       opt  root  sbin  sys  usr

Listing files in user home directory:

ls ~

This output lists the contents of the home directory (~ or /home/username, where username is your username):

Desktop  Documents  Downloads  Music  Pictures  Public  Templates  Videos

List files in parent directory:

ls ..

The displayed content enumerates the items within the parent directory. For instance, supposing our current location is a home directory, invoking ls .. reveals the directories situated in the parent directory, which includes other users’ home directories:

user1  user2  user3

To navigate up another level, add ../ after the command: ls ../.. reveals the contents of the directory two levels above.

Listing files recursively

To recursively list the contents of directories and their subdirectories, use -R option.

For example, if you have a directory structure like this:

parent_directory/
    ├── subdirectory1/
   ├── file1.txt
   └── file2.txt
    └── subdirectory2/
        ├── file3.txt
        └── file4.txt

Executing ls -R within the parent_directory would output:

parent_directory/:
subdirectory1
subdirectory2

parent_directory/subdirectory1:
file1.txt
file2.txt

parent_directory/subdirectory2:
file3.txt
file4.txt

Detailed Listing

For a more detailed listing that includes file permissions, ownership, size, and modification date, use the -l option:

Example:

ls -l

This command displays files and folders in the long format, information displayed for each file: file mode, number of links, owner name, group name, number of bytes in the file, abbreviated month, day-of-month file was last modified, hour file last modified, minute file last modified, and the pathname:

total 32
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 12 15:23 Desktop
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 12 15:23 Documents
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 12 15:23 Downloads
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 12 15:23 Music
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 12 15:23 Pictures
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 12 15:23 Public
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 12 15:23 Templates
drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 12 15:23 Videos
-rw-r--r-- 1 ubuntu ubuntu  220 Apr 12 15:23 file1.txt
-rw-r--r-- 1 ubuntu ubuntu  220 Apr 12 15:23 file2.txt

Showing Hidden Files

To include hidden files (those starting with a dot .) and display file sizes in a human-readable format, use -h flag.

Example:

ls -lah

The output is similar, but includes hidden files (names starting with the dot . character):

total 72K
drwxr-xr-x 5 ubuntu ubuntu 4.0K Apr 11 10:23 .
drwxr-xr-x 3 root   root   4.0K Apr  9 08:12 ..
-rw-r--r-- 1 ubuntu ubuntu  570 Apr 10 15:32 .bashrc
drwxr-xr-x 2 ubuntu ubuntu 4.0K Apr 10 15:32 .cache
drwx------ 3 ubuntu ubuntu 4.0K Apr 11 09:47 .config
-rw-r--r-- 1 ubuntu ubuntu   18 Apr  9 07:12 .hidden_file
-rw-r--r-- 1 ubuntu ubuntu  32K Apr  9 07:12 .hidden_image.jpg
drwxr-xr-x 2 ubuntu ubuntu 4.0K Apr  9 07:12 .ssh
-rw-r--r-- 1 ubuntu ubuntu  189 Apr  9 07:12 .vimrc
drwxr-xr-x 2 ubuntu ubuntu 4.0K Apr  9 07:12 Desktop
drwxr-xr-x 2 ubuntu ubuntu 4.0K Apr  9 07:12 Documents
drwxr-xr-x 2 ubuntu ubuntu 4.0K Apr  9 07:12 Downloads
drwxr-xr-x 2 ubuntu ubuntu 4.0K Apr  9 07:12 Music
drwxr-xr-x 2 ubuntu ubuntu 4.0K Apr  9 07:12 Pictures
drwxr-xr-x 2 ubuntu ubuntu 4.0K Apr  9 07:12 Public
drwxr-xr-x 2 ubuntu ubuntu 4.0K Apr  9 07:12 Templates
drwxr-xr-x 2 ubuntu ubuntu 4.0K Apr  9 07:12 Videos

Skipping Current and Parent directories

In Unix-like operating systems, every directory contains two special objects denoted by dot characters: . represents the current directory, while .. denotes the parent directory. To hide these default items, employ the -A option:

ls -lAh

Result:

total 72K
-rw-r--r-- 1 ubuntu ubuntu  570 Apr 10 15:32 .bashrc
drwxr-xr-x 2 ubuntu ubuntu 4.0K Apr 10 15:32 .cache
drwx------ 3 ubuntu ubuntu 4.0K Apr 11 09:47 .config
-rw-r--r-- 1 ubuntu ubuntu   18 Apr  9 07:12 .hidden_file
-rw-r--r-- 1 ubuntu ubuntu  32K Apr  9 07:12 .hidden_image.jpg
drwxr-xr-x 2 ubuntu ubuntu 4.0K Apr  9 07:12 .ssh
-rw-r--r-- 1 ubuntu ubuntu  189 Apr  9 07:12 .vimrc
drwxr-xr-x 2 ubuntu ubuntu 4.0K Apr  9 07:12 Desktop
drwxr-xr-x 2 ubuntu ubuntu 4.0K Apr  9 07:12 Documents
drwxr-xr-x 2 ubuntu ubuntu 4.0K Apr  9 07:12 Downloads
drwxr-xr-x 2 ubuntu ubuntu 4.0K Apr  9 07:12 Music
drwxr-xr-x 2 ubuntu ubuntu 4.0K Apr  9 07:12 Pictures
drwxr-xr-x 2 ubuntu ubuntu 4.0K Apr  9 07:12 Public
drwxr-xr-x 2 ubuntu ubuntu 4.0K Apr  9 07:12 Templates
drwxr-xr-x 2 ubuntu ubuntu 4.0K Apr  9 07:12 Videos

Excluding The Group Information

To omit the group information from the output use -o option:

ls -oh

This command provides a listing of files and directories without including group information:

total 72K
-rw-r--r-- 1 ubuntu  570 Apr 10 15:32 .bashrc
drwxr-xr-x 2 ubuntu 4.0K Apr 10 15:32 .cache
drwx------ 3 ubuntu 4.0K Apr 11 09:47 .config
-rw-r--r-- 1 ubuntu   18 Apr  9 07:12 .hidden_file
-rw-r--r-- 1 ubuntu  32K Apr  9 07:12 .hidden_image.jpg
drwxr-xr-x 2 ubuntu 4.0K Apr  9 07:12 .ssh

Sorting and Formatting Options

When it comes to managing files and directories in Linux, sorting and formatting options offer users the flexibility to tailor the presentation of listings according to their preferences. Let’s delve into some of these options.

Sorting by Size or Modification Time

The -S option in the ls command sorts files by their sizes in descending order. This is particularly useful when you want to identify the largest files in a directory.

Example:

ls -Slh

This command lists files and directories, sorted by size, with human-readable file sizes displayed:

total 72K
-rw-r--r-- 1 user user  32K Apr  9 07:12 .hidden_image.jpg
drwxr-xr-x 5 user user 4.0K Apr 11 10:23 .
drwxr-xr-x 3 user user 4.0K Apr  9 08:12 ..
drwxr-xr-x 2 user user 4.0K Apr  9 07:12 Desktop
drwxr-xr-x 2 user user 4.0K Apr  9 07:12 Documents
drwxr-xr-x 2 user user 4.0K Apr  9 07:12 Downloads
drwxr-xr-x 3 user user 4.0K Apr 11 09:47 .config
drwx------ 3 user user 4.0K Apr 11 09:47 .ssh
-rw-r--r-- 1 user user  570 Apr 10 15:32 .bashrc
-rw-r--r-- 1 user user  189 Apr  9 07:12 .vimrc

To sort files by modification time, you can use the -t option. By default, files are sorted in descending order, showing the most recently modified files first. You can reverse this order using the -r option.

Example:

ls -tlr

This command lists files and directories, sorted by modification time in descending order:

total 72K
drwxr-xr-x 2 user user 4.0K Apr  9 07:12 Desktop
drwxr-xr-x 2 user user 4.0K Apr  9 07:12 Documents
drwxr-xr-x 2 user user 4.0K Apr  9 07:12 Downloads
drwxr-xr-x 2 user user 4.0K Apr  9 07:12 Music
drwxr-xr-x 2 user user 4.0K Apr  9 07:12 Pictures
drwxr-xr-x 2 user user 4.0K Apr  9 10:34 Templates
drwxr-xr-x 3 user user 4.0K Apr 11 09:47 config
-rw-r--r-- 1 user user  588 Apr 11 10:23 example.txt

Comma-Separated List

The -m option in the ls command lists files and directories as a comma-separated list. This can be handy when you need a concise representation of files and directories.

Example:

ls -m

This command produces a comma-separated list of files and directories.

Quoted List

Adding the -Q option to the ls command encloses each file or directory name in double quotes. This is useful when dealing with filenames containing spaces or special characters.

Example:

ls -mQ

This command outputs a quoted list of files and directories, with each name enclosed in double quotes.

"Desktop", "Documents", "Downloads", "Music", "Pictures", "Videos"

These options provide users with versatile ways to organize and format file listings, making it easier to navigate and manage files and directories in the Linux environment.

Exploring File Hierarchies with tree

While ls provides a flat list of files and directories, the tree command in Linux offers a powerful way to explore file hierarchies by presenting directory structures in a tree-like format, providing a visual representation of parent-child relationships among files and directories. Let’s delve into its various options and capabilities:

Basic Usage

Without any options, the tree command displays the directory structure starting from the current directory.

Example:

tree

This command produces a tree-like structure of directories and files rooted at the current directory:

.
├── Desktop
   ├── file1.txt
   ├── file2.txt
   └── folder1
       └── file3.txt
├── Documents
   ├── document1.docx
   └── document2.pdf
├── Downloads
   ├── image1.jpg
   ├── image2.png
   └── folder2
       └── image3.jpeg
├── Music
   ├── song1.mp3
   └── song2.wav
├── Pictures
   ├── photo1.jpg
   └── photo2.png
└── Videos
    ├── video1.mp4
    └── video2.mov

6 directories, 12 files

Including Hidden Files

Adding the -a option to the tree command includes hidden files and directories in the output.

Example:

tree -ah

This command displays all files and directories, including hidden ones, in a tree-like structure.

Printing File Types and Permissions

The -p option in the tree command prints file types and permissions alongside file names.

Example:

tree -p

This command displays file types and permissions for each file and directory in the tree.

Printing Owner and Group

Adding the -ug option to the tree command prints the owner and group of each file and directory.

Example:

tree -ug

This command includes the owner and group information in the output tree structure.

Combining Options

You can combine options described above, to customize the output according to your preferences. For example, combining -ughp with --metafirst prints permissions, user, group, and metadata first, similar to ls -lah.

Example:

tree -ughp --metafirst

This command organizes the output with permissions, user, group, and metadata displayed first.

Listing Directories Only

The -d option in the tree command lists directories only, omitting files from the output.

Example:

tree -d

This command displays only directories in the tree-like structure.

Printing Full Paths

Adding the -f option to the tree command prints full paths as a prefix to each file and directory.

Example:

tree -f

This command prefixes each file and directory with its full path in the output tree.

Printing files up to 3 levels deep

Using the -L level option allows to define maximum depth of the directory tree.

Example:

tree -L 3

This command lists files and directories three levels deep.

Pattern matching

By adding the -P option, tree command lists only those files that match the wild-card pattern.

Example:

tree -P *.sh

This command lists only files with .sh extension.

Using .gitignore file for filtering the results

When you supply the --gitignore option, the command will exclude files and directories listed in the .gitignore file.

Example:

tree --gitignore

Saving output to the file

The -o option tells tree command to send an output to the file.

Example:

tree -o out.txt

These options offer users a flexible and customizable way to explore and visualize file hierarchies in the Linux filesystem using the tree command.

Conclusion

Navigating the Linux filesystem is an essential skill for every Linux user, and mastering the art of listing files and directories is a crucial aspect of this journey. By leveraging the powerful capabilities of commands like ls and tree, users can efficiently explore, organize, and manage their files and directories with ease. With the knowledge gained from this guide, you’ll be well-equipped to navigate the Linux filesystem like a seasoned pro.