3

I need to print the path of the current, parent and root directory in UNIX. I can use commands, or a shell script. I managed to do that for the current and the parent directory, but I do not know how to print it for the root directory.

Here is what i have done:

  • current: pwd
  • parent: echo $(pwd) | sed -e s/\\/[^\\/]*$//
NeronLeVelu
  • 9,908
  • 1
  • 23
  • 43
dd1714
  • 139
  • 1
  • 6

3 Answers3

3

gnu sed

echo $(pwd) | sed -r -e 'p; :a; s#(.*)/.*#\1#; H; ta; x' | sed '/^$/d'  

this will print all directories from current -> root

it uses greedy search for '/' keeping only left part, appending in hold space, ta is conditional branch for substutution, so if there is one it branches to :a, at the end x is swaping hold space to pattern space.
Last sed command is for clearing empty lines

josifoski
  • 1,696
  • 1
  • 14
  • 19
  • this does not work. I got some error and then the path of the current directory printed twice – dd1714 Jun 07 '15 at 22:39
  • os system? version of sed? is line copy/pasted? here it works :) – josifoski Jun 07 '15 at 22:40
  • i have free bsd installed on virtual box. i did not use copy/paste, but the line is correctly written – dd1714 Jun 07 '15 at 22:44
  • it works now? (with deleted \s in last sed command) I think that part is incompatible. \s stands for whitespace, standard is [[:space:]] – josifoski Jun 07 '15 at 22:47
  • 2
    BSD sed doesn't terminate commands that use labels at semicolons like GNU sed. Try `pwd | sed -n -e ':a' -e '/./p; s#/[^/]*$##; ta'` Note that every time a label name is used, the `-e` option ends. – Wintermute Jun 07 '15 at 22:51
  • so -e option is key :) tnx – josifoski Jun 07 '15 at 22:53
  • no.. i get the same thing. Is says something about unused label 'a; s#(.*)/.*#\1#; H; ......' – dd1714 Jun 07 '15 at 22:53
  • @josifoski Just using `-e` doesn't help. The important bit is to split the sed code after every use of a label name because otherwise BSD sed will treat the rest of the code as part of the label name. You could use `-e 'p; :a' -e 's#....'`. Depending on the version of FreeBSD, you'd also need to use `-E` instead of `-r`. – Wintermute Jun 07 '15 at 22:57
  • tnx again, only advice will be using gnu sed, it's difficult engine (and cute also) enough to stumble with this issues – josifoski Jun 07 '15 at 22:59
1

I think it's easier to do this using awk:

echo $(pwd) | awk 'BEGIN {FS=OFS="/"} {l=NF; while (i++<l-1) {print; NF--}}'

Explanation

This will print all directories from current --> root. It does this by printing the line, then decrementing the NF, the number of fields. Notice that I have set both the input and output field separators to the /.

bkmoney
  • 1,256
  • 1
  • 11
  • 19
1

Same idea as @josifoski

pwd | sed ':a;\#/\(.*\)/[^/]*# {p;s##/\1#;ba}'

Print and cycle until only 1st folder name, removing every cycle the last folder of the path

NeronLeVelu
  • 9,908
  • 1
  • 23
  • 43
  • some new ideas here, needs explanation (learning), why it starts with "\#" +1 – josifoski Jun 08 '15 at 06:35
  • using another separator than the default `/` (often when is part of the pattern), escaping the first character, specify that this character is the new delimiter for next pattern, in this case `#`, same idea but no need of escape with `s` action in `s###` – NeronLeVelu Jun 08 '15 at 06:39
  • new for me was also that group can be marked in address reference – josifoski Jun 08 '15 at 06:46
  • in fact a empty search pattern like `s//` mean, use last pattern (not content of the regex, the regex itself) so `/./ s//t/;s//z/` with `foo` will change to `too`and finaly to `zoo` changing first letter (on for `f` and next for `t`) – NeronLeVelu Jun 08 '15 at 07:02
  • You've got me! I was ashamed to ask why s##/\1# with empty pattern, now i understand! – josifoski Jun 08 '15 at 07:19