Short introduction to UNIX

This resource is an introduction to those who have no experience with using the command line, terminal on MacOS or UNIX interface (shell). I provide a reader with basic commands to browse direcotries, to manipulate files, and to run programs.

Working with files and folders in UNIX

cd /dir1/dir2: change directory to dir2, located in dir 1

cd ~: change to home directory

First symbol / in the beginning of the directory name /dir1/dir2 means the root directory, and that dir1 is in the root directory. The abscence of / (example: dir1/dir2) would mean that we refer to a directory dir1 within a current directory that we are working in. Starting a directory name with ../ (example: ../dir1/dir2) means that we refer to a directory dir1 in the lower level directory, that also contains our working directory.

Type less /location/filename just to view the file "filename". Just press q to quit the preview.

ls: list all files in the current directory. We can also add options using a dash symbol:

mkdir directory_name: make directory;

rm -rf directory_name: removes directory with all of its contents.

rm -r *: removes everything from the current directory

rm file_name: removes file;

pwd: show address of current directory;

ctrl+A when typing in the command line: go to the beginning of the line you are typing in the command line

head --lines=-100 file.txt >> file_new.txt: delete last 100 lines in a file

How to copy and move files

cp location1/file1 location2/file2: copies file;

cp location1/* location2: copes contents of location1 folder into location2 (copy folder);

wget http://...../file.extension: to download a file from a remote website

Sometimes there are too many files in folder, and it doesn’t work. This is the solution:


find /folder1/folder2/ -name "*" -exec cp {} /home/boris.goncharov/folder3/ \; -print

In the above command I copy all files from /folder2/ to empty folder /folder3/.

mv location1/name1 location2/name2: moves a file/folder.

Create a shortcut: ln -s ~/mydirectory. It creates shortcut to "mydirectory" in a current directory. This command is useful for websites. Usually for the website user it is only possible to view directories inside the "public_html" folder with website content, and it is not possible to view other folders on the server. But if we make a shortcut to these directories in "public_html", website users will be able to access them.

File editor (vim)

To open a text file type in the command line vim filename, then press the button i to start editing. To exit editing mode press the button esc. In the not editing mode one can press : or / to enter different commands, following by pressing Enterbutton:

Stream file editor (sed)

Stream file editor allows to edit many files with just one command!

Use: sed -i ’s/name1/name2/‘ ~/path2/path2/*

To replace phrase name1 to name2 in every file in directory ~/path2/path2/* 

It is also possible to take a file, find and replace some of its contents, and save it by a new name, using this command:


sed -e 's/replace_this/to_this/' -e 's/replace_that/to_that/' old_name.txt > new_name.txt

File search

How to find a file:

Running programs in a command line

matlab name.m: runs matlab program.

python name.py: runs python program.

top: show what is currently running on the server;

ps -u boris.goncharov: show what processes are run by the user boris.goncharov;

which program_name: shows where a program lives

Running a program in the background

To run: nohup /path/to/command-name-with-arguments &

To see list of programs running in the background: jobs -l

To stop a program in the background:

  1. To find a process ID: ps -ef |grep nohup
  2. kill -9 process_ID

Fork bombs

Sometimes an attempt to parallelize a code using all available cores on a big cluster can cause a kind of fork bomb. After that all commands or login attempts will be slow and followed-up with system messages:

Re-logging in doesn't help, and "jobs -l" doesn't show the processes that cause a problem. What helped me to break out of this is the following command:
while true; do killall -u boris.goncharov; done

Recent Work