The Cookie Machine - Click here to drag window

DUMMY TEXT - Real text set in assets/js/theCookieMachine.js

Views: 4,667     Votes:  15     ✅ Solution
Tags: command-line   cd-command  
Link: 🔍 See Original Answer on Ask Ubuntu 🔗

URL: https://askubuntu.com/q/1086196
Title: How do I prevent 'cd' command from going to home directory?
ID: /2018/10/22/How-do-I-prevent-_cd_-command-from-going-to-home-directory_
Created: October 22, 2018    Edited:  June 12, 2020
Upload: November 24, 2022    Layout:  post
TOC: false    Navigation:  false    Copy to clipboard:  false


Use gedit ~/.bashrc and insert these lines at the bottom:

cd() {
    [[ $# -eq 0 ]] && return
    builtin cd "$@"
}

Open a new terminal and now when you type cd with no parameters you simply stay in the same directory.


TL;DR

If you want to be really elaborate you can put in a help screen when no parameters are passed:

$ cd

cd: missing operand

Usage:

    cd ~            Change to home directory. Equivelent to 'cd /home/$USER'

    cd -            Change to previous directory before last 'cd' command

    cd ..           Move up one directory level
    
    cd ../..        Move up two directory levels
    
    cd ../sibling   Move up one directory level and change to sibling directory

    cd /path/to/    Change to specific directory '/path/to/' eg '/var/log'

The expanded code to accomplish this is:

cd() {
    if [[ $# -eq 0 ]] ; then
        cat << 'EOF'

cd: missing operand

Usage:

    cd ~            Change to home directory. Equivelent to 'cd /home/$USER'

    cd -            Change to previous directory before last 'cd' command

    cd ..           Move up one directory level
    
    cd ../..        Move up two directory levels
    
    cd ../sibling   Move up one directory level and change to sibling directory

    cd /path/to/    Change to specific directory '/path/to/' eg '/var/log'

EOF
        return
    fi

    builtin cd "$@"
}
⇧ How to put the laptop to sleep/hibernatation by closing the LCD lid? Sleep mode drains battery very fast  ⇩