How to change HOME directory and start directory on MSYS2? - windows

I have installed MinGW-w64 and MSYS2. But how do I change the HOME directory in MSYS2? So that when I type cd $home or cd ~ it goes to another directory that I defined.
And how do I write a code so that the starting directory is always where the .bat file is placed on?
In cmd I used this code:
%~d1
cd "%~p1"
call cmd
so when I open cmd on my desktop, it starts from the directory on desktop.
How can I do a similar thing with msys2?

If you would like to use your windows home folder as the home folder for MSYS2, you can edit /etc/nsswitch.conf and write:
db_home: windows

Msys2 will use windows %HOME% as it's $HOME dir. If you set %HOME% in environment variables (to the windows directory you need Msys2 to use) it will work.

I prefer to just update /etc/fstab
# fstab.
# cat /etc/fstab
# For a description of the file format, see the Users Guide
# https://cygwin.com/cygwin-ug-net/using.html#mount-table
# DO NOT REMOVE NEXT LINE. It remove cygdrive prefix from path
none / cygdrive binary,posix=0,noacl,user 0 0
##################################################################
# Canonicalize the two home directories by mounting the windows #
# user home with the same path mapping as unix. #
##################################################################
none /c/Users/Edward /home/Edward binary,posix=0,noacl,user 0 0

In one of your shell startup scripts (e.g. ~/.bash_profile where ~ is the default/original home directory) you can change the $HOME environment variable:
export HOME=/something/else
If you want your shell to open in that directory you might need to run cd (with no arguments) after setting $HOME; I have not tested it.

Besides the above anwsers, there is another way using the Windows command mklink to make a directory symbol link, similar to ln of Linux.
First make a directory of e:\msys\home, then run cmd as Administrator, then run:
cd c:\msys64
mklink /j home e:\msys\home
no other change required.
Occasionally after specific base updates, the link got invalid, need to be made once again, after the newly generated 'home' directory cleared or renamed.

Create or modify an MSYS2 /etc/passwd file. Two ways of doing this are shown below.
The following command can be run from an MSYS2 shell, and works safely whether or not the file exists and whether or not it already contains the current user:
$ grep "^${USERNAME}:" /etc/passwd >/dev/null 2>&1 || mkpasswd | grep "^${USERNAME}:" >>/etc/passwd
Next, edit /etc/passwd, and change the relevant user's home directory field (the 6th colon-delimited field).
$ vim /etc/passwd
BONUS: It is also possible to change the MSYS2 username by editing the first field.
As desired, move current home directory content to the new home directory.
Log off, then log back in.
The /etc/passwd edits can be done without manual editing, but this makes for a more complex command-line to paste into the MSYS2 shell, and, it might not work if the /etc/passwd file already exists and has the username in it already:
__DIR="/path/to/home"
mkpasswd | grep "^${USERNAME}:" | \
awk -v DIR="${__DIR}" -v RS=":" -v ORS="/n" \
'NR == 6 { printf(DIR ":"); next } { printf("%s", $0) } NR < 7 { printf(":") }' - >>/etc/passwd

I add HOME='E:/Users/your_directory' in /etc/profile. It looks as follows:
...
# Setup some default paths. Note that this order will allow user installed
# software to override 'system' software.
# Modifying these default path settings can be done in different ways.
# To learn more about startup files, refer to your shell's man page.
MSYS2_PATH="/usr/local/bin:/usr/bin:/bin"
MANPATH='/usr/local/man:/usr/share/man:/usr/man:/share/man'
INFOPATH='/usr/local/info:/usr/share/info:/usr/info:/share/info'
HOME='e:/Users/HP'
case "${MSYS2_PATH_TYPE:-minimal}" in
...
but the shell shows the path fully, not just ~.

I've created a batch file which sets the HOME variable:
set HOME=C:\Users\%USERNAME%
C:\LocalApp\PortableGit\git-bash.exe
This allows me to put my .bash_profile in this HOME (rather than on the default network location which performs very slowly).

Related

How to change the default directory for Linux at startup

I'm using Linux doing some course labs,it always startup with the default path, rather than my working directory, very inconvenient.
this is the default:enter image description here
and I want it to be like this:enter image description here
what should I do?
=======update=============
Solution is in this Start all sessions in a directory
Just run the following command in your terminal:
echo "cd ~/Desktop/Java\ Files" >> ~/.bashrc
The above command will add a new line in your ~/.bashrc file that contain cd ~/Desktop/Java\ Files and that will change your default working directory to /home/Varun/Desktop/Java Files when you will open the terminal.
if you are still in doubt, you can read here
https://askubuntu.com/questions/332062/setting-default-path-when-opening-a-terminal-session
I think your question has to do with the concept of HOME directory.
To change a user's home dir use this command:
usermod -m -d /newhome/username username
Explanation:
usermod command to change user attributes
-d (abbreviation for --home) will change the user's home directory
-m (abbreviation for --move-home) will move the content from the user's current directory to the new directory
You can omit the -m option if you don't need to move files from the old home to the new.

Change directory in bash script in windows

How can I change my working directory in bash script in windows. I have
~dp0 = C:\test\docker\windows and I want to change my directory to C:\test\build
So it means 2 levels up and then int o build folder
Thanks
Since C:\ is mounted by default into /mnt/c this will work.
Create a .bashrc in your home path by following command:
echo "BUILDDIR=/mnt/c/test/build" >> ~/.bashrc;source ~/.bashrc
cd $BUILDDIR
# Do your work below for example ./configure.
./configure
On my system in Git bash the C:\ root is just /c/ and other dirs from there are whatever they are, so it would be cd /c/test/build/.
You could also still say cd ../../build/.
Good luck.
To change the directory using a bash script is just like you would using normal bash.
cd "C:/test/build"
echo "You're now in the folder, do what you will."
Save the file as .sh and it can be used as such.
Note, when navigation your folders using a bash script remember the directory you'll be starting from is always the home directory.

Configuring Cygwin for ConEmu with different start directory

I am using ConEmu 64bit with Cygwin 64bit on a Windows 7 machine.
I don't want to set the working directory for cygwin in my .bashrc, as I like to setup more than one Task for Cygwin with different path parameters.
I set up the "home directory" in the task parameters:
\dir "D:\Downloads\Programming\Selenium\"
and added this line in the commands window:
C:\cygwin64\Cygwin.bat --login -i
Nevertheless Cygwin does not start in the "cygwinized" version of the given directory (/cygdrive/d/Downloads/Programming/Selenium) but in "normal" home directory of my user (~).
I only found howto's on how to add a new path in bashrc. Maybe one of you can enlighten me.
First of all, have you look at contents of your C:\cygwin64\Cygwin.bat? Why not?
#echo off
C:
chdir C:\cygwin64\bin
bash --login -i
Obviously, Cygwin.bat will never open bash in your desired dir.
Also, Cygwin ignores user defined startup directory! That is because cygwin always do cd "${HOME}" from /etc/profile script.
But, for example, bash from msysgit works properly.
Solution
However, /etc/profile script checks for CHERE_INVOKING environment variable before CD doing. So, the proper command line for starting cygwin will be:
set CHERE_INVOKING=1 & c:\cygwin64\bin\sh.exe --login -i
Another workarounds you may find in the project wiki page.
I had a similar demand (opening a new Cygwin-Tab in the folder currently viewed in explorer via the context menu) and found the following solution:
As stated by Maximus the Cygwin-Bash is usually opened by C:\cygwin64\Cygwin.bat which includes the login process. My approach was to change the working directory through the login.
.bash_profile:
if [ -f "${HOME}/startup.sh" ] ; then
source "${HOME}/startup.sh"
fi
startup.sh
cdc "D:\Downloads\Programming\Selenium\"
#cdc is a custom function, see .bashrc
.bashrc
cdc()
{
# converts a double-quoted windows-path and changes directory to it
p=$(cygpath -u "$1")
cd "$p"
}
The login progress thus changes the path of you bash to the one you've set in startup.sh. You might of course just type a Cygwin-compatible path in startup.sh without using the custom-function of .bashrc. It's still quite useful, especially if you want to want to use a dynamic startup.sh.
More Dynamic-approach:
Create a Cygwin.bat in C:\opt\ConEmu\ConEmu with the following content:
Cygwin.bat
#echo off
C:
echo cdc %1 > C:\opt\cygwin64\home\%USERNAME%\startup.sh
:: Your ConEmu-Task-definition here
C:\opt\ConEmu\ConEmu64.exe /Single /cmd {Cygwin}
This one writes a cdc-command followed by the path you provide into your startup.sh and starts an new instance of the ConEmu-Task Cygwin. The task itself does not do anything but starting an instance of Cygwin in my configuration:
ConEmu Task {Cygwin}
"C:\opt\cygwin64\Cygwin.bat"
I invocate the Cygwin.bat through the context-menu of the explorer. Another way would be to make a Windows-Shortcut pointing to Cygwin.bat with you path appended.

Why won't cygwin read the .ssh/config file in my home directory?

I am using cygwin and I am trying to set up an .ssh/config file. Cygwin's folder is in my C: drive. My Home directory is C:\Users\USER. I have noticed that when I edit C:\Users\USER.ssh\config, cygwin does not even notice it (even though it notices the other files I have in my USER folder, i.e. .bashrc). But it does notice the config file if I put it in C:\cygwin64\home\USER.ssh\config.
Is there a way to get cygwin to read the .ssh folder in my USER home directory instead?
Make sure your home directory setup in /etc/passwd matches /home/ Often it is not set at all (second to last field in the ':' delimited line. Then you'll need to restart your cygwin session (exit the terminal, and restart)
That should fix your problem.
This isn't the greatest solution but it works:
ln -s /cygdrive/c/Users/username/.ssh /home/username/.ssh
If /home/username/.ssh already exists, perhaps rename it to .ssh_original or move the files into the other .ssh folder.

How to default to other directory instead of home directory

I am developing on a windows machine. The only place I need for linux command line is Git Bash. The problem is: When I open it, I am in the home directory. I have to change the directory to my workspace, like:
cd ../../../d/work_space_for_my_company/project/code_source
Can I wrap this in a .sh file so I don't have to hand-type it anymore? This should be simple but I have zero knowledge about Linux command line. I am really appreciated If you can walk me
through how to create that .sh file.
Here's a more Windows-ish solution:
Right click on the Windows shortcut that you use to launch git bash, and click Properties. Change the value of "Start In" to your desired workspace path.
Edit: Also check that the Target value does not include the --cd-to-home option as noted in the comments below.
Just write that line to a file "cd.sh", then do this from your shell prompt:
. ./cd.sh
Or you can create an alias or function in your $HOME/.bashrc file:
foo() { cd /d/work_space_for_my_company/project/code_source ; }
If the directory name includes spaces or other shell metacharacters, you'll need quotation marks; it won't hurt to add them even if they're not necessary:
foo() { cd "/d/Work Space/project/code_source" ; }
(Note that I've omitted the ../../..; you don't need it.)
EDIT: If you add a line
foo
to your .bashrc after the function definition, your shell will start in that directory. Or you can just use the cd command directly in your .bashrc if you aren't going to need to use the function later.
(The name foo is just an example; you should pick a more meaningful name.)
Add the line to the .bashrc file in the home directory (create the file if it doesn't exist):
cd ~
touch .bashrc
echo "cd ~/Desktop/repos/" >> .bashrc
I use ConEmu (strongly recommended on Windows) where I have a task for starting Git Bash like
Note the button "Startup dir..." in the bottom. It adds a -new_console:d:<path> to the startup command of the Git Bash. Make it point to wherever you like
This may help you.
Right click on git bash -> properties
In Shorcut tab -> Start in field -> enter your user defined path
Make sure the Target field does not include --go-to-home or it will continue to start in the directory specified in your HOME variable
Thats it.
I also just changed the "Start in" setting of the shortcut icon to: %HOMEDRIVE%/xampp/htdocs/
(Please read warning below)
Really simple way to do this in Windows (works with git bash, possibly others) is to create an environmental variable called HOME that points to your desired home directory.
Right click on my computer, and choose properties
Choose advanced system settings (location varies by Windows version)
Within system properties, choose the advanced tab
On the advanced tab, choose Environmental Variables (bottom button)
Under "system variable" check to see if you already have a variable called HOME. If so, edit that variable by highlighting the variable name and clicking edit. Make the new variable name the desired path.
If HOME does not already exist, click "new" under system variables and create a new variable called HOME whose value is desired path.
NOTE: This may change the way other things work. For example, for me it changes where my .ssh config files live. In my case, I wanted my home to be U:\, because that's my main place that I put project work and application settings (i.e. it really is my "home" directory).
EDIT June 23, 2017: This answer continues to get occasional upvotes, and I want to warn people that although this may "work", I agree with #AnthonyRaymond that it's not recommended. This is more of a temporary fix or a fix if you don't care if other things break. Changing your home won't cause active damage (like deleting your hard drive) but it's likely to cause insidious annoyances later. When you do start to have annoying problems down the road, you probably won't remember this change... so you're likely to be scratching your head later on!
This will do it assuming you want this to happen each time you open the command line:
echo cd ../../../d/work_space_for_my_company/project/code_source >> ~/.bashrc
Now when you open the shell it will move up three directories from home and change to code_source.
This code simply appends the line "cd ../../../d/work_space_for_my_company/project/code_source" to a file named ".bashrc". The ">>" creates a file if it does not exist and then appends. The .bashrc file is useful for running commands at start-up/log-in time (i.e. loading modules etc.)
Right-click the Git Bash application link go to Properties and modify the Start in location to be the location you want it to start from.
From a Pinned Start Menu Item in Windows 10
Open the file location of the pinned shortcut
Open the shortcut properties
Remove --cd-to-home arg
Update Start in path
Re-pin to start menu via recently added
Thanks to all the other answers for how to do this! Wanted to provide Win 10 instructions...
For windows: Follow these steps-
Go to windows home> Right click on "Git Bash" application.
Properties> Shortcut
Change these two settings:
(a) Delete --cd-to-home from target
(b) Type folder path you want to start with git in "Start in".
This worked for me:)
If you want to have projects choice list when u open GIT bash:
edit ppath in code header to your git projects path, put this code into .bashrc file and copy it into your $HOME dir (in Win Vista / 7 it is usually c:\Users\$YOU)
#!/bin/bash
ppath="/d/-projects/-github"
cd $ppath
unset PROJECTS
PROJECTS+=(".")
i=0
echo
echo -e "projects:\n-------------"
for f in *
do
if [ -d "$f" ]
then
PROJECTS+=("$f")
echo -e $((++i)) "- \e[1m$f\e[0m"
fi
done
if [ ${#PROJECTS[#]} -gt 1 ]
then
echo -ne "\nchoose project: "
read proj
case "$proj" in
[0-`expr ${#PROJECTS[#]} - 1`]) cd "${PROJECTS[proj]}" ;;
*) echo " wrong choice" ;;
esac
else
echo "there is no projects"
fi
unset PROJECTS
you may want set this file as executable inside GIT bash chmod +x .bashrc (but its probably redundant, since this file is stored on ntfs filesystem)
My Git Bash shortcut on Windows complained when I put the cd to my work directory into ~/.bashrc
WARNING: Found ~/.bashrc but no ~/.bash_profile, ~/.bash_login or ~/.profile.
This looks like an incorrect setup.
A ~/.bash_profile that loads ~/.bashrc will be created for you.
So git created this .bash_profile:
$ cat ~/.bash_profile
# generated by Git for Windows
test -f ~/.profile && . ~/.profile
test -f ~/.bashrc && . ~/.bashrc
Which does the job.
Alternatively, you can just remove the .bashrc again and put the cd command into .bash_profile:
$ rm ~/.bashrc
$ echo "cd Source/Repos" >~/.bash_profile
$ cat ~/.bash_profile
cd Source/Repos
Once this is done you can close the Window and re-open it using your desktop shortcut and the prompt will tell you that your location is now where you wanted it - looks like this is my case:
Administrator#raptor1 MINGW64 ~/Source/Repos
$
If you type this command:
echo cd d:/some/path >> ~/.bashrc
Appends the line cd d:/some/path to .bashrc. The >> creates a file if it doesn’t exist and then appends.
it must be cd d:/work_space_for_....
without the : it doesn't work for me
Another solution for Windows users will be to copy the Git Bash.lnk file to the directory you need to start from and launch it from there.

Resources