Did you know you can make Bash automatically run ls every time you use cd? In this video, I show a simple but powerful Bash trick that improves your Linux command-line workflow by automatically listing files whenever you change directories.
We start by creating a custom cd function in Bash, then I walk through exactly how it works so you understand what’s happening behind the scenes. This approach helps you learn more about Bash functions, shell behavior, and how to customize your terminal in a practical, real-world way. Once you’ve set this up, navigating directories becomes faster and more informative, especially when working in complex file structures.
As a bonus, I also demonstrate how to create a reusable extract function that can automatically unpack many common archive formats from the command line, saving you even more time.
If you’re looking for useful Bash tips, want to customize your Linux terminal, or enjoy learning small tricks that improve productivity, this video is for you.
Video-specific Links
cd Trick
Intro
Hello and welcome back to Learn Linux TV!
In this article, I’m going to show you a quick trick that will be useful for any of you that spend a decent amount of time in the terminal. This is something I use personally, and totally love. It actually addresses something that almost every Linux user is guilty of. I’ll show you what I mean.
Here in my terminal, I’m going to change into another directory. And as you can see, I used ls to list the contents of this directory. What’s interesting about this is that most Linux users will use ls after switching directories habitually, whether they needed to see a directory listing or not. Now, you could adjust your muscle memory and only use ls when you actually need it, but why fight it?
Now, check this out. I’ve implemented the tweak I’m going to show you, and watch what happens when I change directories now.
Automatic ls. In fact, every time I change directories, I’m automatically going to see a directory listing. And while this tweak may not be desirable for everyone, I’m sure a lot of you are going to love it.
So, what I’m going to do is show you how to implement this in your terminal config. It’s a simple and quick trick, so, what I’m going to do is show you how to implement this in your terminal config. It’s a simple and quick trick! Ok let’s jump right into it!
Implementation
To implement this tweak, it’s actually quite simple. In the description below I’ll have a link to a block of code, and what you’re going to do is visit that link and copy the contents.
Once you’ve done that, open up a terminal, and we’ll edit the .bashrc file.
nano ~/.bashrc
Inside the file, we’ll place the following block of code:
# Show a directory listing when using 'cd'
function cd() {
new_directory="$*";
if [ $# -eq 0 ]; then
new_directory=${HOME};
fi;
builtin cd "${new_directory}" && /bin/ls -lhF --time-style=long-iso --color=auto --ignore=lost+found
}
And it’s literally that simple. Since the bash configuration file is read when you start your shell, the change will take effect the next time you open a terminal. If you’re using SSH, you can log out and log in again.
To speed up the process so you won’t have to log out or close your terminal to test it, we can run the following command:
exec bash
And now, the change should be effective. As you can see, I get an automatic ls every time I use cd.
To understand better what’s going on and how this works, let’s break down the code. What we have here is a function, and I’ve covered functions in my bash scripting series. It’s a full course that’s available here on YouTube for free. But to summarize it, a function in bash is a block of code you can call anytime. You can name your function whatever you want, but in this case I named it “cd” – which actually ends up overriding the default cd command.
Inside the directory, we’re declaring a variable named new_directory, which will store the directory that we’re changing into. It’s also checking to see if we’ve added no arguments, meaning if we use cd without specifying a directory – it’ll default to our home directory. This is intended to mimic the default behavior of the cd command, where you can simply type “cd” with no arguments to go back to y our home directory.
Near the end, we have an ls command. To understand better how that ls command is structured, we can enter it by itself:
/bin/ls -lhF --time-style=long-iso --color=auto --ignore=lost+found
And this variation of the ls command includes all of my favorite options. Here I’m using options -lhF, with -l enabling a long listhing, -h gives us human-readable file sizes, and the -F option appends an indicator to the end of files, such as including a slash after directory names. The –time-style option sets the date format, and I use this format because I like to see dates with the year first. It just makes more sense to me. The –color option will enable colorized output, and the –ignore option will hide files that would normally show up when we enter the ls command, but we’d rather not see.
On your end, you can customize the ls portion of the function however you normally use the ls command, with whatever your favorite options end up being.
The ls command is preceded by the following:
builtin cd "${new_directory}" &&
And basically what that does is force the cd command within the function to be your actual built-in cd command, and not this function itself. ${new_directory} sets up a subshell, which returns our desired directory. Putting this together, you’ll now have the ls command entered automatically every time you change directories.
Let me give you another function that I find useful:
function extract () {
if [ -f $1 ] ; then
case $1 in
*.tar.bz2) tar xjvf $1 ;;
*.tar.gz) tar xzvf $1 ;;
*.tar.xz) tar xvf $1 ;;
*.bz2) bzip2 -d $1 ;;
*.rar) unrar2dir $1 ;;
*.gz) gunzip $1 ;;
*.tar) tar xf $1 ;;
*.tbz2) tar xjf $1 ;;
*.tgz) tar xzf $1 ;;
*.zip) unzip2dir $1 ;;
*.Z) uncompress $1 ;;
*.7z) 7z x $1 ;;
*.ace) unace x $1 ;;
*) echo "'$1' cannot be extracted via extract()" ;;
esac
else
echo "'$1' is not a valid file"
fi
}
And this one is super-useful. If you add this function to your .bashrc file, it’ll make extracting compressed archives a lot easier. Regardless of what type of archive it is, you can use the extract function like it’s own command to use the appropriate options to extract it.
Unlike my cd function, which I came up with myself, I can’t claim responsibility for the extract function, I found that online over a decade ago and I’ve been using it ever since. It’s just another example of how much you can fine-tune your terminal configuration.
And, that’s about it – although it may not be for everyone, the cd function is super useful to me. It almost makes the cd command operate as a file manager. The extract function is quite useful too.


