John Seed
John Seed

Reputation: 397

Cygwin initial directory in unknown location

I'm usually a Linux command prompt user, now I'm using Windows 10 on my laptop instead, I know... poor choice. So I'm using Cygwin as my terminal on windows, the only problem is when I started it for the first time, it initializes in a directory I don't recognize

![Cygwin upon start]1

I would like to be able to navigate to my Documents and Desktop folders to access where my code files are stored. Below is another screencap of what I see after using 'ls' and 'cd ..' commands.

enter image description here

I've spent far too much time navigating all these directories visible, and I can't seem to find where my files are located in the system. So how do I find them? How can I set Cygwin to always start in the proper directory?

Thanks.

Upvotes: 2

Views: 2596

Answers (2)

dimo414
dimo414

Reputation: 48864

Cygwin creates a POSIX-like file system layout inside the install directory (normally C:\cygwin), and thats what's mounted as / inside Cygwin.

The "real" Windows file system is mounted as /cygdrive, so /cygdrive/c/cygwin is a recursive directory that has the same contents as / (if that helps visualize the relationship).

Your Windows user home directory (not to be confused with the Cygwin directory inside C:\cygwin\home\USERNAME) is located in /cygdrive/c/Users/USERNAME. If you wanted you could add the following to the bottom of your ~/.bashrc file:

cd "/cygdrive/c/Users/$USER"

To start your shell in your Windows user directory. Personally, I find in practice I rarely actually need to end up in Windows-land while working in Cygwin, but to each their own.

Upvotes: 2

jbetancourt11
jbetancourt11

Reputation: 11

The cleanest solution would be to change the C:\cygwin\etc\nsswitch.conf file in the following way:

The %H variable represents the Windows home directory in POSIX style, so add it to the db_home: line and uncomment the line.

Example:

# /etc/nsswitch.conf
#
#    This file is read once by the first process in a Cygwin process tree.
#    To pick up changes, restart all Cygwin processes.  For a description
#    see https://cygwin.com/cygwin-ug-net/ntsec.html#ntsec-mapping-nsswitch    
# Defaults:
# passwd:   files db
# group:    files db
# db_enum:  cache builtin
db_home: /%H/home
# db_shell: /bin/bash
# db_gecos: <empty>

See this answer for more information on how to change Cygwin's defaults by editing the /etc/nsswitch.conf file.

More info: the root directory / is c:\cygwin (or c:\cygwin64 on a 64-bit installation) by default, so everything exists under there. Changing the setting above will make a symlink in c:\cygwin\home corresponding to your profile directory in Windows, usually c:\Users\<username> referenced by the environmental variable %userprofile%.

Edit: The last sentence isn't correct. What actually happens is that your home directory will change to /cygdrive/c/Users/<username>

One last useful tip: if you want to verify which directory you're in, relative to Windows' directory structure, you can run the following at the cygwin prompt: explorer.exe .

(Note the .) This opens up an explorer window in your current directory.

I think the simplest answer to your question is that you can navigate to any directory on any drive (i.e. C:, D:, etc) by replacing the drive letter with /cygdrive/<letter> (i.e. C:\ can be accessed at /cygdrive/c)

Upvotes: 1

Related Questions