TheShadowKnows
TheShadowKnows

Reputation: 15

How would I create the command "cd.." to work as "cd .." in linux?

I am not a frequent Linux user, and really only use it for servers. I'm wondering if there would be an easy way to make the command "cd.." work as it does in Windows (going up one directory), I understand that the proper way to do this in general is to use "cd .." but when it comes down to muscle memory I find myself doing the former all the time before my brain kicks in and reminds me that it should be the latter.

I have tried creating a file at /bin/cd.. with the following contents:

#!/bin/bash
cd ../

But upon executing it does nothing, not really sure what I did wrong here so if anyone has some guidance it would be appreciated.

Thanks!

Upvotes: 0

Views: 641

Answers (2)

Gereon
Gereon

Reputation: 17882

A cd command in a shell script does nothing to the invoking shell - that's a feature. You need to run the command in the context of the current shell, and that's what aliases are for.

Put alias "cd.."="cd .." into your .bash_profile, that should to the trick.

Upvotes: 1

Charlie Wilson
Charlie Wilson

Reputation: 284

Add the alias to your bash profile at $HOME/.bash_profile. You likely will need to restart your terminal session for the profile to be loaded. It will be there from that point forward. The $HOME/.bashrc may not get you what you want, that is for non-interactive sessions.

# $HOME/.bash_profile

alias cd..='cd ..'

# Some other handy ones?
alias ..='cd ..'
alias ../..='cd ../..'

Upvotes: 1

Related Questions