cd commands not working from .bash_profile on MobaXTerm - bash

I'm on Windows 10, using MobaXterm. I have a persistent home directory, and I've created .bash_profile there. My .bash_profile contains:
echo "Loading bash profile."
cd "/drives"
When I open a new MobaXterm tab, I get:
Loading bash profile
: No such file or directory
But when I run:
cd "/drives"
from the command line, it changes directory to /drives.
I don't really need to go to /drives when a tab starts up, what I want to do is create alias for cd commands that go to long directory names without having to type them out every time, but no cd commands will work from .bash_profile, I always get 'No such file or directory'. Any ideas?

I contacted MobaXTerm support, and their reply was to convert the .bash_profile to unix format with this command:
dos2unix /home/mobaxterm/.bash_profile
Now the cd commands work as expected.

Related

Git bash change into a directory and start a file

My default git bash starts on the C: drive and I often have to change into a project directory and start a file up.
Is there a way to cd into a directory and start a file all in one command using Git bash on Windows?
You should have a file .bashrc into your home directory. It should be under
C:\Users\login
Add the following line in the file: cd "projet_directory_full_path"
Open a new session and it should start on your project directory
Yes, you can directly start with cat command following with complete path using forward slashes as 'cat C:/Users/username/filename' and cd command also works in similar way.

Warning on git bash start "bash: C:/Users/RS3/Anaconda3/etc/profile.d/conda.sh: No such file or directory"

Whenever I open git bash I get the following warning:
bash: C:/Users/RS3/Anaconda3/etc/profile.d/conda.sh: No such file or directory
The problem is that I uninstalled Anaconda, because I installed independent python, which resulted in giving that error.
How to remove this Warning.
Check the content of ~/.bashrc (which was mentioned here)
Maybe that (or ~/.profile, mentioned here) includes a command for that old uni uninstalled program.
Remove those lines, and try to open again a git bash session.
To remove that warning, simply erase the file path string in the .bashrc file using nano command. Here's how:
Steps to remove that warning:
Open Git Bash and check .bashrc location by typing ~/.bashrc into git bash.
Navigate to folder where .bashrc file is located.
Type nano .bashrc
Erase the string that says C:/Users/RS3/Anaconda3/etc/profile.d/conda.sh
Press ctrl+O to save .bashrc file and then ctrl+X to exit.
When you reopen git bash nothing should appear because .bashrc doesn't have that path in it anymore. Also, if conda commands were working before it will still continue to work after erasing this.

Shell shortcuts to folders in Windows

I'm learning Vagrant and Virtualbox, Now to add a line to my hosts file in a (windows)
echo "test" >> c:/windows/system32/drivers/etc/hosts
But i'm wondering if i can make a Shortcut to c:/windows/system32/drivers/etc/hosts that i can use in any shell from everywhere.
You might create an environment variable:
[System.Environment]::SetEnvironmentVariable('Youralias','c:/windows/system32/drivers/etc/hosts','Machine)
and then access it using
$env:YourAlias
from PowerShell... If you want to access it from CMD
%Youralias%
should work.
Easier solution using a .bash_profile file
In your home folder (windows) you can make a .bash_profile file and specify aliases.
Go to your home folder
cd ~
make a .bash_profile file and open it in vim or any other editor
touch .bash_profile && vim .bash_profile
add a new alias by adding the following line
alias testfolder="cd /c/testfolder"
save and close your editor.
Explanation someone?
For some reason it only works after i run the following command
source .bash_profile

.bashrc: Permission denied

I try to work with a project in vagrant.
I have made the command vagrant ssh, and connected to VM. Now I need to edit .bashrc file to set path to the source code. But first I couldn't find that file. So I googled and find that the way is call command ~/.bashrc. But doing this I get message, that I have no access to it:
[vagrant#nupic-vagrant:~]$ ~/.bashrc
-bash: /home/vagrant/.bashrc: Permission denied
So what to do now?
UPD.
I can't find the .bashrc file.
When I try to make command ls -a I get following:
[vagrant#nupic-vagrant:~]$ ls -a
. .bash_logout cleanup.sh sshd.sh .veewee_params
.. .bash_profile minimize.sh vagrant.sh .veewee_version
.bash_history .bashrc .ssh .vbox_version .zsh_profile
[vagrant#nupic-vagrant:~]$ locate .bashrc
/etc/skel/.bashrc
/home/vagrant/.bashrc
/var/chef/backup/etc/skel/.bashrc.chef-20130614181911
/var/chef/backup/home/vagrant/.bashrc.chef-20130614181912
[vagrant#nupic-vagrant:~]$
But only the place where I can find some of those files is the directory where cygwin is installed. Pls, see illustrations, they reflect relations between directories vagrant and cygwin.
.bashrc is not meant to be executed but sourced. Try this instead:
. ~/.bashrc
or, equivalently
source ~/.bashrc
See the reference about the . (aka source) builtin.
Note that if what you're looking for is to restart your Bash session after modifying your ~/.bashrc file, you might as well use:
exec bash
That will replace your current Bash session (thanks to exec) by a new session.
If you want to edit that file (or any file in generally), you can't edit it simply writing its name in terminal. You must to use a command to a text editor to do this. For example:
nano ~/.bashrc
or
gedit ~/.bashrc
And in general, for any type of file:
xdg-open ~/.bashrc
Writing only ~/.bashrc in terminal, this will try to execute that file, but .bashrc file is not meant to be an executable file. If you want to execute the code inside of it, you can source it like follow:
source ~/.bashrc
or simple:
. ~/.bashrc
If you can't access the file and your os is any linux distro or mac os x then either of these commands should work:
sudo nano .bashrc
chmod 777 .bashrc
it is worthless
The .bashrc file is in your user home directory (~/.bashrc or ~vagrant/.bashrc both resolve to the same path), inside the VM's filesystem. This file is invisible on the host machine, so you can't use any Windows editors to edit it directly.
You have two simple choices:
Learn how to use a console-based text editor. My favourite is vi (or vim), which takes 15 minutes to learn the basics and is much quicker for simple edits than anything else.
vi .bashrc
Copy .bashrc out to /vagrant (which is a shared directory) and edit it using your Windows editors. Make sure not to save it back with any extensions.
cp .bashrc /vagrant
... edit using your host machine ...
cp /vagrant/.bashrc .
I'd recommend getting to know the command-line based editors. Once you're working inside the VM, it's best to stay there as otherwise you might just get confused.
You (the vagrant user) are the owner of your home .bashrc so you do have permissions to edit it.
Once edited, you can execute it by typing
source .bashrc
I prefer to logout and in again (there may be more than one file executed on login).
Please find the step to fix bash restricted error on Linux servers.
If you are getting below restricted message while try to login to the server by using your credentials , then it might be an issue with lack of directory permissions in the server.
Because of this permission issue we were unable to navigate to required directories and getting error “bash: cd: restricted”
Fix : To release bash restriction error use the highlighted command in Linux server -bash -f
As same if wants to restrict the permission use the highlighted command - bash -r
Once you executed the bash -f command the restrictions will be released from the directories and we can able to proceed with patch scripts.

Whats different when you executable a scripts in OSX?

I have a question I have been trying to fix for a while. I want to understand what's the difference between starting a script from the command line and making it executable and then running it from the Finder.
Because this is what I am experiencing;
I have a simple script called trash-files which contains this command:
trash ~/Downloads/*
When I run from the terminal it works as expected; however if I doubleclick the shell script in the finder I see this:
/Users/xx/Desktop/trash-files: line 1: trash: command not found
I hope anyone can tell me why this doesn't work as expected
trash is not a standard command in OS X. Is it something defined in your ~/.profile or a similar file? If so, these are not run for non-login shells, such as those created to run a script.
If you're using homebrew, you could run
brew install trash
which would install the necessary scripts to have the trash command work in the way you're expecting.
There is a folder in your home folder location called
.Trash
The "dot" in front of the folder name makes it hidden while searching for it in finder. You'll have to use Terminal to execute the following command:
cd ~/
ls -la
This will change the directory to the current logged in users home folder, then second command will list files and show hidden files. You can then run:
rm .Trash/*
This will remove everything inside the Trashcan on the dock.
So open TextEdit from the /Applications folder, go to "Format" and make it "Plain Text". Paste in the two lines below.
#!/bin/sh
rm ~/.Trash/*
Save the file as "emptyTrash.sh" (uncheck use txt extension). Save it to your Desktop or wherever you'd like. Then open Terminal, cd (change directory) to where the files is and run this command to make the script executable:
chmod +x emptyTrash.sh
Then you can execute the script by cd (changing directory) to path where the script is, and run:
./emptyTrash.sh
That's it.

Resources