Zsh function to list file tree

When navigating through directories on the command line, it’s often helpful to see the structure of directories and subdirectories in a "tree" format. While there are plenty of tools out there, such as tree, sometimes you might want a simple custom solution integrated directly into your shell. In this post, we’ll walk through creating a simple Zsh function to list a directory’s structure in a tree format.

Creating a Zsh Function to List File Trees


If you're using Zsh as your default shell, adding a custom function to your .zshrc file can help you create a quick and efficient command for viewing directories in a tree structure. This function works similarly to the popular tree command but is built using ls and sed, making it easy to integrate directly into your Zsh environment.

Step 1: Add the lst() Function to .zshrc

The first step is to define the lst() function inside your .zshrc file. This function takes an optional argument, which can be any directory path, and lists the directory tree starting from that point. If no directory is specified, it defaults to the current directory.

Open your .zshrc file using your preferred text editor and add the following function:

lst() {
    if [ "$1" != "" ] # or better, if [ -n "$1" ]
    then
        ls -R $1 | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/   /' -e 's/-/|/'
    else
        ls -R | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/   /' -e 's/-/|/'
    fi
}


Step 2: Reload Your Zsh Configuration

After adding the function, you'll need to reload your .zshrc file for the changes to take effect. You can do this by running the following command in your terminal:

sh
source ~/.zshrc

Step 3: Using the lst() Function

Once you've added the function to your .zshrc, you can start using it right away. The lst() function allows you to list all the subdirectories in a specified directory in a tree format. Here’s how to use it:

To list the subdirectories of a folder (e.g., spec), run:
sh
lst spec

This command will generate a tree-like structure that lists all the subdirectories inside the spec folder.

If you want to see the tree structure of the current directory, simply run:
sh
lst

The lst() function will output the folder structure, making it easier to understand the hierarchy of your directories.

Example Output

Let’s say you have the following directory structure:

bash
spec/
├── controllers/
│   └── users/
├── models/
└── views/
Running the command lst spec would produce output similar to the following:

css
   spec
   |--controllers
   |   |--users
   |--models
   |--views
This simple tree structure makes it easy to visualize the relationships between directories and subdirectories.

How It Works

Let’s break down the function step-by-step:

  1. ls -R $1: The ls -R command recursively lists the contents of the directory (or current directory if no argument is provided).
  2. grep ":$": Filters the results to only show directory names (lines ending with :).
  3. sed Commands:
    • 's/:$//': Removes the colon at the end of each directory name.
    • 's/[^-][^\/]*\//--/g': Replaces forward slashes (/) with dashes (--), converting the structure into a tree-like format.
    • 's/^/ /': Adds indentation to the directory tree.
    • 's/-/|/': Replaces dashes with vertical pipes (|), further enhancing the tree format.

Step 4: Customize the lst() Function

You can easily customize the lst() function to suit your needs. For example, you can change the formatting of the tree structure or add additional options for listing files. If you frequently need to list files as well as directories, you could modify the function to include files by removing the grep filter that only shows directories.

Alternative: Using the tree Command

If you prefer, you can use the tree command, which is designed specifically for displaying directory structures. tree is a widely available utility and can be installed on most Unix-like systems. To install tree, use one of the following commands depending on your system:

On Ubuntu or Debian-based systems:
sh
sudo apt-get install tree

On macOS:
sh
brew install tree

Once installed, you can use the tree command to generate similar output:
sh
tree spec

The tree command provides more features out of the box, such as the ability to display file sizes, permissions, and more. However, if you prefer to keep things simple or if tree is not available, the lst() function is a great lightweight alternative.

Conclusion

The Zsh function we’ve created provides a simple and efficient way to list directory structures in a tree format. By adding the lst() function to your .zshrc file, you can quickly visualize the subdirectories of any folder on your system. This can be incredibly useful for navigating complex directory structures and understanding the hierarchy of files and folders in your projects.

If you're looking for more functionality, consider using the tree command for more advanced features. However, for lightweight, custom solutions integrated directly into your Zsh shell, the lst() function is a perfect choice.

FAQs


1. What is the lst() function used for?

The lst() function is a custom Zsh function that displays the directory structure in a tree format. It uses ls and sed to format the output, making it easy to visualize folder hierarchies directly in the terminal.

2. How do I add the lst() function to my Zsh configuration?

You can add the lst() function by editing your .zshrc file and pasting the function definition into the file. After that, run source ~/.zshrc to reload the configuration and make the function available.

3. Can I customize the output of the lst() function?

Yes, you can customize the function by modifying the sed commands that format the output. You can change the tree structure, add more details, or even include files in the listing.

4. What is the alternative to using the lst() function?

The tree command is a widely available utility that displays directory structures in a tree format. It offers more features than the lst() function and can be installed on most Unix-like systems.

5. Can I use the lst() function on systems other than Zsh?

The lst() function is written for Zsh, but it can be easily adapted for other shells like Bash by adding it to the appropriate configuration file (.bashrc for Bash).

By integrating the lst() function into your Zsh workflow, you can enhance your command-line experience and make directory navigation much easier.

Tino

Get our stories delivered

From us to your inbox weekly.