pwd gives me
/data/users/me/some/random/folder
Is there an easy way of obtaining
~/some/random/folder
from pwd?
pwd gives me
/data/users/me/some/random/folder
Is there an easy way of obtaining
~/some/random/folder
from pwd?
If your're using bash, then the dirs builtin has the desired behavior:
dirs +0
~/some/random/folder
(Note +0, not -0.)
With zsh:
dirs
~/some/random/folder
To be exactly, we first need to clear the directory stack, else dirs would print all contents:
dirs -c; dirs
Or with zsh's print builtin:
print -rD $PWD
or
print -P %~
(that one turns prompt expansion on. %~ in $PS1 expands to the current directory with $HOME replaced with ~ but also handles other named directories like the home directory of other users or named directories that you define yourself).
You can use Bash variable substring replacement feature:
$ pwd
/home/vagrant/foo/bar
$ echo ${PWD/#$HOME/'~'}
~/foo/bar
Using sed:
pwd | sed "s|^$HOME|~|"
The idea is to use any character that is less likely to appear in a home directory path as the delimiter in sed regex. In the example above I am using |.
The good thing about this technique is that it is actually shell-independent.
You can also alias pwd to /bin/pwd | sed "s|$HOME|~|" to get this behavior in other scripts that might be using pwd.
tilde=\~${PWD#~}
I think that's what you want?
What's neat about the tilde is when it is expanded as a pattern it doesn't need quoting. Using $HOME in that same way without quotes would render unpredictable results because any of its constituent characters could be pattern characters, and so it might not always render the result expected.
But the tilde expansion is spec'd to always occur as if it were the result of a quoted expansion, and so its results are predictable. This seems to hold true in practically every shell I've tested for both case patterns and parameter patterns with the notable exceptions of both ksh93 and mksh for whatever reasons - neither of which seems to honor the quoted aspect of the tilde in that capacity.
Another neat thing about this is that you can redefine $HOME to handle any directory at all in the same fashion. For example:
cd ~
HOME=/some/prefix/I/would/like/to/trim
#or, perhaps more usefully, some scripted means of arriving at same
tilde=\~${OLDPWD#~}
HOME=$PWD
Here's another example:
set 1 2 3 4 5 \~
cd ~; cd -; HOME= IFS=/
for d do shift
HOME=${*#~/}/$d
set ~ "$d" $HOME
done; cd -; HOME=$PWD
printf %s\\n "$@"
5/4/3/2/1/2/3/4/5/~/1/2/3/4/5/~
~
5
4
3
2
1
2
3
4
5
~
1
2
3
4
5
~
You can try replacing the unwanted part with sed:
pwd | sed 's,^/data/users/me,~,'
or even better:
pwd | sed "s,^$HOME,~,"
With Zsh it's as simple as ${(D)PWD}.
See under "Parameter Expansion Flags" in zshexpn(1):
D
Assume the string or array elements contain directories and attempt to substitute the leading part of these by names. The remainder of the path (the whole of it if the leading part was not substituted) is then quoted so that the whole string can be used as a shell argument. This is the reverse of `~' substitution: see the section FILENAME EXPANSION below.