Bash cannot find any files at all [duplicate] - bash

This question already has answers here:
Difference between ./ and ~/
(6 answers)
Closed 1 year ago.
I am quite new to using Bash, so apologies if this is a rather rudimentary question;
I am trying to open a text file in Bash, but Bash does not seem to be able to find any of my files or directories at all. Whenever I try to use the cat command or cd command (for example, cd Desktop/), no matter what file or directory I specify, Bash tells me "No such file or directory". pwd says that I am in /mnt/c/WINDOWS/system32 . ls shows me a very long list of files, but none of them are that for which I am looking. I am trying to open a .txt file on my Desktop, but neither the Desktop nor the txt file are showing up in that list. I'm running Bash via the Linux subsystem on Windows 10.

He are tips to start with WSL:
Your windows files will be found at /mnt/c/
cd /mnt/c
from here use ls to see the folders and find the files you want to use, I personally tend to put the files I use with windows subsystem for linux on the C: directory for easier use, like c:\FilesForWSL you can just create the folder on windows or cd /mnt/c and mkdir FilesForWsl then cd FilesForWsl.
After this setup anytime you want to put a file to be used by windows or from windows into linux just go to cd /mnt/c/FilesForWsl substitute FilesForWsl with your preferred folder name. Hope this clarify a little how WSL works.

Related

How to find the location of the script that is being executed? [duplicate]

This question already has answers here:
How do I get the directory where a Bash script is located from within the script itself?
(74 answers)
Closed 4 years ago.
How can I find (if possible) the location of the script that is being executed?
So I have a script that I'll use to do some magic, and I'll need to call it from random locations on a system (this system can be mine, my servers, my friend's PC, my mom's PC, etc).
Now, I need to execute some binaries present along with the script, How can I do that?
Say my script folder has 3 files:
binary1
myScript.sh
binary2
Now, this whole folder can be located in "/tmp/scripts", on one system, and in "/home/user/Downloads" on other system(random locations).
And I'll need to run this script from, lets say "/home/user/Desktop".
So my question is, how can I execute the binary1 and binary2 from my script without knowing their actual path before hand?
The only executables that you can execute are either present in your PATH variable or initialization files (like aliases and functions).
So, couple of options. Try to determine the location where your scripts are and at the beginning of your script, add the path like.
# Your script beginning
PATH="$PATH:/<location>"
Other option, create a folder like say $HOME/bin and copy your script in this location on all your machines or better yet you can add the scripts /usr/bin or /usr/local* folder in all machines
If foo is an alias of the script, try:
n="$(type -f foo)" ; n="${n/*\`}"; n="${n%?}"
dirname "$(realpath "$(which "$n")")"

How to enable Git terminal begin with home directory in Windows? [duplicate]

This question already has answers here:
How to make Git-bash command line start up with home directory?
(2 answers)
Closed 5 years ago.
Git-bash is starting up from the directory where the Git-bash application is installed i.e at "C:/Program Files/Git" and it is displaying these prompts right after I launch Git-bash.exe:
bash: /c/Users/Kedar/git-prompt.sh: No such file or directory
This is so insane because I definitely have placed git-prompt.sh file in that path
It is not starting from the home directory as it was expected i.e. Kedar ~ $
Yes, I can reach this directory but after I type this command - Kedar / $ cd ~/
So, my query is to how to enable Git terminal to start up with home directory i.e. Kedar ~ $ and not Kedar / $ ?
If you are on Windows, do this
Right click on the Windows shortcut that you use to launch git bash terminal i.e git-bash.exe, and click Properties.
Go to tab named Shortcut
Change the value of Start in: label to your desired workspace path i.e. C:\Users\Kedar maybe in your case
Then you should run the application as administrator!
Also assuming you use Windows, there are two ways:
Simple way: As Divyanshu Kushwaha explained, open the Properties of the shrotcut you use to launch and just place --cd-to-home behind the target path that points to your git-bash.exe file, like this: "C:\Program Files\Git\git-bash.exe" --cd-to-home.
Flexible way: You can configure the bash command line via the .bashrc file in your home folder. If the file does not exist, you can create it and then add something like this:
MoveToHome() { cd /c/<any path you want>; }
MoveToHome
This lets you define any path you want to be in on startup, by defining a MoveToHome function and calling it right on startup.

move text files with specific name using bash on windows machine

I am trying to move all text files with a specific name in them from one directory to another using bash on a windows machine in cygwin. The code is below and but I am not sure how to reference a windows path in bash. Thank you :).
for i in "C:\Users\cmccabe\Desktop\annovar"; do
mv $i"\"*multianno.txt "C:\Users\cmccabe\Desktop\all""\"basename $i`multianno.txt
done
mv: cannot stat ‘C:\\Users\\cmccabe\\Desktop\\annovar\\*multianno.txt’: No such file or directory
You should use cygpath, which is a cygwin utility for converting windows to/from cygwin paths.
Without additional arguments it will transform a given windows path to its cygwin equivalent, which is what you want :
mv $(cygpath "windows_src") $(cygpath "windows_dst")
I wasn't sure it would work with paths containing jokers, but it looks like it does :
$ cygpath "C:\path\*a*"
/cygdrive/c/path/*a*

Running scripts without dot slash (Ubuntu vs Mac) [duplicate]

This question already has answers here:
Why do you need ./ (dot-slash) before executable or script name to run it in bash?
(9 answers)
Closed 2 years ago.
In ubuntu scripts can be executed with following commands:
$ chmod +x manage.py
$ manage.py
However in mac you need to use ./ in order to actually run the script, as follow:
$ chmod +x manage.py
$ ./manage.py
I would like to know what is exactly ./ (especially that both system use bash by default) and if there is a way to run scripts directly in mac?
It's because you (very sensibly) don't have . in your PATH environment variable. If you do, it becomes an attack vector for people to get you to execute their own code instead of real stuff.
For example, let's say your path is:
.:/usr/bin
so that commands will first be searched for in your current directory, then in /usr/bin.
Then another user creates an executable script file ls in their home directory which changes to your home directory and deletes all your files. Then they tell you they've got something interesting in their home directory. You run ls to see what they have, and your files are deleted. All because it ran ls from your current directory first.
This is a particular favorite attack vector against naive system admins.
To be honest, on my home machines, I don't worry too much, since I'm the only user and I'm not prone to downloading stuff I don't trust. So I usually add . to my path for convenience, but usually at the end so it doesn't get in the way of my more regular commands.
When you are executing a command that file (script/binary) needs to be found by the system. That is done by putting directories where to look for scripts into the PATH environment variable. So if it works in ubuntu it means PATH includes '.' (the current directory). If you want the same behavior on mac then put something like export PATH="$PATH:." in your .bashrc (asuming you are using bash..)

How to send a file to Desktop with bash script?

I am trying to send a file or folder to Desktop with Linux Command Prompt but I don't know how.
Please tell me what command can I use for this?
The move command mv. Use man mv for more information, as this command is a lot more complex than it seems. With cd Desktop/ you should be able to find your desktop on variations of linux like Mint or Ubuntu. To find your present working directory, as in your current path for the terminal, type pwd. This will give you your directory which will be similar to /home/Desktop.

Resources