Your own shortcuts: alias and ~/.bashrc
Make a short name for a long command with alias, keep it in ~/.bashrc so it survives a new terminal, and load it with source. Practise in your browser.
Some commands you type every day, and some are long. An alias gives one a short name:
alias ll='ls -alF'llAnything you type after an alias goes on the end, so ll projects runs ls -alF projects. Use single quotes around the command and, as with variables, no spaces around the =.
aliason its own lists your aliases;type llsays what a name runs.unalias llremoves one.- An alias can hold several commands:
alias up='cd .. && ls'.
Making it last
Aliases disappear when the terminal closes. To keep one, put the alias line in ~/.bashrc, a file bash reads every time a terminal opens. It's where your settings live:
cat ~/.bashrcYou can add a line with an editor, or with echo and >>:
echo "alias gs='git status'" >> ~/.bashrcCareful:Two arrows, >>, add to the end. One arrow, >, replaces the whole file, and everything else in your .bashrc is gone.
A change to .bashrc takes effect in new terminals. To load it into this one now, run source ~/.bashrc.
Your turn
Make an alias called projects that runs cd ~/projects && ls, and try it. Then make it last: add the same alias line to the end of ~/.bashrc, keeping what is already there, and load the file with source.
Your task
Make an alias called projects that runs cd ~/projects && ls, and try it. Then make it last: add the same alias line to the end of ~/.bashrc, keeping what is already there, and load the file with source.
- The projects alias works (not done yet)
- ~/.bashrc has the alias, and its old lines (not done yet)
- Load it with source (not done yet)
The checklist ticks itself off as you work in the terminal: any way that gets the result counts.
Hints come one at a time, then one way to do it. Try each before the next.
What this lesson uses, in one place.
alias- Make a short name for a longer command More on alias
unalias- Remove an alias
~/.bashrc- The file bash reads each time a terminal opens: aliases and settings that last
source- Run a file’s commands in the current shell
Every everyday command on one page: the Linux commands cheat sheet.