Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I want to get just the name of the parent directory for a file.

Example: When I have path=/a/b/c/d/file, I want only d and not /a/b/c/d (which I get from dirname $path) as output.

Is there any sophisticated way to do this?

share|improve this question

4 Answers 4

It sounds like you want the basename of the dirname:

$ filepath=/a/b/c/d/file
$ parentname="$(basename "$(dirname "$filepath")")"
$ echo "$parentname"
d
share|improve this answer

You can use pwd to get the current working directory, and use parameter expansion to avoid forking it into another (sub)shell.

echo ${PWD##*/}

Edit: proven source

share|improve this answer
    
The question has nothing to do with the current directory. You could use ${path##*/} –  Matteo Jan 20 '13 at 9:22
    
Ah, I guess I misread it in my drowsiness –  Fire Jan 20 '13 at 16:59

I think this is a less-resource solution:

 $ filepath=/a/b/c/d/file
 $ echo ${${filepath%/*}##*/}
 d

edit: Sorry, nested expansion isn't possible in bash, but it works in zsh. Bash-version:

 $ filepath=/a/b/c/d/file
 $ path=${filepath%/*}
 $ echo ${path##*/}
 d
share|improve this answer
    
There are some edge cases this doesn't handle well, mainly when there isn't a full multilevel path. For example, try it with filepath=file or filepath=/file`. –  Gordon Davisson Jan 20 '13 at 22:31
    
Indeed. But what is the parent directory of foofile? If it isn't full path can't know (maybe if foofile is an existing file not only a "string"). –  uzsolt Jan 21 '13 at 8:54

In bash, in one line:

$ dirname /a/b/c/d/file | sed 's,^\(.*/\)\?\([^/]*\),\2,'
share|improve this answer
    
can you please elaborate the procedures involved? it can be of help to future readers. also, please try not to write 1/2 line answers. –  Lorenzo Von Matterhorn Apr 28 '13 at 19:18

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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