1

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 ?

asked Nov 19, 2015 at 21:15
1
  • 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. Commented Nov 19, 2015 at 23:07

2 Answers 2

0

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.

answered Nov 19, 2015 at 21:40
3
  • exactly what i do not want to do. this script is to be run on multiple computers Commented Nov 19, 2015 at 21:57
  • 1
    @LenaBru Ok. you didn't put that in the question, though. Commented Nov 19, 2015 at 22:01
  • i didnt have to, the question is asking to find an anonymous folder.... ;) Commented Nov 19, 2015 at 22:02
0

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"
answered Nov 19, 2015 at 21:43
5
  • I don't want to cd into the folder, I want its absolute path without cd, how do i get that? Commented 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 result Commented Nov 19, 2015 at 21:59
  • (1) Sorry for the confusion: the cd command is run only within a subshell. When the assignment of dir completes, you are back in the parent shell and the cd is gone. If you try it, you will see that cd has no lasting effect. (2) Thank you for the clarification. Commented Nov 19, 2015 at 22:08
  • @LenaBru See updated answer for a script that searches through parent directories. Commented Nov 19, 2015 at 22:25
  • @LenaBru Did this answer your question? Commented Nov 23, 2015 at 2:15

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.