Home > System Tutorial > LINUX > How To Change Directory And List Files In One Command In Fish Shell

How To Change Directory And List Files In One Command In Fish Shell

William Shakespeare
Release: 2025-03-05 10:25:10
Original
308 people have browsed it

If you often use the command line on Linux, you’ve probably wished for a quicker way to navigate directories and view their contents.

Typically, this involves running cd to change directories and then ls to list files. While combining these commands into one seems like a neat idea, it can cause problems when dealing with directories containing a lot of files.

In this guide, we’ll show you how to safely combinecdandlsin Fish Shell using thetimeoutcommand. This allows you to quickly change directories and list files in one command while ensuring your shell remains responsive, even in directories with a large number of files.

Table of Contents

Why Combine cd and ls?

By default, when you use cd to change directories, Fish (or any other shell) does not display the files in the new location. Running ls separately every time can be repetitive. Combining these commands makes it easier to see the contents of a directory without extra steps.

However, there’s a catch: listing the contents of a directory with millions of files can cause your shell to hang, consuming excessive resources and making your terminal unresponsive.

No worries! In the following steps, we will provide all the possible ways to combine cd and ls commands.

Method 1: One Time Use Command

If you just need to do this once, you can use the following command:

cd /path/to/directory; and ls
Copy after login
Copy after login
Copy after login

Replace /path/to/directory with the folder you want to open. The and ensures ls runs only if cd is successful.

The Problem with Automatically Listing Directory Contents

Automatically running ls after every cd can lead to:

  1. Performance Issues: Listing millions of files can take a long time and use a lot of CPU and memory.
  2. Unresponsive Shell: Your terminal might freeze while trying to list the files.
  3. Unnecessary Output: Sometimes, you don’t need to see the contents of a directory immediately after navigating to it.

To avoid these issues, we need a smarter way to combine cd and ls.

Method 2: Create a Custom Fish Function with timeout

The timeout command allows you to run a command with a time limit. If the command doesn’t complete within the specified time, it’s terminated. This is perfect for preventing ls from hanging in directories with too many files.

Here’s how to create a Fish shell function that combines cd and ls safely using timeout.

Step 1: Create the Function

Open your Fish shell configuration file (~/.config/fish/config.fish) in a text editor:

cd /path/to/directory; and ls
Copy after login
Copy after login
Copy after login

Add the following function:

nano ~/.config/fish/config.fish
Copy after login
Copy after login

Here's the breakdown of the above function.

  1. builtin cd $argv[1]: Changes to the specified directory.
  2. and begin ... end: Ensures the following commands only run if cd is successful.
  3. timeout 1s ls -l: Runs ls -l with a 1-second timeout. If ls takes longer than 1 second, it’s terminated.
  4. echo "Changed to directory: $PWD": Prints the current directory path for clarity.

You can also use this compact function:

function cdls
    # Change to the specified directory
    builtin cd $argv[1]
    and begin
        # List directory contents with a timeout of 1 second
        echo "Changed to directory: $PWD"
        timeout 1s ls -l
    end
end
Copy after login
Copy after login

Just use any of the above functions. They both perform the same task. If you want to use both functions, simply give each one a unique name.

Step 2: Save and Reload the Configuration

Save the file and reload the Fish shell configuration to apply the changes:

function cdls
    cd $argv; and timeout 1s ls -l
end
Copy after login

Step 3: Use the cdls Command

Now, you can use the cdls command to change directories and list their contents safely:

source ~/.config/fish/config.fish
Copy after login

Example:

cdls /path/to/directory
Copy after login

If the directory contains a manageable number of files, ls -l will complete within 1 second, and you’ll see the listing. If the directory is too large, timeout will kill the ls command after 1 second, preventing your shell from hanging.

Sample Output:

cdls enlightenment/sources/e26/
Copy after login

How To Change Directory And List Files In One Command In Fish Shell

Customizing the Timeout

You can adjust the timeout duration to suit your needs. For example:

  • Use 0.5s for a shorter timeout: timeout 0.5s ls -l
  • Use 2s for a longer timeout: timeout 2s ls -l

Just modify the timeout value in the function.

Optional: Override the Default cd Command

If you want this behavior to apply to the default cd command, redefine cd in your Fish shell configuration:

Changed to directory: /home/ostechnix/enlightenment/sources/e26
total 56
drwxrwxr-x  7 ostechnix ostechnix 4096 Jan 17 19:13 ecrire
drwxrwxr-x 11 ostechnix ostechnix 4096 Jan 17 19:13 edi
drwxrwxr-x 18 ostechnix ostechnix 4096 Jan 17 19:02 efl
drwxrwxr-x 12 ostechnix ostechnix 4096 Jan 17 19:14 eflete
drwxrwxr-x 11 ostechnix ostechnix 4096 Jan 17 19:10 enlightenment
drwxrwxr-x  8 ostechnix ostechnix 4096 Jan 17 19:14 enlightenment-module-forecasts
drwxrwxr-x  8 ostechnix ostechnix 4096 Jan 17 19:14 enlightenment-module-penguins
drwxrwxr-x  7 ostechnix ostechnix 4096 Jan 17 19:14 enlightenment-module-places
drwxrwxr-x  7 ostechnix ostechnix 4096 Jan 17 19:14 entice
drwxrwxr-x  9 ostechnix ostechnix 4096 Jan 17 19:13 enventor
drwxrwxr-x  7 ostechnix ostechnix 4096 Jan 17 19:12 ephoto
drwxrwxr-x  7 ostechnix ostechnix 4096 Jan 17 19:13 evisum
drwxrwxr-x  7 ostechnix ostechnix 4096 Jan 17 19:13 express
drwxrwxr-x  6 ostechnix ostechnix 4096 Jan 17 19:13 rage
Copy after login

Now, every time you use cd, it will automatically list the directory contents with a 1-second timeout.

Method 3: Use an Abbreviation

Fish shell supports abbreviations, which expand into full commands when you type them. To create an abbreviation for cd that includes ls, run:

function cd
    builtin cd $argv[1]
    and begin
        echo "Changed to directory: $PWD"
        timeout 1s ls -l
    end
end
Copy after login

This method is useful because it keeps the original cd command while automatically running ls with a timeout to prevent hangs.

Bonus: Use exa for Faster Listing

If you frequently work with large directories, consider using exa, a modern alternative to ls. exa is faster and more feature-rich, making it better suited for handling directories with many files.

Here’s how to modify the function to use exa:

cd /path/to/directory; and ls
Copy after login
Copy after login
Copy after login

We already have compiled a list of modern alternatives to popular Linux commands. This list provides the best replacements for the old classic Linux Commands. Please visit the following link for more details:

  • The Best Modern Linux Commands For Beginners And Experts

Remove Fish Function

If you don't want to use the fish function cdls anymore, simply remove the lines that you added in the fish configuration file. After removing those lines, reload the fish configuration using command:

nano ~/.config/fish/config.fish
Copy after login
Copy after login

If you have added the abbreviation for cd in Fish shell as shown in the Method 3, tou can remove it using command:

function cdls
    # Change to the specified directory
    builtin cd $argv[1]
    and begin
        # List directory contents with a timeout of 1 second
        echo "Changed to directory: $PWD"
        timeout 1s ls -l
    end
end
Copy after login
Copy after login

This will delete the abbreviation and restore cd to its default behavior. If you want to ensure the abbreviation is permanently removed, check your ~/.config/fish/config.fish file and remove any line that defines abbr --add cd.

Keep Functions in a Separate Directory for Easy Management

As you may have noticed, I saved the function in the Fish configuration file in this tutorial. While this works, it is not the best approach.

Adding more functions can clutter the configuration file, making it harder to manage. To keep it clean, store each Fish function in its own file within a separate directory. For more details, please read the following guide:

  • How To Manage Functions In Fish Shell On Linux

Conclusion

Combining cd and ls in Fish shell is a great way to simplify command line navigation in Linux. By using the timeout command, you can perform automatic directory listing without risking performance issues or an unresponsive shell.

Whether you use a one-time command, a custom function, or an abbreviation, adding a timeout ensures you avoid performance issues when dealing with large directories. Using these methods, you can make navigating directories in Fish shell faster and more efficient.

Related Read:

  • [Bash Tips] How To cd and ls in One Command
  • How To Navigate Directories Faster In Linux

The above is the detailed content of How To Change Directory And List Files In One Command In Fish Shell. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template