Hints and tricks¶
This section contains some hints that might make working with Linux easier.
Tab auto-complete¶
If you press TAB on the keyboard after starting to type a command or a file, it will auto-complete if no other matches or else suggest possible matches.
Short-cuts on the CLI¶
CTRL-SOMEKEY refers to pressing down the CTRL key and then another key while continuing to hold down CTRL.
Examples:
- CTRL-a: Go to the beginning of the line
- CTRL-e: Go to the end of the line
- CTRL-l: Clear the terminal
- TAB: Auto-complete (i.e. start write a command or file name and then press TAB to auto-complete as far as possible
- ARROW-UP: Pressing the arrow-up key repeatedly will let you cycle through recent commands
- CTRL-r: you will get a prompt to write text to search in the list of recent commands. The list is saved in
.bash_historyin your $HOME. On some systems it might be called.bash.history
Alias¶
You will often have to write the same command again and again. If it is a longer command, it is reducing your productivity having to repeat it. Then you can use the alias command to create an ‘alias’ for your command.
To see the currently definted aliases, execute the ‘alias’ command:
Example
This is how it might look when you run alias:
As an example, this means that if you type ‘dir’ the actual command that is executed is ‘ls -lAF’.
In order to create a new alias, you could write:
Warning
The alias will only be valid in that shell, and only until you logout. Next time you will have to issue the ‘alias’ command again, unless you add it to either your .bashrc or .bash.profile file.
Adding a new alias to the .bashrc file, using ‘nano’ editor
-
Open the file:
nano ~/.bashrc -
Inside the editor, scroll down to where your aliases are. If you do not have any, just add them at the end, like this
#My custom aliases alias c="clear" alias ll="ls -alF" # Colourize ls output alias ls='ls --color=auto' # Colourize grep output alias grep='grep --color=auto' # Easily list my SLURM batch jobs alias jobs='squeue -u $USER' # Find all entries starting with d in the output from the ls -lahrt command alias ldir=’ls -lahrt | egrep "^d"’ - Save and Exit the file:
CTRL-x(Press CTRL and hold it down while pressing x). Answer ‘Y’ to save. - Next time you start a shell or after a new login your new alias is available. To make it available immediately, run
Finding help¶
You can often get more info on the usage of a Linux command. This includes options and flags. Depending on the setup of your system either or both of the following should succeed:
COMMAND --helpman COMMAND
where COMMAND is the Linux command you want information about, like ls, mkdir, gcc, etc.
Misc¶
- Type
clearto clear the terminal - Type
historyto see a list of the recent commands used in the terminal- You can change the number of saved commands by setting the environment variable HISTSIZE in your
.bashrcfile in your home directory. - Example: Open
.bashrcwithnano. Somewhere (at the end for instance) add:export HISTSIZE=NUMBERwhereNUMBERis the number of commands to save, for instance 500.
- You can change the number of saved commands by setting the environment variable HISTSIZE in your
man PROGRAMwill give you the manual for a specific program or command, if it exists- Example:
man gccwill give open manual/help for the compilergcc, containing flags to the compiler etc. Note that you need to first load a module that has gcc in if you are on Kebnekaise.
- Example:
Warning
Passwords in the history file
If you enter a password (e.g. login password for a computer or website) at the command prompt and hit return, the password will be included into the .bash_history file once you log off. Neither the password nor the history file are encrypted in any way. In particular when using a shared system such as offered by NAISS, it is best to reset the password on the computer or website where you have been using it, to a new value.