McCarty Group Wiki
Posts (Latest 10 updated) :
Read all
3 July 2019

Introduction to UNIX

The most important UNIX commands needed for computational research

About this tutorial

As a computational chemist, you should become acquainted with the basics of the Linux operating system. There is a wealth of free information about Linux available online and in several books. This tutorial is a starting point to help you begin using Linux and to understand the common commands useful to scientific computing.

man (manual)

The easiest way to get more information on a particular Linux command or program is to use the man command followed by the item you want information on:

$ man [program-name]

ls (list)

$ ls

list all files within a directory. Using the -l modifier gives more information such as read/write permissions. The -a option includes entries starting with “.” The command can be combined with search strings in order to produce partial listings. For example

$ ls test*.com

will list all files starting with the string “test” and ending with “.com”.

cd (change directory)

$ cd [path/name]

change to the directory called “name”; when given without any arguments, the shell moves to the user’s home directory. In order to change to a specific directory, the full pathname must be given e.g. cd /usr/local/bin changes to the directory /usr/local/bin. Moving up one directory can be done more easily by using

$ cd ..

To display the full path of the current directory type

$ pwd

mkdir (make directory)

$ mkdir [name]

creates directory “name” as a subdirectory of the current working directory.

cp (copy a file)

$ cp [file1] [file2]

copies “file1” to a new file called “file2”

mv (moving a file)

$ mv [file1] [file2]

changes the name of “file1” to “file2”

rm (deleting a file)

$ rm [file]

removes file “file1”. To remove a directory with all of its contents use the flag -r for recursive

$ rm -r [directory]

Caution: deleting a file with rm cannot be undone. Make sure you have backups!

find

 $ find -iname [file]
 

finds file file in the current working directory as well as all of its subdirectories. The command

$ find -iname "*400"

will thus locate all files whose names end with the string 400.

grep

$ grep string1 file2

search for string1 in file file2. For example, the command

$ grep "B3LYP" test*.log

will search for string B3LYP in all files starting with test and ending with .log. Please observe that UNIX is case sensitive (e.g. “B3LYP” is different from “b3lyp”). If the string occurs repeatedly in one file, all occurrences are listed. Searching through all files of a given file system can be achieved with the -r modifier. The command

$ grep -r /scr6/user99 -e "573442"

searches for number sequence 573442 in all files located in file system /scr6/user99. For searching binary files, use grep -a.

top

$ top

dynamic real-time view of the processes running on the system. Each task has a unique PID (process ID). To exit hit control c

pwd (print working directory)

$ pwd

prints the name of the current working directory.

Input and Output (I/O)

To save the output (stdout) from a program to a file use the redirection operator >

 $ example_program > my_output.txt
 

Input can be given to a command from a file instead of typing it in the shell by using the redirection operator <

 $ my_command < programinput
 

Alternatively, you can use the “pipe” operator:

$ cat programinput | mycommand

Using the pipe operator, you can link commands together. The pipe will link stdout from one command to stdin of another command. In the above example we use the cat command to print the file to the screen (stdout), and then we redirect that printing to the command mycommand.

ssh (secure shell)

SSH, or Secure Shell, is a remote protocol that allows users to access remote servers over the Internet. This allows you to remotely access a machine and execute shell commands in the same manner as you would if you were physically operating the remote computer.

$ ssh {user}@{host}

The SSH key command instructs your system that you want to open an encrypted Secure Shell Connection. {user} represents the account you want to access. You will be prompted to enter the password for the requested account. Note: When you are typing your password, nothing will appear on the screen, but your password is, in fact being transmitted.

To display graphics (for example from plotting programs) you need to enables trusted X11 forwarding with the -Y option

$ ssh -Y {user}@{host}