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

Find dir where certain dir or file exists #35

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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