2

I am trying to get the directory name of this file, which is not located in my current directory:

/home/me/PhD/file.tsv

when I run:

dirname /home/me/PhD/file.tsv

I get:

/home/me/PhD/

But, I would like to obtain just: PhD

How can I do this?

7
  • 2
    What OS are you running? Commented May 6, 2020 at 10:28
  • @ctrl-alt-delor I am using an enterprise linux server
    – Jeni
    Commented May 6, 2020 at 10:39
  • Okay, I've edited the question: when I run exactly dirname file.tsvI get . and when I run dirname /home/me/PhD/file.tsvI get /home/me/PhD/
    – Jeni
    Commented May 6, 2020 at 10:49
  • The trailing slash looks suspicious but it doesn't matter that much, I guess. Commented May 6, 2020 at 10:50
  • Yes but what OS? I could guess that it is a Gnu/Linux. Commented May 7, 2020 at 20:29

1 Answer 1

7
basename "$(dirname /home/me/PhD/file.tsv)"

Notes:

  • dirname and basename operate purely on strings. It doesn't matter if the path exists.
  • $() is explained here: What is $() in a command?

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .