Be the first to know and get exclusive access to offers by signing up for our mailing list(s).

Subscribe

We ❤️ Open Source

A community education resource

Automate ls after cd on Linux with this Bash trick

Stop typing ls after every directory change and let this Bash function do it for you.

Ever catch yourself typing ls immediately after cd, every single time, whether you actually need to see the directory contents or not? In this video from Learn Linux TV, you’ll learn how a simple Bash function makes that habit productive instead of wasteful.

Jay walks through a clever Bash customization that automatically runs ls whenever you change directories. The trick involves creating a function in your .bashrc file that overrides the default cd command. The function mimics the standard cd behavior (like returning to your home directory when called with no arguments) while adding an automatic directory listing at the end.

Jay breaks down exactly how the function works. It uses builtin cd to call the actual cd command rather than recursively calling itself, stores the target directory in a variable with a fallback to $HOME, and executes a customized ls command with options like -lh for human-readable sizes, -F to append indicators to filenames, and --color for colorized output. You can tweak the ls section to include whatever options you prefer.

As a bonus, Jay shares a second useful function for extracting compressed archives. The extract function automatically detects the archive type and uses the appropriate extraction command, so you never have to remember whether to use tar, unzip, gunzip, or something else.

Key takeaways

  • Override cd with a Bash function – By naming your function cd, it replaces the default command while still calling builtin cd internally to avoid recursion.
  • Customize the ls command to your taste – The function includes Jay’s preferred ls options, but you can modify it to match your own preferences for date formats, colors, and sorting.
  • The extract function is a time-saver – Instead of remembering extraction commands for different archive types, one function handles .tar.gz, .zip, .7z, and more automatically.

This tweak won’t appeal to everyone, but if you’re already typing ls after every cd anyway, why not automate it? Jay’s walkthrough makes implementation straightforward, even if you’ve never written a Bash function before.

More from Learn Linux TV

The opinions expressed on this website are those of each author, not of the author's employer or All Things Open/We Love Open Source.

Working on something worth sharing? Write for us.

Contribute to We ❤️ Open Source

Help educate our community by contributing a blog post, tutorial, or how-to.

Two World-class Events

If you didn't make it to All Things AI, check out the event summary, and make plans to join us October 19-20 for All Things Open.

Open Source Meetups

We host some of the most active open source meetups in the U.S. Get more info and RSVP to an upcoming event.