How to default to other directory instead of home directory - bash

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.

Related

How to remove entry from $PATH on mac

I was trying to install Sencha Touch SDK tools 2.0.0 but could not run it properly. It created an entry in the $PATH variable.
Later I deleted the sencha sdk tools folder but didn't realize that the path variable is still there.
When i did echo $PATH I got -
/Applications/SenchaSDKTools-2.0.0-beta3:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin
I searched on how to remove variables from $PATH and followed these steps :
Gave the command PATH="/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin"
Did echo $PATH which showed /usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin
gave the command export PATH
Closed terminal and reopened it. Gave the command echo $PATH. This time I got
/Applications/SenchaSDKTools-2.0.0-beta3:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin
Can anyone tell me what am i doing wrong?
echo $PATH and copy it's value
export PATH=""
export PATH="/path/you/want/to/keep"
Check the following files:
/etc/bashrc
/etc/profile
~/.bashrc
~/.bash_profile
~/.profile
~/.MacOSX/environment.plist
Some of these files may not exist, but they're the most likely ones to contain $PATH definitions.
On MAC OS X Leopard and higher
cd /etc/paths.d
There may be a text file in the above directory that contains the path you are trying to remove.
vim textfile //check and see what is in it when you are done looking type :q
//:q just quits, no saves
If its the one you want to remove do this
rm textfile //remove it, delete it
Here is a link to a site that has more info on it, even though it illustrates 'adding' the path. However, you may gain some insight.
What you're doing is valid for the current session (limited to the terminal that you're working in). You need to persist those changes. Consider adding commands in steps 1-3 above to your ${HOME}/.bashrc.
If you're removing the path for Python 3 specifically, I found it in ~/.zprofile and ~/.zshrc.
$PATH contains data that is referenced from actual files. Ergo, you should find the file containing the reference you want to delete, and then delete said reference.
Here is a good list to run through progressively [copied from #Ansgar's answer with minor updates].
/etc/bashrc
/etc/profile
~/.bashrc
~/.bash_profile
~/.profile
~/.MacOSX/environment.plist
/etc/paths
/etc/paths.d/
Note that /etc/paths.d/ is a directory that contains files with path references. For example, inside this directory may be a file called, say, fancy-app, and inside this file you'll see an entry like below:
/path/to/fancy-app
This path will appear in your $PATH and you can delete the entry in the file to remove it, or you can delete the file if it has only the one reference you want to remove.
Use sudo pico /etc/paths inside the terminal window and change the entries to the one you want to remove, then open a new terminal session.
when you login, or start a bash shell, environment variables are loaded/configured according to .bashrc, or .bash_profile. Whatever export you are doing, it's valid only for current session. so export PATH=/Applications/SenchaSDKTools-2.0.0-beta3:$PATH this command is getting executed each time you are opening a shell, you can override it, but again that's for the current session only. edit the .bashrc file to suite your need. If it's saying permission denied, perhaps the file is write-protected, a link to some other file (many organisations keep a master .bashrc file and gives each user a link of it to their home dir, you can copy the file instead of link and the start adding content to it)
Close the terminal(End the current session). Open it again.
If the manual export $PATH method does not seem to be working after you close the terminal and open again, definitely check the shell configuration files.
I found a small script that kept adding some more path in front of the $PATH everytime it was open.
For zsh you can check the ~/.zshrc file.

locate my bash configuration file

Is there a way to find the name of the bash configuration file that is currently on use. I searched for the .profile or .bashrc but did not find them. However still I can print some of the env variables i.e
$HOME, $PATH
If you are using one of these sudo su -, bash --login, ssh user#host,
these are considered as login shell,
then you might want to try
/etc/profile
~/.bash_profile
~/.bash_login
~/.profile
Environment variables are stored in the each process itself.$HOME and $PATH is stored in the bash process itself.
So,First you have to find process id of your bash process.You can use ps command to get the process id of the process.
Then,
cat /proc/your_bash_process_id/environ
I hope this will helps you.
As some people said, those files are in the home directory.
(Screenshot from Wiki article.)
I'll tell you here how to find it (the explanation here is for Windows 10 only), which is my system.
You can quickly find and open some of those locations with the following commands (using bash console):
cd ~ ← It will take you to your home directory.
ls -a ← It will display files and folders, both visible and hidden.
explorer . ← It will open the home folder in Windows Explorer, which can be really useful. Look for the file/folder you need. Move to it using:
cd folder-name or open the file in the text editor using notepad file-name. For your case, you should find and open notepad .bashrc
Those files are in the user's home directory.
$HOME/.profile
$HOME/.bashrc

Windows - start git in given directory

I'm using git portable on windows. It's rather user friendly, but there is one thing that bothers me. Every time I run it I have to type the entire path to the project directory, which is quite long sometimes. Maybe it's not a serious problem, but it would be very nice to shorten it. I tried the following:
bash script.sh // cd in this file // nothing happens
create symbolic link it - it just copies the directory
create windows shortcut - can't open it within git console
Anybody managed to solve this?
You can right click on the Windows shortcut that launches Git Bash, edit its properties and modify the "Start in" path to your project path. Every time you launch this shortcut, it will cd into that project path.
Or you can add an alias to your ~/.bashrc like below:
alias proj="cd /path/to/project/"
This will allow you to cd into the project dir on demand by typing the alias name at the prompt.
If you run git-bash.bat from Portable git, it should work mostly as normal git installation. So, to work with a specific repository, just cd into it:
cd /c/code/MyRepo/
git whatever
I've added
cd /c/dev
to
~/.bashrc
with
echo cd /c/dev >> ~/.bashrc
But I'm running MSysGit the non-portable version. Hope this helps in some way anyway.

How can I cd to an alias directory in the Mac OSX terminal

Is there a way to get into an alias directory from shell with the command "cd" ? It always returns that "htdocs" isn't a directory.
Edit: I made the shortcut with the OS GUI -> rightclicked the htdocs directory and chose "Alias..." (i'm using a german OS if it's not alias maybe it's called shortcut in english?) then i moved it to my home directory (because my terminal starts from there when i open it).
All i want is to open my terminal and type "cd htdocs" so that i can work from there.
you can make symbolic link to it.
ln -s EXISTING_PATH LINK_NAME
e.g.
ln -s ~/Documents/books ~/Desktop/
Reference
Enter into a directory through an alias in Mac OS X terminal
All i want is to open my terminal and type cd htdocs so that i can work from there.
The easier approach is probably to ignore the links and add the parent directory of your htdocs directory to the CDPATH environment variable. bash(1) will check the contents of the CDPATH environment variable when you type cd foo to find the foo directory in one of the directories listed. This will work no matter what your current working directory is, and it'll be easier than setting symbolic links.
If the path to your htdocs is located /srv/www/htdocs/, then you could use CDPATH=/srv/www. Then, cd foo would first look for /srv/www/foo/ and change to it if it exists; if not, then it would look for foo in the current working directory and change to it if it exists. (This might get confusing if you have multiple htdocs directories on your system; in that case, CDPATH=.:/srv/www would let you change into a child directory easily but still use the /srv/www/htdocs/ version if no ./htdocs directory is present.)
You can add the CDPATH=/srv/www line to your ~/.bashrc file so it works every time you start a terminal.
I personally use this to quickly work in the directory which is present deep inside one of my Volumes in my Mac.
Open your ~/.bash_profile, create an alias to the directory by adding this:
alias cdh="cd /Volumes/Haiku/haiku/src/apps/superprefs"
Save it, restart your terminal. Now on typing cdh in your terminal should change the working directory to the one mentioned as the alias.
I am not sure how OSX exposes Alias links but since you are using bash you can just create a variable in your .bashrc file.
On its own line put:
htdocs=YourDirectoryPath/
Once you have restarted bash you can just type cd $htdocs
There is a old hint on macworld to do this in a way that is integrated with BASH: Enable 'cd' into directory aliases from the Terminal
Plus, here is an answer that uses this solution on superuser.
You may be able to use osascript to do this -- this command seems to work:
cd "`osascript -e "on run aFile" -e "set aFile to POSIX file aFile as alias" -e "tell application "\""Finder"\"" to return POSIX path of ( ( original item of aFile ) as text ) " -e "end run" path_to_my_Finder_alias 2>/dev/null`"
Basically this command is running an AppleScript that finds the destination path of the argument (path_to_my_Finder_alias) in a subshell, then wraps it in double quotes, and changes the directory to it.
Maybe someone with a little more bash expertise can turn it into a bash alias or function.
try:
alias cdgo=`echo cd /root/go/`
cdgo will run, then get command "cd /root/go/" and enter, and it will change your directory in current terminal process
It works on my centos, no test with osx

How to make a shell script global?

I am on Mac's OS 10.6, and I am trying to learn a thing or two about shell scripting. I understand how to save a shell script and make it executable, but I am wondering what I can do or where I can save the file to make it global (that is, accessible no matter what folder I am in).
For example, if I save a .sh file in the /Users/username/ directory and make it executable, I can only execute that script in that specific directory. If I navigate to /Users/username/Downloads, for example, I can't execute the script.
Also, any suggestions of resources for learning more about shell scripting would be helpful. Thanks
/usr/local/bin would be the most appropriate location. Mac OS X has it in the PATH by default
There are two ways to do it -
Put your script in usr/local/bin and make sure it is executable(chmod +x my_script)(This is already set in the path, you can check by doing an echo $PATH)
Create a folder in your home directory called bin. (For your personal scripts)
cd ~ (Takes you to your home directory)
mkdir bin (create a bin folder)
vim .bash_profile (to set path environment variable)
export PATH=~/bin:$PATH (Press i then add this line and then do esc and type :wq)
Now you can just type the name of your script and run it from anywhere you want.
** NOTE: If you want to run the script with a shortened command rather than typing your entire filename, add the following to your .bash_profile:
alias myscript='my_script.sh'
Then you can run the script by simply typing myscript. (you can sub in whatever alias you'd like)
Traditionally, such scripts either go in ~/bin (ie: the bin directory in your home directory) or /usr/local/bin/ The former means the script will only work for you, the latter is for scripts you want anybody on the system to be able to run.
If you put it in ~/bin, you may need to add that to your PATH environment variable. /usr/local/bin should already be on the path.
In mac operating system
Open bash ~/.bashrc file.
add path of your script in your bashrc file , using
export PATH="$PATH:/Users/sher.mohammad/Office/practice/practiceShell"
Open your ~./bash_profile file and add [[ -s ~/.bashrc ]] && source ~/.bashrc
open new terminal window
Now whenever you will open your terminal your script will be loaded
This one is super easy if you are familiar with your bashrc file! This will entirely use just your .bashrc file and takes 2 seconds to accomplish.
(I use Arch Linux Manjaro so I use .bashrc located in my home directory)
The code to be placed in your .bashrc file:
# Simple bashrc method to launch anything in terminal from any directory
YOURCOMMAND () {
cd /path/to/directory/containing/your/script/ && ./YOURSCRIPT
}
As you can see, first you use the simple 'cd' command and give it the directory of the scripts location, then use '&&' so that you can make the next command executed right after, and finally open your script just as you would normally! Super easy and saved right in your .bash file! :)
Hope I've helped someone!
Sincerely,
AnonymousX
On using bash shell, write that script as function and then put it to the .bashrc or source the file which containing that function by "source file_name"
Now execute the script by function call in the shell.
Either saving it in /usr/bin (or any other directory present in PATH) or editing PATH to include the directory you saved it in will basically make it run in any directory.
from the working directory of 'script.sh'" mv [script.sh] /usr/local/bin"( not tested but seems to be the least complex way IMO.)
You should put it in the global executable directory on your machine. I think that would usually be /usr/bin on Unix-based operating systems (this would however most often require super user privileges on that machine).
You could also put it in any other directory that is in the $PATH environment variable, although it would only work for those users who have that directory in that variable.
You can find the value of $PATH by typing echo $PATH in a shell. The directories are separated by :.

Resources