Linux Programming Unit1
Linux Programming Unit1
What is LINUX?
An Operating System (OS) Mostly coded in C Machine independence It provides a number of facilities:
management of hardware resources directory and file system loading / execution / suspension of programs
History (Brief)
1969
1980s
1970s
Bell Labs makes UNIX freeware Berkeley UNIX (BSD) Bill Joy vi editor, C Shell
System V release 4 TCP/IP Sun Microsystems Solaris Microsoft Xenix, SCO MIT X-Windows GNU, LINUX Stallman, Torvalds
1990s
multi-tasking / multi-user lots of software networking capability graphical (with command line) easy to program portable (PCs, mainframes, super-computers)
continued
free! (LINUX, FreeBSD, GNU) Popular not tied to one company Wider choice active community
Next
Your Account
Each user has their own space called their account. Type your login ID and password to enter your account. Only if the login ID and password match will you be let in.
The UNIX prompt (or similar). You can now enter commands.
or
^D
or
exit
On-line Help
man
man gnuchess man man
apropos topic
apropos game apropos help
Hardware
Kernel Compilers
Next
The Shell
The UNIX user interface is called the shell. The shell does 4 jobs repeatedly:
display prompt
read command
the shell
process command
execute command
Typing Commands
Try these:
date cal 3 2005 who ls -a man cal clear
Date Commands
Calculators
expr e
expr 3 + 5 + 7
Simple arithmetic
bc
Programmable Calculator
Pipes : sending the output of one program to the input of the other
ls | sort who | sort
The File
Ordinary files
Ordinary files can contain text, data, or program information. Files cannot contain other files or directories. Unlike other operating systems, UNIX filenames are not broken into a name part and an extension part (although extensions are still frequently used as a means to classify files). Instead they can contain any keyboard character except for '/' and be up to 256 characters long (note however that characters such as *,?,# and & have special meaning in most shells and should not therefore be used in filenames). Putting spaces in filenames also makes them difficult to manipulate - rather use the underscore '_'.
Directory Files
Directories are containers or folders that hold files, and other directories.
Device Files
To provide applications with easy access to hardware devices, UNIX allows them to be used in much the same way as ordinary files. There are two types of devices in UNIX - blockoriented devices which transfer data in blocks (e.g. hard disks) and character-oriented devices that transfer data on a byte-by-byte basis (e.g. modems and dumb terminals).
etc ...
usr1 faculty
dev ...
tmp
...
mj
System Directories
/ root directory /home Home directory for all users /bin and /usr/bin all commands /sbin and /usr/sbin admin commands /etc configuration & system data files (e.g. /etc/passwd) /dev files representing I/O devices (they dont occupy space on disk) /lib and /usr/lib Library files in binary form. /usr/include Standard Header files used by C Program /tmp allows users to create temporary files. /var Variable part of file system
Pathnames
A pathname is a sequence of directory names (separated by /s) which identifies the location of a directory. There are two sorts of pathnames
Absolute Pathnames
The sequence of directory names between the top of the tree (the root) and the directory of interest. For example:
/bin /etc/terminfo /export/user/home/ad /export/user/home/s3910120/proj1
Relative Pathnames
The sequence of directory names below the directory where you are now to the directory of interest. If you are interested in the directory proj1:
if you are in s3910120 s3910120/proj1 if you are in home home/s3910120/proj1 if you are in user
proj1
pwd
/ . ..
The root directory The current working directory The parent directory (of your current directory)
Examples
cd / cd ~ cd cd ../..
Change to root directory Change to home directory (Special case; means cd ~) Go up two levels.
Use cd
cat file
cd /etc cat passwd
List file
ls
ls ls /etc
Directory listing
List current dir. List /etc
Usually, you can only create directories (or delete or rename them) in your home directory or directories below it.
mkdir rmdir mv
Permissions
ls l /etc/passwd
-rw-r--r-1 root root 2365 Jul 28 16:19 /etc/passwd
directory
chmod
-w, +w .
-- listing order numerals, UPPERCASE, lowercase -- displays multicolumnar output -- in o/p * indicates executables and /refers to directories -- lists (.) (dot) files -- output in long format -----recursive list sorts filenames in reverse order one filename in each line lists only dirname if dirname is a directory displays inode number
Copying a file - cp
$ cp file1 file2 $ cp file1 /data/official
letter1
- file1 is copies into /data/official
$ cp file1 /data/official/letter1 - file1 is copied to /data/official as $ cp /home/mca01/p1.c p1.c -- p1 is copied as p1.c in current
directory (destination is a file)
$ cp /home/mca01/p1.c . -- destination is a directory $ cp file1 file2 file3 off_data -- copying multiple files,
here the last filename must be a directory and must be an existing directory.
cp options
$ cp i file1 file2 -- interactive copying $ cp R off_data official - copies directory structure.
Here off_data and official are directories. If official directory does not exists then cp creates it. If official directory exists then off_data becomes a subdirectory under official.
Deleting files : rm
$ rm file1 file2
the
$ rm /data/file1 /data/chap2
$ rm /data/file[12]
one.
$ rm I file1
before deletion. $ rm r * or rm R * -- recursively deletes the directories and subdirectories. $ rm rf * --- the f option forceably removes the files/directories
Renaming Files : mv
mv filename1 filename2 --- moves a file (i.e. gives it a different name, or moves it into a different directory) mv chap01 unit01 mv chap01 chap02 chap03 notes Here notes is a directory
ls l complete description
File Type & Permissions, Links, Ownership, Group Ownership, File Size, Last Modification Time
Process utilities
Disk utilities
df :- Report how much free disk space is available for each file system separately. It shows the information like
Directory where file system is attached. Device name of the file system. Number of 512-byte blocks available. Number of files we can create. Options t (Total), -k (Reports size in KB), -h (Reports size in MB, GB)
du :- Disk usage :- To find the consumption of a specific directory tree rather than entire file system.
Networking Commands
Information on Others
users who ps
ps -au
Who else is logged on? Information on current users What are people doing?
w
w -sh
Fingering People
finger
finger -l
finger user
finger ad
finger @machine-name
finger @catsix finger @ratree.psu.ac.th
ping machine-name
ping catsix
chfn
finger also prints the contents of the .plan and .project files in your home
Talking
talk user
mesg n
mesg y
Sending E-mail
Send mail :
mail Add
Subject: Shoe Problem What colour are my shoes? I cannot see them at the moment because of my desk. - Jim ^D
The vi Editor
Two modes
Append a Replace character r, Replace word R .. Deleting character x, Deleting line dd Exit
Goto command mode press :wq
Filters
The UNIX programs that read some input, perform a simple transformation on it and write some output.
grep, egrep, fgrep tr, dd, sort Sed, awk programmable filters
grep
Some option
-c Counting number of occurrences -n Line numbers along with lines grep Mamata e mamata database grep [Mm]amata database
Character sets
grep : RE
c \c
^ $ . []
Any non-special character c matches Turn off any special meaning of character c Beginning of line End of line Any single character Any one of character in ; ranges like a-z are legal
^r pattern beginning with expression r ^[^r] pattern not beginning with expression r r$ pattern ending with expression r
[^]
r* r1r2
Any single character not in ; ranges are legal Zero or more occurrences of r RE r1 followed by RE r2
fgrep
Searches for multiple patterns Does not accept regular expression Multiple patterns are separated by new line character. The disadvantage of grep family is that none of them has a separate facility to identify fields.
sort
-f : eliminates distinction between uppercase and lowercase letters. -n : numeric comparison - r : largest to smallest +m : comparison skips first m fields +0 : beginning of the line -u : discard duplicates
comm
Lines that occur only in file 1 Lines that occur only in file 2 Lines that occur in both Comm 12 f1 f2
tr
tr a-z A-N
Assignment
1. Try all the UNIX commands. Store the output in a file appropriately using redirection operators. 2. Read a word from the terminal and check if the spelling is correct. Suggest few alternatives. 3. Create a file using Vi. Store few names in the file. Search all the names containing the letter M or m. 4. Create another file using cat command 5. Compare both the files to find the differences 6. Use calculator commands to compute 5 arithmetic expressions.