This question already has answers here:
Why can't I change directories using "cd" in a script?
(33 answers)
Find file then cd to that directory in Linux
(12 answers)
Make a Bash alias that takes a parameter?
(24 answers)
Closed 2 years ago.
I want to find a file and goto his directory. I made a bash script :
#!/bin/bash
FILE=$1
FILEPATH=`find . -name "$FILE"`
if [ -f "$FILEPATH" ]
then
cd $(dirname "$FILEPATH")
fi
But this script does not work. I saw on this post that I have to add exec bash or $SHELL but it create a new bash prompt ans display my welcome message.
How can I do ? I just want a script, alias or something to find a file and go to the directory containing that file.
Source your script instead of running it like you do. When you run it like you do, you spawn a new shell that executes the cd, completes succesfully, closes the shell and returns to your current shell, leaving you in your pwd.
Use source myscript.sh or . myscript.sh instead of bash myscript.sh or myscript.sh.
Related
This question already has answers here:
How do you normalize a file path in Bash?
(24 answers)
Difference between sh and Bash
(11 answers)
Closed last year.
I'm not familiar with bash scripting. In my app, I have three folders. Frontend, backend, and script are the three folders. My bash script is contained within the script. This is the structure of my app Image. I'd like to retrieve absolute path variables (frontend and backend) from my script. I'm not sure what to do. I was successful in obtaining the script path folder. My objective is to obtain an absolute route from a script.
#!/bin/sh
root_path="$( cd "$(dirname "$0")" >/dev/null 2>&1 || exit ; pwd -P )"
#frontend_path=
#backend_path=
echo "Getting absolute path ${root_path}"
Adding /.. after the output of dirname should do the trick to get you the root path:
root_path="$( cd "$(dirname "$0")/.." >/dev/null 2>&1 || exit ; pwd -P )"
From that on, you can just add /frontend / /backend to get the other paths:
frontend_path="$root_path/frontend"
backend_path="$root_path/backend"
This question already has answers here:
How to go to each directory and execute a command?
(12 answers)
Closed 5 years ago.
I'm currently trying to write a script which will cd to a directory and execute a python script if it matches a predefined string to the directory name. I need the code to change to a directory with the string 'batch' in it and execute the script. The most useful tool would probably be the case statement, but I can't get it to work for some reason. I'm fairly new to bash scripting, so i'm not really sure why this isn't working.
Here's my code:
#!/bin/bash
dir_string="batch"
workingdir=$PWD
for dir in *;do
echo "$dir"
if [[-d "$dir"]]; then
case "$dir_string" in
$dir)
cd $workingdir/$dir
for i in *;do
execute python script
done
;;
*)
echo "Can't find appropriate directories."
;;
esac
fi
done
Any help is appreciated!
What you are trying to achieve with your script, can be achieve with find:
#!/bin/bash
dir_string="batch"
workingdir=$PWD
for dir in $(find "$workingdir" -type d|grep $dir_string)
do
(cd "$dir" && execute python script)
done
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 6 years ago.
I have a shell script here:
/node_modules/.bin/exec.sh
in the exec.sh script, I want to obtain the path of the directory's parent directory that the script is contained in (not pwd/cwd!). I can obtain the containing directory like so:
`dirname $0`
which will yield:
/node_modules/.bin
but I am looking to get at one directory higher, I just want to get
/node_modules
I am having trouble searching for the answer, my guess is:
`dirname $1`
but just a guess, not sure if that's right at all. Can anyone give an explanation of how to do this and how it works?
Run dirname twice (nested).
~$ dirname $PWD
/home
~$ dirname `dirname $PWD`
/
~$
I believe the answer is
$(dirname $(dirname "$0"))
don't forget about the double-quotes around "$0"
This question already has answers here:
One command to create and change directory
(9 answers)
Closed 7 years ago.
Let's say I have the following command:
mkdir directory && cd directory
I normally do this a lot during the day so I'm wondering if there is a simpler shorter way of doing this.
Does anybody know?
you can call last argument by &_
mkdir directory && cd $_
this is result
system:/tmp # mkdir directory && cd $_
system:/tmp/directory #
Put the following code in your ~/.bashrc or ~/.zshrc :
mkcd () {
mkdir "$1"
cd "$1"
}
Then in your shell, enter the following command mkcd foo. As you can see, this function need one argument which are the name of the directory.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
In the bash script how do I know the script file name?
I often need the file system of a bash script to reference other needed resources. Normally use the following code in line after the shebang line. It sets a variable called 'scriptPos' contains the current path of the scripts
scriptPos=${0%/*}
It works fine, but is there something more intuitive to replace the shell expansion?
There's dirname but it requires a fork:
scriptPos=$(dirname "$0")
One option is
scriptPos=$(dirname $0)
which comes at the cost of an extra process, but is more self-descriptive. If the script is in the current directly, the output differs (for the better, in my opinion):
#!/bin/bash
scriptPos=$(dirname $0)
echo '$0:' $0
echo 'dirname:' $scriptPos
echo 'expansion:' ${0%/*}
$ bash tmp.bash
$0: tmp.bash
dirname: .
expansion: tmp.bash
UPDATE: An attempt to address the shortcomings pointed out by jm666.
#!/bin/bash
scriptPos=$( v=$(readlink "$0") && echo $(dirname "$v") || echo $(dirname "$0") )