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 combine cd
and ls
in Fish Shell using the timeout
command. 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:
nano ~/.config/fish/config.fish
Add the following 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
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 ifcd
is successful.timeout 1s ls -l
: Runsls -l
with a 1-second timeout. Ifls
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 cd $argv; and timeout 1s ls -l 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:
source ~/.config/fish/config.fish
Step 3: Use the cdls
Command
Now, you can use the cdls
command to change directories and list their contents safely:
cdls /path/to/directory
Example:
cdls enlightenment/sources/e26/
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:
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
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:
function cd builtin cd $argv[1] and begin echo "Changed to directory: $PWD" timeout 1s ls -l end end
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:
abbr --add cd 'cd $argv; and timeout 1s ls -l'
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
:
function cdls builtin cd $argv[1] and begin echo "Changed to directory: $PWD" timeout 1s exa -l end end
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:
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:
source ~/.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:
abbr --erase cd
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:
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: