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. ]
Über den Autor
Ray is a Desktop Software Engineer who works on GNOME, Fedora, and Red Hat Enterprise Linux.
Ähnliche Einträge
Behind the scenes of RHEL 10, part 3
Alliander modernises its electricity grid with Red Hat for long-term reliability in balance with rapid innovation
The Overlooked Operating System | Compiler: Stack/Unstuck
Linux, Shadowman, And Open Source Spirit | Compiler
Nach Thema durchsuchen
Automatisierung
Das Neueste zum Thema IT-Automatisierung für Technologien, Teams und Umgebungen
Künstliche Intelligenz
Erfahren Sie das Neueste von den Plattformen, die es Kunden ermöglichen, KI-Workloads beliebig auszuführen
Open Hybrid Cloud
Erfahren Sie, wie wir eine flexiblere Zukunft mit Hybrid Clouds schaffen.
Sicherheit
Erfahren Sie, wie wir Risiken in verschiedenen Umgebungen und Technologien reduzieren
Edge Computing
Erfahren Sie das Neueste von den Plattformen, die die Operations am Edge vereinfachen
Infrastruktur
Erfahren Sie das Neueste von der weltweit führenden Linux-Plattform für Unternehmen
Anwendungen
Entdecken Sie unsere Lösungen für komplexe Herausforderungen bei Anwendungen
Virtualisierung
Erfahren Sie das Neueste über die Virtualisierung von Workloads in Cloud- oder On-Premise-Umgebungen