Useful commands

The cluster does not have a graphical interface and therefore you will need to interact with it through the terminal.Therefore on this page you will find a set of useful commands, mainly aimed at people who don’t have all that much experience with the Linux Terminal. (All key combinations and descriptions are given for Ubuntu 18.04)

Tab-complete

Remember that Tab-complete works almost everywhere. So if you want to change to a child directory you can start typing the name and press Tab to auto-complete the name. If there is not a single option that would fulfill the start you have typed, press Tab a second time to show a list of all available options.

When you open up the terminal, it will always be located at some point in your file system. Usually it will start in your home directory (Ctrl + Alt + T, to open a terminal), but you can also open one from the graphical file-browser at some other location. To do so right click and select “Open in Terminal”.
cd

Change the directory. You can either give a full path or simply a series of child-directories.

Use: cd /path/to/folder

ls

View what child-directories and files there are in the current working directory. On some operating systems they might be color-coded as well.

Use: ls

  • To see hidden folders type ls -a

  • To view the size of the files in the current directory type ls -sh

pwd

Shows the absolute path to your current working directory.

Use: pwd

find

Prints a list of files matching your input. If you type a full file name it will either show that file-name or an error message stating that the file was not found.

It is particularly useful when combined with the *-operator for pattern matching.

Use: find <file_name>

  • If you want to find out if a file of name <file_name> is in the current or any of its child directories (at some level) type: find . -name <file_name>

  • If you only know some part of the file-name and want to view all files that contain this part, type find *<file_name_part>*, where you replace <file_name_part> by the part you know.

du

Print the size of the directory you give as an argument.

Use: du -sh <directory_name>

Interact with files

Sometimes you will need to interact with files through the command line. Be it moving them, viewing their content, creating them or deleting. These are the main commands you will be needing for these tasks.
mv

Move or rename files.

Use: mv <old_name/location> <new_name/location>

cp

Copy files from <old_location> to <new_location>.

Use: cp <old_location> <new_location>

rm

Delete a file. named <file_name>. Be careful with this, especially when using it in combination with the *-operator or with the -r flag!

Use: rm <file_name>

cat

View the content of a file by displaying it on the Terminal. It is meant for test-files only, so you might get weird results when trying to view for instance an image.

Use: cat <file_name>

touch

Create an empty file, named <file_name>

Use: touch <file_name>

mkdir

Create a directory called <dir_name>

Use: mkdir <dir_name>

grep

Grep is a very powerful command, but put simply searches for string-parts in a document. One of the more useful ways of using it, is directly piping the output of some command to it.

Example: Find <text> in a text-file called <file> cat <file>

grep <text>
*-operator

The star operator is used for pattern matching. The star operator can be viewed as a joker, that can be replaced by any string, even an empty one. It is useful for auto-completion or accessing multiple files that have similar names.

Example: cat *.txt will print out any text-file using the file extension .txt to your terminal.

System

This section lists a whole lot of commands that can be useful when trying monitoring your system resources, processes and interacting with the latter.

man

View the manual page of any command that comes with a manual page. (Most commands do have one) On this page you will be shown the usage and all the options the command provides.

Use: man <command>

top

Shows a list of running processes and used system resources. To quit it, press q or Ctrl + C. Especially useful for finding Process-IDs.

Use: top

htop

Shows a colorful and dynamic almost graphical) interface of the top command. It has a more readable layout and allows to kill processes by selecting them. Also allows sorting the processes by different metrics.

Use: htop

kill

Sends a stop-flag to the process given by the process-ID. You can choose between different flags. The graceful way (i.e. the one to try first) is to write

kill -6 <process-ID>

It allows the killed program to handle the exit and to some last minute store.

If this does not work, one can also use the more aggressive flag

kill -9 <process-ID>

free

Displays the amount of system memory in use and how much is still free. To display it in a human readable way (i.e. in GB or MB rather than Bytes), use the -h flag.

Use: free -h

watch

Executes the following command every 2s and updates the screen. This command is useful to monitor the behavior of some statistics.

One can also decrease the time between updates by using the -n flag.

Use: watch -n <time_between_updates> <command>

Example: watch -n 0.2 free -h (displays the free memory every 0.2 seconds)

nvidia-smi

If a Nvidia graphics card is installed this command shows important information about the current status of the graphics card.

Use: nvidia-smi

Run programs in the background

screen

Provides an environment, in which you can emulate a console. You can safely detach from this environment and it will keep being executed. You can than reconnect at any point and it will show you the contents of the terminal at that point in time.

It is especially useful when working on the cluster, as you can have your virtual environment running in one screen and detach from this screen and disconnect from the server, without the process being killed. (If you don’t run your script through slurm)

One downside of this environment is however, that you cannot easily scroll within it.

Use:

  • screen -S <screen_name> to create a screen named <screen_name>

  • screen -r <screen_name> reattach to a screen called <screen_name>

  • Ctrl + A → D, detach from a screen

  • Ctrl + D, terminate screen

  • screen -ls list all screens running on this node

&-operator Execute a command in the background. This will have your terminal run the command without occupying the screen. (i.e. you can still interact with the terminal as normal)

See also

QuickStartGuide · List of installed software · Setup a virtual environment for Python · slurm

-- MarlinSchFer - 29 Jun 2019
Topic revision: r1 - 29 Jun 2019, MarlinSchFer
This site is powered by FoswikiCopyright © by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding Foswiki? Send feedback