Skip to content

Linux Command-Line Basics

When you ssh into one of the login nodes, you will receive a shell prompt, a Command Line Interface (CLI). (Specifically, this will be a "bash" shell unless you changed your default shell). You must use commands you type in to navigate the filesystems and manipulate files. The following descriptions are only a quick-start to the Linux CLI capabilities.


Tutorial recommendations

If you are new to Linux, you should find a comprehensive Linux tutorial. A few recommendation:


Commands

All of the following commands have manual (man) pages. You can access them by simply typing man <command> or see Linux man pages online. Take some time to learn how to navigate and quickly find the information from the "man pages".

Directory navigation and files

  • pwd (print working directory) Get the absolute directory name of your current location.
  • ls (list directory) List the content of current or specified location.
    Useful options: ls -al, ls -altrF
  • cd (change directory) Go to the home directory or to the specified location.
    Useful option: cd - (go back to the previous directory)
  • mkdir (make directory) Create a new directory.
    Useful option: mkdir -p dir_one/dir_two creates the parent directory as well.
  • rmdir (remove directory) Remove empty directory.
  • cp (copy) Copy file or directory.
    Useful options: cp -p (preserve mode, ownership, timestamp), cp -R (copy recursively)
  • mv (move/rename) Move or rename a file/directory.
  • cat (catenate) Display content of the specified file(s) on the screen.
  • less (catenate one screen at a time) Display the content and allows to scrolling and search.
    Useful less commands: <space bar> (move forward one page), b (move back one page), /<chars> (search for <chars> forward), ?<chars> (search for <chars> backwards), q (quit from less)
  • rm (remove) Remove files and directories. Be careful with this command. (Reminder, 'scratch' and 'SHELL' filesystems on the cluster are not backed up, so if you accidentally remove a file or a directory, there is no way for the systems staff to restore it.)
    Useful options: rm -i(prompt first), rm -r (recursively, including directories), rm -rf (force and recursively; use caution with this)

Pipes and redirects

  • | (pipe) Send standard output (stdout - FD 1) of left side command to the standard input (stdin - FD 0) of right side command.
  • some_script 1>file.log 2>&1
    Send STDOUT (FD 1) to file.log, send STDERR (FD 2) to STDOUT. Net effect is to log both output and errors into a single file.
  • some_script 2>&1 | tee -a some_file
    Send STDERR to STDOUT, pipe STDOUT to the tee command which sends its output to STDOUT and appends to some_file. This is useful to see output/errors on the display and log to a file at the same time.
  • some_command > /dev/null 2>&1
    Send both STDOUT and STDERR to the bit bucket, ie: throw away all output/errors.

Controlling processes

  • ps (process show) Show my process IDs.
    Useful option: ps -ef (show all processes)
  • kill Send a signal to process (see man page for details). Useful options: kill 1234 (terminate process ID 1234)
  • pkill Send SIGTERM to process specified.
    Useful option: pkill -u $USER (kill all your own processes- this will also log you out)

Text Editors

  • nano - Small and effective for quick edits. Use Control-G inside the app to get a quick help screen or see the documentation
  • vi or vim - A powerful and complex editor with a bit of a learning curve. See the vi/vim documentation.
  • emacs - An editor with everything but the kitchen sink. See the GNU Emacs documentation

Bash shell

Your login shell, by default, is bash. Note that you'd use "bash" both as your login shell (to run operating system commands interactively) and as a scripting shell, a non-login shell, to submit your batch jobs. Depending on how it is invoked, bash can read multiple config files from your home directory.

If you need to change your default login shell, please open a ticket and tell us the alternative shell you prefer.

bash dot files

  • ~/.profile -- executed automatically for all login shells.
  • ~/.bash_profile -- similar to .profile, but only for bash login shells.
  • ~/.bashrc -- executed for non-login shells (when you run bash on a command line or start a script with #!/bin/bash).
  • ~/.bash_logout -- executed upon logging out of a bash login shell.

Note that your ~/.profile will read-in your ~/.bashrc file so login shells also get what you add to your bash config file.

We recommend you add aliases and other bash configuration items to the ~/.bashrc file, like:

Example

# Example aliases; keep these if you like the results
alias ls='ls -F --color=auto'
alias ll='ls -altr'
alias l='less -Xi'

# My env variable
export MYSPECIALDIR=~/scratch/special

We highly recommend you DO NOT add to your ~/.bashrc:

  • module load commands -- Instead, add module load commands to your submit bash script.
  • Conda init scripts -- Instead, source the .../etc/profile.d/conda.sh file and activate your conda environment only for scripts where it is needed.

Previous: Connecting to the Cluster Next: Slurm Command Basics