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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.