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

Commit

Permalink
Find dir where certain dir or file exists
Browse files Browse the repository at this point in the history
  • Loading branch information
bas080 committed Aug 13, 2018
1 parent 2c3aeb7 commit 6a8cbdd
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1013,6 +1013,30 @@ $ basename ~/Pictures/Downloads/
Downloads
```
## Find parent directory that contains a certain file or directory
**Example Function:**
```sh
parent_find() {
[[ $1 == '/' ]] && return 1
[[ -e "$1/$2" ]] && printf '%s\n' "$1" && return 0
parent_find "$(dirname "$(realpath "$1")")" "$2"
}
```
**Example Usage:**
```shell
$ pwd
/home/black/Projects/awesome/src/assets
$ parent_find "$PWD" .git
/home/black/Projects/awesome/
```
<!-- CHAPTER END -->
<!-- CHAPTER START -->
Expand Down
10 changes: 10 additions & 0 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,16 @@ test_basename() {
assert_equals "$result" "1.jpg"
}

test_parent_find() {
local dir="$PWD/"

cd ./manuscript/

assert_equals "$dir" "$(parent_find "$PWD" .git)"

cd ../
}

test_hex_to_rgb() {
result="$(hex_to_rgb "#FFFFFF")"
assert_equals "$result" "255 255 255"
Expand Down

0 comments on commit 6a8cbdd

Please sign in to comment.