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
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.
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.
Automatically running ls after every cd can lead to:
To avoid these issues, we need a smarter way to combine cd and ls.
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.
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.
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.
Save the file and reload the Fish shell configuration to apply the changes:
function cdls cd $argv; and timeout 1s ls -l end
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/
You can adjust the timeout duration to suit your needs. For example:
Just modify the timeout value in the function.
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.
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.
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:
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.
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:
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:
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!