Ahmad
Ahmad

Reputation: 9658

Relative path to the home directory (linux)

When I use pwd it returns:

/home/ahmad/multifit-pretrain-lm/multifit/datasets

I look for a command so that I get the path relative to the home directory

multifit-pretrain-lm/multifit/datasets

Upvotes: 3

Views: 2710

Answers (2)

xhienne
xhienne

Reputation: 6134

Variable substitution is a good solution but since you asked for a command, here is one:

realpath --relative-base="$HOME" .

Upvotes: 2

anubhava
anubhava

Reputation: 785156

I look for a command so that I get the path relative to the home directory

You can use bash parameter substitution here:

echo "${PWD#"$HOME"/}"
  • $PWD shell variable returns same value as the command pwd
  • ${PWD#"$HOME"/} strips "$HOME"/ at the start from $PWD

Upvotes: 5

Related Questions