Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

cd to a directory using a filename

28 views
Skip to first unread message

waset2...@gmail.com

unread,
May 28, 2018, 1:08:50 AM5/28/18
to
Sometimes I cd to a directory (that is not in my $CDPATH) with an environmental variable pointing to a filename in that directory e.g.

cd $( dirname $filename )

It works but I'd like to simplify it.

I tried a script which I named cdf but that just executed in a subshell.

a=$( dirname $1 )
/usr/bin/cd $a


I tried a function in my .bashrc but that didn't execute the dirname for some reason.

cdf() { /usr/bin/cd $( dirname "$@" ) ; }


I tried an alias in my .bashrc but that also did not execute the dirname.

alias cdf=cd `dirname "$@"`


What am I doing wrong? Thanks.

Grant Taylor

unread,
May 28, 2018, 1:25:52 AM5/28/18
to
On 05/27/2018 11:08 PM, waset2...@gmail.com wrote:
> Sometimes I cd to a directory (that is not in my $CDPATH) with an
> environmental variable pointing to a filename in that directory e.g.

Okay.

> What am I doing wrong?

I don't know what you're doing wrong per say. My guess would be that
the variable is expanded when the alias / function is set. There may be
a way to escape the variable so that it's expanded when said alias /
function is executed.

I would forgo the sub-shell with the dirname command. Bash and Zsh both
support removing suffixes (and prefixes) when expanding variables. So
it's possible to do something like this:

cd ${filename%/*}

> Thanks.

Good luck. Please let us know what you find out.



--
Grant. . . .
unix || die

waset2...@gmail.com

unread,
May 28, 2018, 2:21:11 AM5/28/18
to
I tried your suggestion, Grant, but unfortunately, I had no luck with this either:

cdf() { /usr/bin/cd "${@%/*}" ; }

Thanks for your assistance.

Casper H.S. Dik

unread,
May 28, 2018, 3:21:39 AM5/28/18
to
/usr/bin/cd is basically a no-op.

The simple reason is that "/usr/bin/cd" run as a separate process with its
own directory.

You should use "cd" or "chdir" (chdir is an alias to cd in most shells, specifically
so you can make an alias "cd" or a function called "cd" and use "chdir" to
implement it.

So:
cdf() { cd "${@%/*}" ; }

Casper

waset2...@gmail.com

unread,
May 28, 2018, 3:48:24 AM5/28/18
to
On Monday, May 28, 2018 at 12:21:39 AM UTC-7, Casper H.S. Dik wrote:
> waset2nekhen writes:
>
> >On Sunday, May 27, 2018 at 10:25:52 PM UTC-7, Grant Taylor wrote:
Thanks, Casper! That works perfectly. I should have known to try without using /usr/bin.

Kaz Kylheku

unread,
May 28, 2018, 2:15:34 PM5/28/18
to
On 2018-05-28, waset2...@gmail.com <waset2...@gmail.com> wrote:
> Sometimes I cd to a directory (that is not in my $CDPATH) with an environmental variable pointing to a filename in that directory e.g.
>
> cd $( dirname $filename )
>
> It works but I'd like to simplify it.

cdf()
{
cd $(dirname "$1")
}

?

> cdf() { /usr/bin/cd $( dirname "$@" ) ; }

cd is *necessarily* a builtin because it changes the current
working directory of the invoking process.

It cannot work as an external executable.

https://unix.stackexchange.com/questions/50058/what-is-the-point-of-the-cd-external-command
0 new messages