Here are some useful terminal hacks.
Aliases
Have a lengthy command and want to make shorter? Use aliases. You can add aliases to your .bashrc file so that they remain permanent.
Consider the following command sudo apt-get install [package name] . You can reduce the number of instructions by adding an alias to the .bashrc file:
alias install='sudo apt-get install'
The .bashrc file is a shell script file, generally used as a user specific configuration file for the BASH (Bourne Again SHell) shell. It is located in a user’s home directory and will be executed when that user logs into bash. Use ls -a flag to list all files including hidden files starting with a full stop.
After saving the .bashrc file and restarting the terminal you can now run install zip instead of sudo apt-get install zip .
The syntax is always shortname=’long command’ without spaces either side of the = sign.
PS1 (Prompt String 1)
You can customise the prompt to make it more meaning full.
Historically the original Bourne shell would use a $ as the normal user prompt and # for the root user prompt. This made it easy to tell if you were running as a superuser. The # is also a the comment character, so anyone blindly entering data wouldn’t run any real commands.
A few examples.
- Check the current PS1 prompt: echo $PS1
- Remove the computer name from the prompt: PS1=’\u: \w\$’
- Add system inbuilt variables: PS1=’$SECONDS [u@h W]$ ‘
- Add a command output in the prompt: PS1=’$(uptime) [u@h W]$ ‘
Tab completion
Just tap tab whenever you want to autocomplete. For example if you’re writing the command cd med then press tab you will get cd media/ and if you tap tab again you get cd media/cdrom .
Pressing tab twice will list commands. For example, if you type ls then press tab twice you will get a list of all commands starting with ls :
ls lsblk lshw lslocks lspci lsusb lsattr lscpu lsmod lspgpot
Bash History
If you want to view your bash history run the command history | less or history | tail -n 15 to get only the last 15 lines of your history.
Commands are written to /home/username/.bash_history
Fun
Want to watch Star Wars in the terminal?
Run telnet towel.blinkenlights.nl
To exit press CTRL + ] followed by quit .