Shorten an absolute path
JavaScript (ES6), 107 106 bytes

Takes the absolute path a and the current path c in currying syntax (a)(c).

a=&gt;c=&gt;(A=a.split`/`,s='',c.split`/`.map(d=&gt;!s&amp;A[0]==d?A.shift():s+='../'),s+=A.join`/`)[a.length]?a:s||'.'


Test cases



let f =

a=&gt;c=&gt;(A=a.split`/`,s='',c.split`/`.map(d=&gt;!s&amp;A[0]==d?A.shift():s+='../'),s+=A.join`/`)[a.length]?a:s||'.'

console.log(f
  ('/home/user/mydir/myfile')
  ('/home/user')
);

console.log(f
  ('/var/users/admin/secret/passwd')
  ('/var/users/joe/hack')
);

console.log(f
  ('/home/user/myfile')
  ('/tmp/someplace')
);

console.log(f
  ('/dir1/dir2')
  ('/dir1/dir2/dir3/dir4')
);

console.log(f
  ('/dir1/dir2')
  ('/dir1/dir2')
);