Useful tricks for running commands#
As commands can become quite complex in their structure when adding multiple options(s) and argument(s) and any typing error will lead a failed command, it is important to make your commands as reproducible as possible. One way to achieve this is by using tricks for running commands that make the typing more efficient, prevent mistakes and allow tracking of commands that you ran previously to ensure you can re-run them. And importantly, it saves a lot of time.
You can use the ‘Up’ and ‘Down’ arrow keys to navigate through previously used commands (known as your history) and repeat or modify them.
When typing a command or file name, you can press the ‘Tab’ key to auto complete what you are typing. If there are multiple commands or files with similar names, auto completion will fill in as far as the first ambiguous character before you have to give it some more input. This method makes it much less likely that you make a spelling error. Also, if you double press the ‘tab’ key all the available options to complete will be shown.
Windows: To copy text from the terminal you will have to ‘Highlight’ -> ‘Right-click’ -> ‘Copy’. Similarly you have to use the in-browser menu ‘Right-click’ -> ‘Paste’ to paste your selected text into the terminal. The reason for this is that Ctrl + c and Ctrl + v have effects inside the terminal.
Mac: You can fortunately use ‘Cmd + c’ and ‘Cmd + v’ to copy and paste as normal. You can use Ctrl and various keys for in-terminal commands.
Pressing ‘Ctrl + l’ will clear the screen.
Pressing ‘Ctrl + r’ will allow you to search through your command history.
See previous commands by typing history and pressing enter.
Pressing ‘Ctrl + c’ will send an interrupt signal that cancels the currently running command and brings you back to the command line.
‘Double-click’ to select a word, ‘Triple-click’ to select a line
Using a ‘#’ character as part of a command allows you to make comments that have no effect when run.
Exercise 1.2#
Exercise 1.2
Use the arrow key to execute the same command again
# Press the up arrow once and the last command appears
echo 'My first command'
Try typing e then pressing tab twice, what do you see?
# You see all the possible commands that start with "e" when you press tab twice after entering “e”
e2freefrag edquota era_check eu-readelf
e2fsck efibootdump era_dump eu-size
e2image efibootmgr era_invalidate eu-stack
e2label efikeygen era_restore eu-strings
e2mmpstatus efisiglist esac eu-strip
e2undo efivar escputil eutp
e4crypt egrep espdiff eu-unstrip
e4defrag eject espeak-ng eval
eapol_test elfedit ether-wake evince
easy_install-2 elif ethtool evince-previewer
easy_install-2.7 else eu-addr2line evince-imageer
easy_install-3 enable eu-ar evmctl
easy_install-3.6 encguess eu-elfclassify ex
ebtables enchant-2 eu-elfcmp exec
ebtables-restore enchant-lsmod-2 eu-elfcompress exempi
ebtables-save enscript eu-elflint exit
echo env eu-findtextrel exiv2
ed envsubst eu-make-debug-archive expand
edgepaint eog eu-nm export
edid-decode eps2eps eu-objdump exportfs
editdiff eqn eu-ranlib expr
Try adding c to make ec and pressing tab again. What happens?
# The command autocompletes after adding the “c” to the “e”
echo
Try to copy/paste your echo command “echo ‘My first command’”
# Note that ctrl + c to copy does not work in windows terminal or mobaxterm - instead highlighting text automatically copies it
# In mobaxterm ctrl + v to paste also does not work - instead right-click to paste
echo 'My first command'
Try to clear the screen, can you still paste your echo command?
# To clear the screen use ctrl + l or the command 'clear' and you can still paste the command
echo 'My first command'
Try to using your command history with ctrl+r, to re-run your last command. How many letters of echo do you need to type to find your previous command?
# To search through your command history, type ctrl + r, typing e is sufficient to find your last command, press enter to re-run the command
Press ctrl + r again, then imagine you wanted to search by history instead of ctrl+r, thus try to interrupt your command. What happens?
# After typing ctrl + r, type ctrl + c to interrupt that command, then type history and you can copy/paste your command from the history to re-run
# Then either double-click on each word in your command and copy/paste or instead triple-click on the line of words to copy/paste and then remove the line number to re-run the command
What will happen to your command history when you log out of the server and re-connect?
# Check your history
history
# Exit from the server and close the window
exit
# Re-connect to the server and check your history
ssh <your eth username>@cousteau.ethz.ch
history
Run your first command with a comment describing what this command does. What happens to the comment?
# Run your first command with a comment describing what it does after #
echo 'My first command' #This command prints 'My first command' on the command line
# Notice that your comment after the # is not displayed in your result