如何获取目录的绝对路径?

时间:2015-09-19 18:27:06

标签: linux bash

我想自动执行以下操作:

进入当前目录

library(dplyr)

family.head_gender =
  person %>%
  filter(Relation.to.Head == 1) %>%
  select(Id, head_gender = Gender)

family.type = 
  person %>%
  filter(Relation.to.Head == 2) %>%
  group_by(Id) %>%
  summarize(number_of_spouses = n() ) %>%
  left_join(family.head_gender) %>%
  mutate(type = 
           ifelse(head_gender == 1,
             ifelse(number_of_spouses = 1,
               1,
               3),
             2)) %>%
  select(-head_gender, -number_of_spouses)

person.final = 
  person %>%
  left_join(
    person %>%
    select(Mother.Id.In.HH = Id.In.HH,
           Mom.Birth = Birth) ) %>%
  left_join(family.type)

制作新目录

cd workdir

并获取此mydata目录的绝对路径

2 个答案:

答案 0 :(得分:3)

有一个Unix实用程序可以实现:

readlink --canonicalize nameofyourfileorfolder

答案 1 :(得分:0)

假设 cd workdir; mkdir mydata 没有失败:

dir=$(cd workdir; mkdir mydata; pwd)

稍微强健一点:

dir=$(mkdir -p workdir/mydata; pwd)

对于更强大的方法,您可以在检查创建的目录存在后运行以下命令:

dir=$(cd workdir/mydata; pwd)

如果“绝对路径”是指物理路径,请考虑使用 pwd -P 而不是 pwd