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.
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.
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.
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.
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.