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 8 years ago.
I'm new to Shell Scripting:
I'm trying to create a script that asks for user input as to the folder name, finds the directory the script is being run from, and creates a folder within that same directory with a bunch of files (I can do that part)
My issue is I can't seem to figure out how to make the script find its current directory without explicitly spelling it out.
I want to be able to run it from anywhere and have it create the folder right next to it without the user having to path it from the home folder every time.
Could someone help with this?
You could try this one liner:
DIR="$( cd "$( dirname "$0" )" && pwd )
Will leave you with a $DIR variable that contains the full path to the current directory. See this answer for more information!
script_dir=$(dirname "$0")
$0 is the name of the running script, as entered on the command line (for example: ./bin/script)
If you want the full path:
script_dir=$(cd -P -- "$(dirname "$0")" && pwd -P)
Related
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 1 year ago.
I created a .command file that runs a certain command java -jar myApp.jar in the Terminal, however it doesn't work because it first needs to cd into a certain directory.
Is there a way to make the .command file automatically go to the address where the .command file currently is, and execute the command from there?
I can't just add the cd line into the script because I need to distribute this and it needs to work no matter where on the computer it is.
In a bash script:
cd "$(dirname "$0")"
will cd into the directory containing the script.
I'm working a script that reads back the input of a full path to a directory (e.g. ~/test) but when I run the shell script it come back with this:
test-script.sh: line 34: cd: ~/test: No such file or directory
I'm running the test-script.sh file in a different directory, so I was assuming that a simple:
cd ~/test in the script would do it, but I guess not.
I know that this is probably a redundant question, but most of the other issues/examples have been with loops and other cases outside of just doing a simple cd ~/test in a script.
And for reference this is all I'm doing in my script:
echo What directory do you want to change to?
read directory_name
cd $directory_name
Again, new to bash/shell scripting so if there are any other ideas or suggestions, I'm all for it. Thanks!
solution 1)
if you want to use ~ , you need to use eval .
#!/bin/bash
pwd
echo What directory do you want to change to?
read directory_name
eval "cd $directory_name"
pwd
But this solution is weak , because someone can enter some maliciuois command instead of a directory name and this script will execute .
solution 2)
check if the first character of directory_name is a ~ and replace it by the appropriate value , it can the current user home dir or another user home dir .
you can do cd ~ or cd ~usera to change to usera homedir
Below is my .sh file
sh summaryByClient.sh $1 - takes around 10 mins to fetch the required data
mv summary.html ~/public_html/chats/ - **this is not happening**
exit 0
I do not understand why mv summary.html ~/public_html/chats/ this is not working inside .sh file, However I am able to mv separately using the same above command.
Could you be running into the issue from this answer with expanding the user's home directory? What happens if you write your script like this:
#!/bin/bash
# Other tasks to retrieve summary.html done here
mv ./summary.html $HOME/public_html/chats/
exit 0
Also, it's always a good idea to check whether the destination directory exists before a mv command. Examples are shown in this answer
I've been working on a script but always with a fixed directory (/opt/mw/script).
I need to change that to be able to execute the script from any directory.
I think I will need to add a "." at the beginning of the line to be able to execute the script?
For example ./mw/script
Is this correct?
Thanks
You already can execute that script from any directory with that absolute file path. It's the relative file paths (that start with ./ or ../) that can only be executed from a specific directory.
You need to add the "." if the script is in the current directory (e.g. ./script), but it is optional if the script is already inside another directory (e.g. mw/script).
Also notice that if your script contains relative references to other files and directories, you may need to use this trick to properly refer to them from any directory.
For instance, consider the following script using absolute paths:
#!/bin/bash
# lists all files in this directory
ls /opt/mw
If it is converted to relative paths as follows:
#!/bin/bash
# lists all files in this directory
ls mw
Then this script will only work if you run it from its own directory (e.g. cd /opt/ && ./script).
But then you can generalize it like this:
#!/bin/bash
SCRIPT_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
# lists all files in this directory
ls $SCRIPT_DIR/mw
Now the script works even when executed from another directory.
I have a file called execute.command. When I execute it by opening it. It runs from my users folder. I would like it to run in the folder where it is located. is that possible?
You must change the script to change working directory to script-location directory:
#!/bin/bash
cd "$(dirname "$0")"
More answers here : Bash script: set current working directory to the directory of the script
I added cd dirname $0 to the top of the command line. This will cd you into the correct folder. Here are more details.
http://hints.macworld.com/article.php?story=20041217111834902