I was just on a system that didn't have this hack, and it reminded me how useful it is, so I thought I'd share it for others. Maybe you all can reply with your own shell tricks.
Some of you may know about the pushd
and popd
commands. pushd
is like cd
, but keeps track of where you've been. Then you can popd
back as many levels as you want. It's like cd
and cd -
but with a history bigger than one.
The big issue I have with pushd
and popd
is that in order for popd
to work, I have to remember to pushd
first. But, like many of you, by default, I navigate the terminal using the cd
command instead. By the time that I realize popd
will be useful, it's already too late because I didn't remember to pushd
up front.
Now the hack. Just toss this in your ~/.bashrc
:
function cd
{
if [ $# -eq 0 ]; then
pushd ~ > /dev/null
elif [ " $1" = " -" ]; then
pushd "$OLDPWD" > /dev/null
else
pushd "$@" > /dev/null
fi
}
And start a new shell. From that point forward, cd
will actually execute pushd
under the hood, and popd
will always be available when you need it.
Do you have a tip or trick that you'd like to share? If so, complete the Join Our Community form and join our ever-growing list of authors.
[ Free download: Advanced Linux commands cheat sheet. ]
About the author
Ray is a Desktop Software Engineer who works on GNOME, Fedora, and Red Hat Enterprise Linux.
Browse by channel
Automation
The latest on IT automation for tech, teams, and environments
Artificial intelligence
Updates on the platforms that free customers to run AI workloads anywhere
Open hybrid cloud
Explore how we build a more flexible future with hybrid cloud
Security
The latest on how we reduce risks across environments and technologies
Edge computing
Updates on the platforms that simplify operations at the edge
Infrastructure
The latest on the world’s leading enterprise Linux platform
Applications
Inside our solutions to the toughest application challenges
Original shows
Entertaining stories from the makers and leaders in enterprise tech
Products
- Red Hat Enterprise Linux
- Red Hat OpenShift
- Red Hat Ansible Automation Platform
- Cloud services
- See all products
Tools
- Training and certification
- My account
- Customer support
- Developer resources
- Find a partner
- Red Hat Ecosystem Catalog
- Red Hat value calculator
- Documentation
Try, buy, & sell
Communicate
About Red Hat
We’re the world’s leading provider of enterprise open source solutions—including Linux, cloud, container, and Kubernetes. We deliver hardened solutions that make it easier for enterprises to work across platforms and environments, from the core datacenter to the network edge.
Select a language
Red Hat legal and privacy links
- About Red Hat
- Jobs
- Events
- Locations
- Contact Red Hat
- Red Hat Blog
- Diversity, equity, and inclusion
- Cool Stuff Store
- Red Hat Summit