订阅内容
Linux 

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. ]


关于作者

Ray is a Desktop Software Engineer who works on GNOME, Fedora, and Red Hat Enterprise Linux.

Read full bio
UI_Icon-Red_Hat-Close-A-Black-RGB

按频道浏览

automation icon

自动化

有关技术、团队和环境 IT 自动化的最新信息

AI icon

人工智能

平台更新使客户可以在任何地方运行人工智能工作负载

open hybrid cloud icon

开放混合云

了解我们如何利用混合云构建更灵活的未来

security icon

安全防护

有关我们如何跨环境和技术减少风险的最新信息

edge icon

边缘计算

简化边缘运维的平台更新

Infrastructure icon

基础架构

全球领先企业 Linux 平台的最新动态

application development icon

应用领域

我们针对最严峻的应用挑战的解决方案

Original series icon

原创节目

关于企业技术领域的创客和领导者们有趣的故事