At Super User we were recently asked the question
Is there any shortcut for “cd ..”?
Well, I answered it, thinking it was just another run-of-the-mill quick questions and answers that we get many of every day. Little did I know, but things were going to take a turn in a slightly surreal direction… My answer, for some unfathomable reason, got an insane amount of upvotes. 50 to be precise, and still counting, and I am not exactly sure why.
The original asker of the question was looking for a shorter way to run the command
cd .., which is used to change to the parent directory. The answer I gave provided this simple shell alias:
$ alias ..="cd .."which allows the user to use the
..command in place of
I didn’t really think anything of this answer and just though it was a simple answer that would help the person who asked the question. Little did I know that it would receive more than 50 upvotes. I’m also slightly surprised that the original poster selected my answer as the one to accept, when other answers (such as the one about a
shopt trick) were, in my opinion, better than mine. Maybe the massive amount of upvotes is from how simple my answer was, and from the fact that it works and is probably the shortest command given.
What is Alias?
alias command is used to substitute (or alias) one command for another. It works like this:
$ alias newcommand=shortcommandAfter running this command in a shell, or putting it in a shell resource file such as
~/.bash_profile, you can type
oldcommandwill be run. This is often used to make commonly used commands easier to type.
aliascan be used on any OS (even Windows, if you use Cygwin) with the bash shell (it can probably be used with other shells as well, such as zsh). I use quite a few aliases myself, like
exit. Using these aliases makes the commands shorter, therefore faster to type and making you more productive.
Here’s an example of how it can make you more productive:
This screenshot shows an example of the command
$ git log --graph --pretty=oneline --abbrev-commitThat would take a long time to type, and you might not remember all the command line flags in order to make it work. So you’d have to figure out all the flags, which would take away from the time you could be using to actually get stuff done. To make this easier, you can use an alias. I have this command aliased to
gll. There’s no funky command line flags to trip up on. Just a simple, three letter command. You just simply type these three letters, look at the git log shown, and get on with being productive!
You can also use aliases for parts of commands. For instance, say I want to use that
gll alias, but I want to append the
-5 switch to the command so it’ll only show me five commits. So I could just do:
$ gll -5And it will run the command:
$ git log --graph --pretty=oneline --abbrev-commit -5This can be used in any situation where you want to put arguments at the end of a long command using an alias.
Is there anything better than Alias?
There were a few other alternatives offered for that question, a few of which using aliases as well, but a few not. The one I found most interesting used
shopt is short for “Shell Options” and is (logically) used to set various options within the shell. One of these options is “autocd”:
$ shopt -s autcdThe autocd option converts all directories into commands which, when run, change into that directory, for example:
$ /tmp/Will expand to:
$ cd /tmpAnother interesting alternative I saw given was the use of the
bindcommand to allow you to link a single keypress to a command:
$ bind '"eu":"cd ..C-m"'Running this command (or of course putting it in a shell resource file, such as
~/.bash_profile) will let you press Control+U and it will automatically do the
So what does using aliases etc all equate to?
- Fewer keystrokes means you type your commands faster
- You don’t have to remember complex commands
- Your keyboard will last longer.
So next time you find yourself typing a long command for the second or third time, why not consider aliasing it?
Filed under Question of the Week