1 of 27

The UNIX SYSTEM

Gurmeet Singh

PG Department of Computer Science & IT

HMV, Jalandhar

2 of 27

Unix Tools

  • Shells
  • Useful Commands
  • Pipes & Redirects

3 of 27

What the ~*&?!

~ “tilde” indicates your home directory: /home/hmv

* “star”: wildcard, matches anything

? wildcard, matches any one character

! History substitution, do not use

& run a job in the background, or redirect errors

#% special characters for most crystallography programs

`\([“’ back-quote, backslash, etc. special to shell

_ underscore, use this instead of spaces!!!

4 of 27

Shells

  • sh, csh, ksh, tcsh, bash
  • Recommend tcsh or bash for interactive use.
  • both have command completion
  • simple command line editing
  • simple to use history facilities.

  • Change logon shell using chsh

5 of 27

Intro to Unix: Files

  • Filesystem is a single tree
  • Filenames case sensititive
  • Physical devices can be mounted anywhere

/

tmp

dev

etc

home

usr

chris

lib

mary

include

bin

local

lib

include

bin

6 of 27

Some basic commands

the bash shell has automatic completion, just press <TAB>

• completion is used for command names and for file names

• pressing <tab> twice gives you all options

7 of 27

Essential Commands

  • cd - change directory - cd
  • mkdir - make a directory - md
  • cp - copy a file - copy
  • ls - list files - dir
  • rm - remove a file - del
  • mv - move a file - move & ren
  • grep - expression searching
  • top - cpu and memory usage
  • who/w - who else is logged in
  • man - read documentation

8 of 27

Where am I?

It prints the name of the “current working directory”

This is the default directory/folder where the shell program will look first for programs, files, etc. It is “where you are” in Unix space.

pwd

9 of 27

What is a directory?

Directories are places where you put files. They are represented as words connected by the “/” character.

On Windows, they use a “\”, just to be different.

DO NOT PUT SPACES

In directory/file names!

/home/yourname/whatever

10 of 27

What have we here?

List contents of the current working directory

ls –l - long listing, with dates, owners, etc.

ls –lrt - above, but sorted by time

ls –lrt /home/yourname/something

- long-list a different directory

ls

11 of 27

Go somewhere else?

Change the current working directory

cd /tmp/yourname/

- go to your temporary directory

cd - - go back to where you just were

cd - no arguments, go back “home”

“home” is where your login starts

cd

12 of 27

A new beginning…

Create a new directory.

mkdir ./something - make it

cd ./something - go there

ls - check its is empty

mkdir

13 of 27

How do I get help?

Display the manual for a given program

man ls - see manual for the “ls” command

man tcsh - learn about the C shell

man bash - learn about that other shell

man man - read the manual for the manual

to return to the command prompt, type “q

man

14 of 27

Move it!

Move or rename a file. If you think about it, these are the same thing.

mv abc.txt bettername.txt

- change name

mv abc/file.txt ../betterplace/file.txt

- same name, different directory

mv abc_*.img bettername_*.img

mv

15 of 27

Copy machine

Copy a file. This is just like “mv” except it does not delete the original.

cp abc.txt bettername.txt

- change name, keep original

rm abc.txt

- now this is the same as “mv

cp

16 of 27

“Permission denied” !?

Change the “permission” of a file.

chmod a+r filename.txt

- make it so everyone can read it

chmod u+rwx filename.txt

- make it you can read/write/execute it

chmod –R u+rw /some/random/place

- make it so you can read/write everything under

a directory

chmod

17 of 27

Destroy File

Remove a file forever. There is no “trash” or “undelete” in unix.

rm unwanted_file.txt

- delete file with that name

rm –f /tmp/yourname/*

- forcefully remove everything in your temporary directory.

Will not prompt for confirmation!

rm

18 of 27

Displaying Pagewise file

Display the contents of a text file, page by page

more filename.txt - display contents

less filename.txt - many installs now have a replacement for “more” called “less” which has nicer search features.

to return to the command prompt, type “q

more

19 of 27

After the download…

File compression and decompression

gunzip ~/Downloads/whatever.tar.gz

- decompress

gzip ~/Downloads/whatever.tar

- compress, creates file with .gz extension

gunzip

20 of 27

Where the %$#& is it?

Search through directories, find files

find ./ -name ’important*.txt’

- look at everything under current working directory with name starting with “important” and ending in “.txt

find / -name ’important*.txt’

- will always find it, but take a very long time!

find

21 of 27

Disk space?

Check how much space is left on disks

df - look at space left on all disks

df . - look at space left in the current working directory

du –sk . | sort –g

- add up space taken up by all files and subdirectories, list biggest hog last

df du

22 of 27

Processes?

Look for programs that may be eating up CPU or memory.

top - list processes in order of CPU usage

jobs - list jobs running in background of current terminal

ps –fHu yourname

- list jobs belonging to your account in order of what spawned what

ps top

23 of 27

Stopping Running Processes

Stop jobs that are running in the background

kill %1 - kill job [1], as listed in “jobs

kill 1234 - kill job listed as 1234 by “ps” or “top

kill -9 1234 - that was not a suggestion!

kill -9 -g 1234 – seriously kill that job and the program that launched it

kill

24 of 27

Using find and grep with wildcards

we can use “wildcard” characters to make searches more general

• “*” is the main one, means any set of characters

– find /home/gurmeet -name “*.ppt” : finds all powerpoint files

– grep human *.txt : look for the word “human” in all the files in my directory.

25 of 27

Pipes & redirects

  • Pipes are used to pass the output from one Unix command as the input to another Unix command.

ls | grep “hmv”

  • Redirects are used to pass the output of a Unix command into a file.

ls > directoryname

26 of 27

Text Editors

  • Important tools for using Unix
  • Two main editors
    • emacs
    • vi
  • Great features in both:
    • Syntax highlighting
    • Brace matching
    • Sophisticated text manipulation/movement
    • Scriptable
    • Many more features …

27 of 27

Any Questions?