Suppose the command :pwd returns
~/Users/MyUser/FolderA/FolderB
I want to write a script where I don't know in advance that I am in FolderB, as I can be in FolderA or even some other external folder.
My script resides in FolderB and I want to find a folder with a specific name (FolderC) that resides in FolderA alongside FolderB
If i search for find ~ iname "FolderC" i find all sorts of results that I do not want.
I want only the 1 closest possible directory with that name to my script.
What is the line that will help me find it ?
-
I think the find your looking for is "find .. inane "FolderC"' using .. will start find in the directory above current. Whereas your ~ starts find at your home directory.Tyson– Tyson2015年11月19日 23:07:04 +00:00Commented Nov 19, 2015 at 23:07
2 Answers 2
Use the absolute path instead of the relative one.
Instead of ~/... Use /home/... (or C:/... for Windows). This avoids the difficultly in finding the file relative to your location.
-
exactly what i do not want to do. this script is to be run on multiple computersLena Bru– Lena Bru2015年11月19日 21:57:52 +00:00Commented Nov 19, 2015 at 21:57
-
1@LenaBru Ok. you didn't put that in the question, though.ztk– ztk2015年11月19日 22:01:31 +00:00Commented Nov 19, 2015 at 22:01
-
i didnt have to, the question is asking to find an anonymous folder.... ;)Lena Bru– Lena Bru2015年11月19日 22:02:16 +00:00Commented Nov 19, 2015 at 22:02
My script resides in FolderB and I want to find a folder with a specific name (FolderC) that resides in FolderA alongside FolderB
First you need to find the directory that your script is in. Then, you need to go up one directory and look for FolderB:
dir=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
folderc="$dir/../FolderC"
Answer for revised question
I do not know if FolderC resides directly above the script, it may be 2 3 or even more directories up, ...
This searches for FolderC through all parent directories up to the root directory:
dir=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
while echo dir=$dir; [ $dir != / ] && dir=$(dirname "$dir")
do
b="$dir/FolderC"
[ -d "$b" ] && break
false
done || echo "Fail no such directory"
echo "The nearest FolderC is $b"
-
I don't want to cd into the folder, I want its absolute path without cd, how do i get that?Lena Bru– Lena Bru2015年11月19日 21:57:57 +00:00Commented Nov 19, 2015 at 21:57
-
I do not know if FolderC resides directly above the script, it may be 2 3 or even more directories up, this is why i said "the closest", meaning look until you find first resultLena Bru– Lena Bru2015年11月19日 21:59:53 +00:00Commented Nov 19, 2015 at 21:59
-
(1) Sorry for the confusion: the
cdcommand is run only within a subshell. When the assignment ofdircompletes, you are back in the parent shell and thecdis gone. If you try it, you will see thatcdhas no lasting effect. (2) Thank you for the clarification.John1024– John10242015年11月19日 22:08:03 +00:00Commented Nov 19, 2015 at 22:08 -
@LenaBru See updated answer for a script that searches through parent directories.John1024– John10242015年11月19日 22:25:40 +00:00Commented Nov 19, 2015 at 22:25
-
@LenaBru Did this answer your question?John1024– John10242015年11月23日 02:15:43 +00:00Commented Nov 23, 2015 at 2:15