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_history in 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:

$ alias

Example

This is how it might look when you run alias:

b-an01 [~]$ alias
alias cdn='cat >/dev/null'
alias dir='ls -lAF'
alias l='dir'
alias ls='ls -F'

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:

$ alias shortName="your custom command here"

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

  1. Open the file: nano ~/.bashrc

  2. 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"’
    

  3. Save and Exit the file: CTRL-x (Press CTRL and hold it down while pressing x). Answer ‘Y’ to save.
  4. Next time you start a shell or after a new login your new alias is available. To make it available immediately, run
    $ source ~/.bashrc
    

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 --help
  • man COMMAND

where COMMAND is the Linux command you want information about, like ls, mkdir, gcc, etc.

Misc

  • Type clear to clear the terminal
  • Type history to 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 .bashrc file in your home directory.
    • Example: Open .bashrc with nano. Somewhere (at the end for instance) add: export HISTSIZE=NUMBER where NUMBER is the number of commands to save, for instance 500.
  • man PROGRAM will give you the manual for a specific program or command, if it exists
    • Example: man gcc will give open manual/help for the compiler gcc, containing flags to the compiler etc. Note that you need to first load a module that has gcc in if you are on Kebnekaise.

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.