How do I change the default startup directory in Cygwin?

37

6

Whenever I start cygwin, I need to cd several levels down to the only directory that I care about when using cygwin.

How can I get cygwin to start in this particular directory by default...i.e., each time I start Cygwin it will be in that directory already?

I found some things that may be it, or close, but I don't understand them, and there's really no results when I search for these (n00b) terms. Any help would be great!

Matt

Posted 2012-02-10T14:59:40.480

Reputation: 834

Just a little tip: using cygwin you don't need to cd several times - you can do it with one command (suggestions will still appear for subdirectories). – Line – 2018-01-04T07:59:03.620

Answers

49

Just add

cd /path/to/directory/you/care/about

to the bottom of your ~/.bashrc file.

garyjohn

Posted 2012-02-10T14:59:40.480

Reputation: 29 085

1where is that ~/.bashrc file in windows installation – Erdem Ece – 2014-11-17T12:02:44.957

4It depends on how you installed Cygwin, but the Windows path to your Cygwin home directory is usually C:\cygwin\home\%USERNAME%, which would make the Windows path to ~/.bashrc C:\cygwin\home\%USERNAME%\.bashrc. – garyjohn – 2014-11-17T19:55:14.793

what if the dafault home directory is a directory where I don't want to put any file. – Arturo – 2015-03-14T15:56:51.193

@Arturo I think you mess up two things - your user home directory and Cygwin home directory. .bashrc file is anyway there if you install Cygwin. – Line – 2018-01-13T10:12:42.640

3

Define "start cygwin"? You can run cygwin servers when you power your machine up, but I'm assuming that's not what you mean.

If you mean, end up in a specific folder every time you open a shell prompt, you can put that in your $HOME/.bashrc

Edit your $HOME/.bashrc using a cygwin editor (one that uses UNIX line endings), add a line

cd /whatever/directory/you/want

If you're constantly going to this directory, you're probably better off making an alias as well.

In your $HOME/.bashrc put:

alias GT='cd /whatever/directory/you/want'

Call it whatever you want; I just used GT for "go there", and I uppercase because it makes it easy to separate from builtin commands, which are never capitalized.

Rich Homolka

Posted 2012-02-10T14:59:40.480

Reputation: 27 121

2

in the last line of your .bash_profile (or .profile), put cd your/dirctory/here

glenn jackman

Posted 2012-02-10T14:59:40.480

Reputation: 18 546

0

This solution doesn't require editing .bashrc:

Below is a snippet from the official docs on mkpasswd :

For example, this command:

Example 3.11. Using an alternate home root

$ mkpasswd -l -p "$(cygpath -H)" > /etc/passwd

would put local users home directories in the Windows 'Profiles' directory.

(answered copied from similar question on stackoverflow because I like this solution more that those here)

Line

Posted 2012-02-10T14:59:40.480

Reputation: 111

0

python script

!!before use add .bashrc any string to the end!!

use name_script.py c:\path

path_bachrc - path to .bashrc

cmd - path to cygwin.bat

#***********************************************#
#   gangelXXX@gmail.com                         #
#***********************************************#
import argparse
import subprocess
import os

path_bachrc = 'c:/PP/cygwin/home/adm/.bashrc'
cmd = 'c:\PP\cygwin\Cygwin.bat'

def delEndLineFromFile(filename):
    with open(filename, 'r') as f:
        aList = f.readlines()

    bList = aList[0:-1]

    with open(filename, 'w') as fd:
        fd.writelines(bList)


parser = argparse.ArgumentParser()
parser.add_argument("newPath", type=str, help="New path in .bachrc cygwin")
args = parser.parse_args();

delEndLineFromFile(path_bachrc);

p = args.newPath;
pNew = 'cd /cygdrive/' + p[:1] + p[2:].replace('\\', '/')
print(pNew)

with open(path_bachrc, 'a') as f:
    f.write(pNew)

PIPE = subprocess.PIPE
p = subprocess.Popen(cmd, shell = True)

user236976

Posted 2012-02-10T14:59:40.480

Reputation: 1

2Please explain. – Roney Michael – 2013-07-10T04:11:12.687

python script use name_script.py c:\path\ – user236976 – 2013-07-10T05:25:10.083

path_bachrc - path to .bashrc – user236976 – 2013-07-10T05:25:49.790

cmd - path to cygwin.bat – user236976 – 2013-07-10T05:26:06.853