There are quick'n'easy commands to goto the previous dir
-- 'cd -' , which cb aliased as 'p' --
& goto the next-higher dir -- 'cd ..' , which cb aliased as 's' -- ,
but is there a way to set up a qne command to goto a parallel dir,
eg if you're in ~/tmp goto ~/hold ( 2 of my commonly-used dirs) ?
It needs to be a Bash function, so in ~/.bashrc
I tried 'function cd2() { cd .. ; cd $1 ; }',
so that 'cd2 hold' would take me where I wanted to go,
but it simply dropped me in ~ , the 2nd half being ignored.
It cb done with a shell var,
ie 'function cd2() { NEWDIR=$1 ; cd .. ; cd $NEWDIR ; NEWDIR= ; }',
which works but is a bit lengthy & could clash with an existing shell var.
The elegant way is 'function cd2() { cd .. ; cd $"$1" ; }' ;
the " ... " are essential: it fails without them or with ( ... ) instead.
HTH a few others.
--
========================,,======================== ====================
SUPPORT ___________//___, Philip Webb
ELECTRIC /] [] [] [] [] []| Cities Centre, University of Toronto
TRANSIT `-O----------O---' purslowatchassdotutorontodotca
11-12-2010, 05:08 PM
Bill Longman
bash scripting tip
On 11/12/2010 09:57 AM, Philip Webb wrote:
> There are quick'n'easy commands to goto the previous dir
> -- 'cd -' , which cb aliased as 'p' --
> & goto the next-higher dir -- 'cd ..' , which cb aliased as 's' -- ,
> but is there a way to set up a qne command to goto a parallel dir,
> eg if you're in ~/tmp goto ~/hold ( 2 of my commonly-used dirs) ?
>
> It needs to be a Bash function, so in ~/.bashrc
> I tried 'function cd2() { cd .. ; cd $1 ; }',
> so that 'cd2 hold' would take me where I wanted to go,
> but it simply dropped me in ~ , the 2nd half being ignored.
>
> It cb done with a shell var,
> ie 'function cd2() { NEWDIR=$1 ; cd .. ; cd $NEWDIR ; NEWDIR= ; }',
> which works but is a bit lengthy & could clash with an existing shell var.
>
> The elegant way is 'function cd2() { cd .. ; cd $"$1" ; }' ;
> the " ... " are essential: it fails without them or with ( ... ) instead.
>
> HTH a few others.
>
cd ${PWD/old/new}
works when you're in /some/old/tree/directory and you want to go to
/some/new/tree/directory
11-12-2010, 05:31 PM
Philip Webb
bash scripting tip
101112 Bill Longman wrote:
> On 11/12/2010 09:57 AM, Philip Webb wrote:
>> but is there a way to set up a command to goto a parallel dir,
>> eg if you're in ~/tmp goto ~/hold ( 2 of my commonly-used dirs) ?
>> The elegant way is 'function cd2() { cd .. ; cd $"$1" ; }'.
> cd ${PWD/old/new}
> works when you're in /some/old/tree/directory
> and you want to go to /some/new/tree/directory
It works, but wouldn't be as useful for what I want to do,
as you'ld have to enter both 'old' & 'new' after the function command.
ie instead of 'cd2 hold', you'ld have to write 'cd2 tmp hold':
it doesn't work if you try 'cd ${PWD/./hold}', which leaves you in 'tmp'.
For generally jumping round the dir tree, I recommend Cdargs.
--
========================,,======================== ====================
SUPPORT ___________//___, Philip Webb
ELECTRIC /] [] [] [] [] []| Cities Centre, University of Toronto
TRANSIT `-O----------O---' purslowatchassdotutorontodotca
11-12-2010, 05:36 PM
Hilco Wijbenga
bash scripting tip
On 12 November 2010 09:57, Philip Webb <purslow@ca.inter.net> wrote:
> It needs to be a Bash function, so in *~/.bashrc
> I tried 'function cd2() { cd .. ; cd $1 ; }',
Doesn't
function cd2() { cd ../$1 }
work? (I haven't tried it.)
11-12-2010, 06:17 PM
Hilco Wijbenga
bash scripting tip
On 12 November 2010 10:36, Hilco Wijbenga <hilco.wijbenga@gmail.com> wrote:
> On 12 November 2010 09:57, Philip Webb <purslow@ca.inter.net> wrote:
>> It needs to be a Bash function, so in *~/.bashrc
>> I tried 'function cd2() { cd .. ; cd $1 ; }',
>
> Doesn't
>
> function cd2() { cd ../$1 }
>
> work? (I haven't tried it.)
So yes, this:
function cd2() { cd ../$1; }
works.
11-12-2010, 06:36 PM
BRM
bash scripting tip
----- Original Message ----
> From: Hilco Wijbenga <hilco.wijbenga@gmail.com>
> On 12 November 2010 10:36, Hilco Wijbenga <hilco.wijbenga@gmail.com> wrote:
> > On 12 November 2010 09:57, Philip Webb <purslow@ca.inter.net> wrote:
> >> It needs to be a Bash function, so in ~/.bashrc
> >> I tried 'function cd2() { cd .. ; cd $1 ; }',
> >
> > Doesn't
> >
> > function cd2() { cd ../$1 }
> >
> > work? (I haven't tried it.)
>
> So yes, this:
>
> function cd2() { cd ../$1; }
>
> works.
Something I have found useful is the pushd/popd functions in Bash.
Of course, to use them the way you want to you'd have to use two step procedure:
You could probably modify the above do pull out the initial directory from a
single string by - e.g. turn /my/path/parent/child into /my/path/parent - as
well.
You could also process the DIRSTACK variable (or use the 'dirs' command) to see
if the parent directory is already on the stack too.
Note: I have the redirs in there because pushd/popd by default dumps the
DIRSTACK as its output.
$0.02
Ben
11-12-2010, 07:36 PM
Philip Webb
bash scripting tip
101112 Hilco Wijbenga wrote:
> On 12 November 2010 09:57, Philip Webb <purslow@ca.inter.net> wrote:
>> I tried 'function cd2() { cd .. ; cd $1 ; }',
> Doesn't 'function cd2() { cd ../$1 ; }' work ? -- Yes
Yes, you're correct (slightly red face) !
I'm not sure why I didn't try that variation originally.
--
========================,,======================== ====================
SUPPORT ___________//___, Philip Webb
ELECTRIC /] [] [] [] [] []| Cities Centre, University of Toronto
TRANSIT `-O----------O---' purslowatchassdotutorontodotca