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


when I run:

dirname /home/me/PhD/file.tsv

I get:


But, I would like to obtain just: PhD

How can I do this?

  • 2
    What OS are you running? May 6, 2020 at 10:28
  • @ctrl-alt-delor I am using an enterprise linux server
    – Jeni
    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
    May 6, 2020 at 10:49
  • The trailing slash looks suspicious but it doesn't matter that much, I guess. May 6, 2020 at 10:50
  • Yes but what OS? I could guess that it is a Gnu/Linux. May 7, 2020 at 20:29

1 Answer 1

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


  • 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 .