Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit 6a8cbdd

Browse files
committed
Find dir where certain dir or file exists
1 parent 2c3aeb7 commit 6a8cbdd

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,6 +1013,30 @@ $ basename ~/Pictures/Downloads/
10131013
Downloads
10141014
```
10151015
1016+
## Find parent directory that contains a certain file or directory
1017+
1018+
**Example Function:**
1019+
1020+
```sh
1021+
parent_find() {
1022+
[[ $1 == '/' ]] && return 1
1023+
1024+
[[ -e "$1/$2" ]] && printf '%s\n' "$1" && return 0
1025+
1026+
parent_find "$(dirname "$(realpath "$1")")" "$2"
1027+
}
1028+
```
1029+
1030+
**Example Usage:**
1031+
1032+
```shell
1033+
$ pwd
1034+
/home/black/Projects/awesome/src/assets
1035+
1036+
$ parent_find "$PWD" .git
1037+
/home/black/Projects/awesome/
1038+
```
1039+
10161040
<!-- CHAPTER END -->
10171041
10181042
<!-- CHAPTER START -->

test.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,16 @@ test_basename() {
109109
assert_equals "$result" "1.jpg"
110110
}
111111

112+
test_parent_find() {
113+
local dir="$PWD/"
114+
115+
cd ./manuscript/
116+
117+
assert_equals "$dir" "$(parent_find "$PWD" .git)"
118+
119+
cd ../
120+
}
121+
112122
test_hex_to_rgb() {
113123
result="$(hex_to_rgb "#FFFFFF")"
114124
assert_equals "$result" "255 255 255"

0 commit comments

Comments
 (0)