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:
- Performance Issues: Listing millions of files can take a long time and use a lot of CPU and memory.
- Unresponsive Shell: Your terminal might freeze while trying to list the files.
- 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.
- builtin cd $argv[1]: Changes to the specified directory.
- and begin ... end: Ensures the following commands only run if cd is successful.
- timeout 1s ls -l: Runs ls -l with a 1-second timeout. If ls takes longer than 1 second, it’s terminated.
- 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/
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!

The security models of Linux and Windows each have their own advantages. Linux provides flexibility and customizability, enabling security through user permissions, file system permissions, and SELinux/AppArmor. Windows focuses on user-friendliness and relies on WindowsDefender, UAC, firewall and BitLocker to ensure security.

Linux and Windows differ in hardware compatibility: Windows has extensive driver support, and Linux depends on the community and vendors. To solve Linux compatibility problems, you can manually compile drivers, such as cloning RTL8188EU driver repository, compiling and installing; Windows users need to manage drivers to optimize performance.

The main differences between Linux and Windows in virtualization support are: 1) Linux provides KVM and Xen, with outstanding performance and flexibility, suitable for high customization environments; 2) Windows supports virtualization through Hyper-V, with a friendly interface, and is closely integrated with the Microsoft ecosystem, suitable for enterprises that rely on Microsoft software.

The main tasks of Linux system administrators include system monitoring and performance tuning, user management, software package management, security management and backup, troubleshooting and resolution, performance optimization and best practices. 1. Use top, htop and other tools to monitor system performance and tune it. 2. Manage user accounts and permissions through useradd commands and other commands. 3. Use apt and yum to manage software packages to ensure system updates and security. 4. Configure a firewall, monitor logs, and perform data backup to ensure system security. 5. Troubleshoot and resolve through log analysis and tool use. 6. Optimize kernel parameters and application configuration, and follow best practices to improve system performance and stability.

Learning Linux is not difficult. 1.Linux is an open source operating system based on Unix and is widely used in servers, embedded systems and personal computers. 2. Understanding file system and permission management is the key. The file system is hierarchical, and permissions include reading, writing and execution. 3. Package management systems such as apt and dnf make software management convenient. 4. Process management is implemented through ps and top commands. 5. Start learning from basic commands such as mkdir, cd, touch and nano, and then try advanced usage such as shell scripts and text processing. 6. Common errors such as permission problems can be solved through sudo and chmod. 7. Performance optimization suggestions include using htop to monitor resources, cleaning unnecessary files, and using sy

The average annual salary of Linux administrators is $75,000 to $95,000 in the United States and €40,000 to €60,000 in Europe. To increase salary, you can: 1. Continuously learn new technologies, such as cloud computing and container technology; 2. Accumulate project experience and establish Portfolio; 3. Establish a professional network and expand your network.

The main uses of Linux include: 1. Server operating system, 2. Embedded system, 3. Desktop operating system, 4. Development and testing environment. Linux excels in these areas, providing stability, security and efficient development tools.

The Internet does not rely on a single operating system, but Linux plays an important role in it. Linux is widely used in servers and network devices and is popular for its stability, security and scalability.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

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),

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Notepad++7.3.1
Easy-to-use and free code editor

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment