Posted by: adamkasza | April 10, 2009

Determining the parent directory from Bash

There must be a lot more straightforward way of doing this, but I failed to find it. Therefore I (re)invented a method of determining the parent directory from Bash:

#!/bin/sh
if [ x"$1" != x"" ]
then
  readlink -f $1 | tr '/' ' ' | gawk '
  {
     for (v=1;v < NF;v++)
       printf("/%s",$v)

     printf("\n")
  }'
fi

As I mentioned there must be an easier way… Anyway, I have this script a file called ‘parentname’.


Responses

  1. dirname $( pwd ) | tr / \\n | tail -1


Leave a response

Your response:

Categories