Take the 2-minute tour ×

In my shell script I have a string of a file path.
I want to remove the file part and get just the path to the directory.

For example, if the path is: path="/var/www/index.html" I want to get /var/www/.

Is there something similar to the tail option with word designators?


share|improve this question
add comment

3 Answers

up vote 4 down vote accepted

Try ${parameter%word}:

~ $ path="/var/www/index.html"
~ $ dir=${path%/*}
~ $ echo $dir
share|improve this answer
add comment

Take a look at the basename and specifically dirname commands.

To remove the "basename", run dirname on the path.

share|improve this answer
add comment

Try dirname.


$ dirname /var/www/index.html
share|improve this answer
add comment

Your Answer


