Sagar Jain
Sagar Jain

Reputation: 33

Change a part of current directory using cd in linux

Assuming user is in dir1 and want to cd to dir2, how can he do it using cd by just telling that replace test with src in current working directory?

Upvotes: 0

Views: 134

Answers (1)

Keith Thompson
Keith Thompson

Reputation: 263617

If you're using bash (the most common default shell), you could do this:

cd ${PWD/test/src}

documented here.

If you're using zsh, it has a built-in feature for this:

cd test src

man zshbuiltins and search for cd.

(And both of these techniques can be used in scripts, so I'd argue that this is a valid programming question.)

On the other hand, depending on how often you do this and how many directories you're dealing with, it might make more sense to set some variables:

testfiles=/home/user1/test/package/files
srcfiles=/home/user1/src/package/files
cd $testfiles
cd $srcfiles

Upvotes: 1

Related Questions