linuxcd

Change to a different folder automatically on Linux once you login


How can I change to a different folder, e.g. foo/bar, automatically once I login into a Linux machine?

cd foo/bar

Solution

  • For logging shell you must modify .bash_profile. Simply put cd foo/bar at the end of this file.

    Extra information: https://linuxize.com/post/bashrc-vs-bash-profile/ .bash_profile vs .bashrc


    With your distro, if .bash_profile does not exist, you can modify .profile instead:

    # ~/.profile: executed by the command interpreter for login shells.
    # This file is not read by bash(1), if ~/.bash_profile or ~/.bash_login
    # exists.
    

    If you want to test it works from a terminal, but logged as another_user, you can do:

    su -l user_to_test
    

    The important part is the -l flag:

      -, -l, --login
              Start the shell as a login shell with an environment 
              similar to a real login: