Chapter 5 Tips and tricks
5.1 Tab complete
Tab complete is a method to quickly type commands and file paths without error. In bioinformatics tab complete is your best friend. The tab key looks like:
Tab complete serves the following purposes:
- To complete path names and commands
- To list all content that starts with what you have already typed
- To ensure you have no typos
In the below examples press the tab button when you see <tab> and replace 2xx with your user number.
Move into the directory "1_directory"
Print out all the content within the current directory that starts with "1_" in the file or directory name. This is carried out with a double tab.
Note: the "./" is put before the 1 so it only looks in the current directory otherwise it will also look for commands.
List the contents of directory "1_1_directory"
Change directory to "1_2_directory". The "./" is not needed before the file name as tab will only look for directories because it is used for an argument of the cd
command.
Change directory to example_1_part_1. The last tab will add the "/" to the end of the directory name, this informs you that you have correctly and fully typed in the directory name. However, this will not always occur if there is another directory name that starts the same but is longer. That is where double tab comes in handy.
In this practical session I have given paths purposefully long names. This has been carried out to demonstrate the usefulness of tab complete and to encourage its use. Although they have been artificially extended in this case, in Bioinformatics long and informative path names are advised.
5.2 Ending a command
There are times when you will want to abandon a command on the command line. To do this simple press ‘Ctrl’ + ‘c’.
This is useful if a command won't respond or you noticed you have run a command with a typo or with the wrong file.
5.3 History
Linux will save commands you have previously entered. In the terminal, whilst at the command line, you can press the up and down keys to scroll through your history. You can then rerun previous commands or edit them with the left and right arrow keys, and run the edited version.
5.4 Clear
The clear
command can be used to clear all the text from the terminal. This is useful for keeping a tidy terminal.
5.5 Bash escape
To continue a command on a new line on the command line use the backslash character, \
. When you press \
followed by enter, the command will not run and you will be on a new line on the command line. This can be useful for clarity and for long commands.
In the below example press "enter" after the end of a line.
Print to screen the term “Hello universe, today is a very nice day. Don’t you think so?”
Notice that there is a space before the \
. This is because there needs to be a space between the echo and text to print out. It is always recommended to use a space before a \
to bash escape.
Bash escape is useful for this document as it will show if commands in this document are separate commands or one command over multiple lines.
5.6 Annotations
You can annotate your code so it will not run. This is carried out by putting a #
at the start of a code line.
An example:
#This line is annotation and will not run
#The below line will print out the text "this line is not annotated"
echo "this line is not annotated"
This is useful to give yourself information about what your code is doing and it is vital if you are creating scripts. We will also use annotations in this workbook to explain what some lines of code are doing.
I also find it useful to put a #
at the front of a long command that I am typing or editing. This means the command won't run if I accidentally press enter. However, be sure to remove the #
at the start of the line before you want to run it.
5.7 MCQs: Tips & tricks
Please attempt to answer the below Multiple-Choice Questions to reinforce what you have learnt in this chapter.
- What allows you to auto fill paths and commands so you can quickly type without error?
- What command clears text from the terminal?
- What symbol is used for annotation?
- What keyboard short cut cancels/ends a command?
- What keys scroll through your previously run commands (aka history)?
- Bash escape allows you to run commands over multiple lines. What symbol is used for bash escape?