0

Edit I am still wondering why this question is closed. Parent is not what I was looking for. Maybe somebody can explain why this question is not opened again? Thank you.

This question has nothing to do with "parent"! So, I do not know why the question was closed and an answer for a parent directory was mentioned.

A question about bash on macOS

I have a file name which is something like

/Users/johndoe/Desktop/folder/file.txt

But I only need the path part

/Users/johndoe/Desktop/folder

with or without the last /, both is OK.

I have no idea how to do it, so I can't provide anything I have tried.

//Edit: There is a discussion now if I asked for the parent path or not. I didn't. Here is the definition what a parent path is.

Christoph S.
  • 585
  • 3
  • 16
  • https://mywiki.wooledge.org/BashFAQ/073 – jordanm Mar 19 '21 at 20:41
  • 1
    In plain bash: `path='/Users/johndoe/Desktop/folder/file.txt'; echo "${path%/*}"` – M. Nejat Aydin Mar 19 '21 at 20:42
  • so many duplicates: [get parent directory of a file in bash](https://stackoverflow.com/q/40700119/995714), [Get the parent directory of a given file](https://unix.stackexchange.com/q/351916/44425), [Getting the parent of a directory in Bash](https://stackoverflow.com/q/8426058/995714), [Extract directory from path](https://stackoverflow.com/q/6509650/995714), [How can I get the source directory of a Bash script from within the script itself?](https://stackoverflow.com/q/59895/995714) – phuclv Mar 20 '21 at 01:22
  • @phuclv No, because I do not search for the parent directory. I already found the solution: `dirname` – Christoph S. Mar 21 '21 at 21:34
  • `/Users/johndoe/Desktop/folder` **is** the parent folder of `/Users/johndoe/Desktop/folder/file.txt`. All the above questions ask exactly that and the answer is also `dirname`. Did you even read them? – phuclv Mar 22 '21 at 02:59
  • @phuclv This is not the parent path. The parent path would be `/Users/johndoe/Desktop/` – Christoph S. Mar 22 '21 at 07:17

1 Answers1

1
dirname /Users/johndoe/Desktop/folder/file.txt
Alex028502
  • 3,486
  • 2
  • 23
  • 50