search
HomeSystem TutorialLINUXHow To Change Directory And List Files In One Command In Fish Shell

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

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

Add the following function:

nano ~/.config/fish/config.fish

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

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

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

Example:

cdls /path/to/directory

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/

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

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

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

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

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

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
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
Explain the role of system calls in Linux and Windows.Explain the role of system calls in Linux and Windows.May 16, 2025 am 12:12 AM

System calls are implemented in Linux and Windows through different mechanisms: 1) In Linux, system calls are implemented through interrupt mechanisms, involving context switching; 2) In Windows, the "fast system calls" mechanism is used to reduce the context switching overhead.

How to Use 'next' Command with Awk in Linux - Part 6How to Use 'next' Command with Awk in Linux - Part 6May 15, 2025 am 10:43 AM

In this sixth installment of our Awk series, we will explore the next command, which is instrumental in enhancing the efficiency of your script executions by skipping redundant processing steps.What is the next Command?The next command in awk instruc

How to Efficiently Transfer Files in LinuxHow to Efficiently Transfer Files in LinuxMay 15, 2025 am 10:42 AM

Transferring files in Linux systems is a common task that every system administrator should master, especially when it comes to network transmission between local or remote systems. Linux provides two commonly used tools to accomplish this task: SCP (Secure Replication) and Rsync. Both provide a safe and convenient way to transfer files between local or remote machines. This article will explain in detail how to use SCP and Rsync commands to transfer files, including local and remote file transfers. Understand the scp (Secure Copy Protocol) in Linux scp command is a command line program used to securely copy files and directories between two hosts via SSH (Secure Shell), which means that when files are transferred over the Internet, the number of

10 Most Popular Linux Desktop Environments of All Time10 Most Popular Linux Desktop Environments of All TimeMay 15, 2025 am 10:35 AM

One fascinating feature of Linux, in contrast to Windows and Mac OS X, is its support for a variety of desktop environments. This allows desktop users to select the most suitable and fitting desktop environment based on their computing requirements.A

How to Install LibreOffice 24.8 in Linux DesktopHow to Install LibreOffice 24.8 in Linux DesktopMay 15, 2025 am 10:15 AM

LibreOffice stands out as a robust and open-source office suite, tailored for Linux, Windows, and Mac platforms. It boasts an array of advanced features for handling word documents, spreadsheets, presentations, drawings, calculations, and mathematica

How to Work with PDF Files Using ONLYOFFICE Docs in LinuxHow to Work with PDF Files Using ONLYOFFICE Docs in LinuxMay 15, 2025 am 09:58 AM

Linux users who manage PDF files have a wide array of programs at their disposal. Specifically, there are numerous specialized PDF tools designed for various functions.For instance, you might opt to install a PDF viewer for reading files or a PDF edi

How to Filter Command Output Using Awk and STDINHow to Filter Command Output Using Awk and STDINMay 15, 2025 am 09:53 AM

In the earlier segments of the Awk command series, our focus was primarily on reading input from files. However, what if you need to read input from STDIN?In Part 7 of the Awk series, we will explore several examples where you can use the output of o

Clifm - Lightning-Fast Terminal File Manager for LinuxClifm - Lightning-Fast Terminal File Manager for LinuxMay 15, 2025 am 09:45 AM

Clifm stands out as a distinctive and incredibly swift command-line file manager, designed on the foundation of a shell-like interface. This means that users can engage with their file system using commands they are already familiar with.The choice o

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Clair Obscur: Expedition 33 - How To Get Perfect Chroma Catalysts
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools