fundamentals on unix programming provides the user with a basic understanding of unix program commands, command syntax and how to use some of the many thousands of program commands that are available in each standard of the unix manual page's.
for advanced unix scripting please click on the following link: http://www.papuwip.00author.com/
1. what is a shell?
all operating systems have a shell. a shell is a program that is used to start other programs.
2. which shell?
there are a variety of shells to chose from, although which one you use is usually dictated by the system administrator.
s.r. bourne developed the bourne shell and it is the original and basic shell, it's filename sh.
david korn developed the bourne compatible korn shell, it's filename ksh. the bash shell is a super set of the bourne shell and is called the bourne-again shell, it's filename is bash. and then there's zsh ect.
you write shell scripts by using a shell script interpreter.
in dos you create batch files using the command.com interpreter.
bourne compatible means scripts share the same programming language. most unix system administration and web-back end processing is done using cgi via the bourne shell interpreter.
non-bourne compatible shell scripts such as the c-shell, filename csh, are closely related to c programming. the c-shell has a compatible shell script whose is filename tcsh. c-shell compatible scripting differ in their implementation, but can be used interchangeably with other shell programming languages.
3. logging in?
a user must identify themselves to the computer with a user name and password.
once logged in, a shell is automatically started to allow the user to run programs. the username and password are usually created by the system administrator. and root has full system privileges.
4. basic commands?
to use text based commands, type the command in, on the command line and press enter. the enter key may have an arrow on it and may also be labeled the return key.
uname gives details about the machine and version of unix used on the machine you are logged in on.
uname -a gives more details, such as the ip address of the computer you are logged into on.
ps gives information on the shell that you are using, the pid process id, tty terminal, time identifies the amount of time used by a process, and cmd the commands that are running.
cal displays the calendar for the month or year that you request.
passwd allows you to change your pass word.
5. command syntax?
every command is entered into unix as follows: programname [options] then [arguments].
the program name is the name of the program you wish to run.
options are optional letter(s) prefixed with a space and a hyphen "-" following the letter.
arguments are separated by space, and are case sensitive, and the argument can be a filename or directory.
6. getting help?
help is a reference tool and is available in the manual pages or man pages on every standard of unix.
man page is used as follows: type man and the program name, press enter, and information and options along with option usage on that program are displayed.
the whatis program command is used to determine what a program does and which program is best to use.
7. logging out?
their are two ways to log out of a bourne shell. you may logout by typing the exit command or by typing ctrl-d.
in a text based shell, once the shell exits you are presented with a login screen. on the other hand, while in a gui graphical user interface, logging out closes the shell window but you remain logged into the shell.
1. data in unix?
data is stored in files, which are organized hierarchically into directories, and these directories are called folders in the windows operating system.
one analogy of directories "/" would be that they are similar to branches, while files are similar to pages of leaves as in .dot[something].
branches are directories, and are indicated and separated by a forward slash "/". and the forward slash leads back to the beginning root directory "/" when the destination file is read along its path from right to left: /usr/tom/documents/reports/sales.txt.
2. working with files and directories?
to see what is available use the ls, program command and the file's or directory's name to display file details and the contents of the files and or directories.
ls -F differentiates the type of files from directories. there are device files link files and data files. and in unix systems, there are 6 file types, regular files, directories, special device files, named pipes, symbolic links, and sockets.
regular file
this is the most common type of a file in unix. a plain collection of bytes with arbitrary data.
directory
this is a special type of a file in unix, which only contains a list of other files, otherwise known as the contents of a directory. you don’t work with directories directly, instead you manage them with standard commands provided with your os.
example:
$ ls -ld *
-rw-r--r-- 1 greys greys 1024 Mar 29 06:31 text
drwxr-xr-x--- 2 greys greys 4096 Aug 21 11:00 mydir
special device file
literally, almost every device has a special file associated with it. this type of file allows access to various devices known to your system.
depending on the way of accessing each device, its special device file can be either a character shown as “c” in the ls output, or a block shown as “b” device. one device can have more than one device file associated, and it’s perfectly normal to have both character and block device files for the same device.
example:
$ ls -al /dev/loop0 /dev/ttys0
brw-r----- 1 root disk 7, 0 Sep 7 05:03 /dev/loop0
crw-rw-rw- 1 root tty 3, 48 Sep 7 05:04 /dev/ttys0
named pipe
pipes represent one of the simpler forms of unix inner process communication. their purpose is to connect i/o of two unix processes accessing the pipe. one of the processes uses this pipe for output of data, while another process uses the very same named pipe file for input.
example:
$ ls -al /dev/xconsole
prw-r----- 1 root adm 0 Sep 25 08:58 /dev/xconsole
symbolic link
this is yet another file type in unix, used for referencing some other file of the filesystem. a symbolic link contains a text form of the path to the file that it references. to an end user, symlink, short for symbolic link, will appear to have its own name, but when you try reading or writing data to this file, it will instead reference these operations to the file it points to.
example:
$ ls -al hosts
lrwxrwxrwx 1 greys www-data 10 Sep 25 09:06 hosts ->
socket
a unix socket is also sometimes called ipc socket, inter-process-communication socket, and is a special file which allows for advanced inter-process communication. in essence, a socket is a stream of data, that is very similar to a network stream and network sockets, but all the transactions in ipc sockets are local to the filesystem.
example:
$ ls -al /dev/log
srw-rw-rw- 1 root root 0 Sep 7 05:04 /dev/log
ls -l long listing, gives the total blocks being used by the files and directories listed, the file and directory permissions, link count, owner and group of the file, file size in bites, date and time that the file was last modified, and the name of the file or directory.
ls -C arranges the files and directories alphabetically in columns.
ls -r displays the content of every directory.
3. change directory?
cd [directoryname] change directory you are in, to go to the specified directory.
cd [noarguments] change directory without specifying a directory name will take you to your home directory.
pwd print working directory program, prints on the screen, where you are located within the directory system that you are working in.
4. filenames and file types?
filenames can contain a-z A-Z 0-9 . , @ - _ + = :
filenames should avoid space ~ ` ! # $ % ^ & * ? ( ) ' " [ ] { } ; < > \ |
it is not possible to use the directory branch or forward slash "/" in a filename.
unix does not have file association, therefore you cannot open a file simply by typing its name. you must use a program to open a file in unix.
a period "." extension can be placed anywhere within a filename.
the file program command is used to guess a file's contents by matching those contents with its content table descriptions.
5. wildcards?
wildcards make it possible to match groups of files without knowing the complete file or directory names.
* asterisk matches any character or group of characters in a filename(s).
example:
*.txt # any file that ends in dot txt.
mark.* # any file that begins with mark dot.
a*b # any file that begins with an a and ends with a b.
and *.* # any file with a dot in it.
6. ? question mark matches any single character.
example:
c?t # match all files with a single character surrounded by c and t.
fred.???? # match all files that begin with fred dot and end with four characters.
and ????x? # match all files that begin with four characters, has an x in
# it and ends with a character.
[ ] square brackets match any single character within the brackets.
example: c[aou]t [a-zA-Z]*.txt [!d]*.
# match any files that have cat, cot, cut and any letters from a through z to capital letters A
# through Z and any characters before the dot txt but dose not include a d, and that has any other
# characters after the d.
the ! exclamation mark indicates do not match the character that it is prefixed to.
7. displaying file contents?
in a file containing text the cat filename(s) program command displays the files contents on screen.
the more filename(s) program command displays the same type of information as cat does, but the output is displayed page at a time by pressing the space bar. by pressing the enter key once each time, a single line at a time is displayed. and you use q to quit and drop back out to the shell prompt.
head filename(s) program command displays only the first 10 lines of the file(s).
tail filename(s) program command displays only the last 10 lines of the file(s).
8. comparing files?
the diff program command compares two text files and outputs a complete list of the differences.
in the diff output:
c identifies on screen the line changes
a the added lines
and d the deleted lines
the cmp program command compares two binary files and outputs the location of their character difference on screen.
9. copy, moving and renaming files?
the cp copy program command is used in two ways. cp copies file1 to file2 and if file2 already exists it is overwritten. and cp can be used to copy multiple files into a directory.
example:
cp file1 [ . . . filen] to a dir1.
the mv move program command moves or renames the original file to a new file in two ways. mv moves file1 to file2 and if file2 already exists it is overwritten. and mv can be used to move or rename multiple files in a directory.
example:
mv file1 [ . . . filen] to a dir1.
10. deleting files?
the rm remove program command removes a file, but if the file is "write-protected" you will be asked for a y/n yes no confirmation. you cannot remove files that are owned by other users unless you are given their permission.
rm -i option asks for y/n yes no confirmation before deleting each file.
rm -f forces without asking for any confirmation before deleting files, and does not give any error messages.
rm -r recursively removes files, directories and subdirectories, directory trees, branches ect.
11. hidden files?
a hidden file is a file whose filename begins with a dot "." and the dot file is not displayed when you do an ls. dot files are not matched by wildcards either. hidden files tend to be set and forget files, or configuration files like the .profile file or .exrc file.
the ls -a option may be used to list the hidden dot files.
12. "." dot and ".." dot dot are hidden entities?
there are two hidden file entities "." dot and ".." dot dot, these are aliases. the dot "." is shorthand for the current file. dot dot ".." is shorthand for the parent directory. directories are like the branches that we call directories that contain other directories.
13. relative vs absolute paths?
an absolute path is the full destination filename path, and it begins with a forward slash "/".
the filename path in the current directory that you are in, is a relative filename path and you can tell that its a relative path because it begins with a filename.
14. working with directories?
the mkdir [newdir] make directory program command is used to make directories, although the name you chose to call it is up to you.
the rmdir dirname remove directory name program command is used to delete directories. you can rmdir dirname from the parent directory, but the directory must be empty before it can be removed. or you can remove directories recursively with rm -r option dirname and this will get rid of the directory and its contents.
15. finding files?
find program command is used to find from the top-directory [criteria and actions to perform].
example:
find where /usr/tom -name report.txt -print.
# finds the report.txt file within the /usr/tom directory and prints it.
when using wildcards with -name criteria enclose the arguments in single quotes ' as follows: find . -name '*.txt' -print # finds within the current directory any .txt and print it.
criteria to find [file] can consist of the file owner, file permissions, i-node number, file date, file size.
actions to find [file] can consist of find . -type f -exec rm -i { } \;
# find file type and execute an -i interactive remove.
16. archiving files?
archiving allows you to bundle files together and move them to another location on the hard disk, or back them up onto removable media like a tape, or send them electronically to another machine.
an archive is a single file or image on a tape that contains all the information about the files and directories, including the file names, ownership, modification dates and contents.
the tar program command can write to offline media and provides compression.
the cpio program command can write to offline media, but does not provide compression.
the gzip program command can not write to offline media but does provide compression.
the compress program command provides compression.
example: find . -name "*.txt" | cpio -o > /dev/tape0
# find any .txt file and copy its output to device tape 0.
example: cpio -i < /dev/fd0 # copy input from device floppy disk 0.
1. security?
unix is a "multi-user" via an operating system that allows users and groups to use it, and that's why you need security.
more than one user may interact with and log onto the system at any given moment. security allows each user to have a separate set of access privileges for the system resources.
unix, allows simultaneous users, and users can have separate access privileges.
windows 2000/xp, allows simultaneous users and has separate access privileges.
windows nt, does not allow simultaneous users but does have separate access privileges.
windows 95/98, does not allow simultaneous users and does not have separate access privileges.
a user is a system id that allows each user that logs in, to identify themselves to the system for resource-access purposes.
when a group of users require similar access to a resource, each user in that collection may be made a member of a group, and the group is given access to the resource.
2. security related program commands?
who am i displays login information including user name.
id displays current user's id and number and group information for the specified user.
su username switch user name temporarily switches and runs a shell as another user. a "-" before the username allows a full simulation of the other user. and once you are done you type the exit program command to return to your shell.
newgroup groupname program command is used to switch your current group to a different group.
3. file protection overview?
the security related information of owner, user, and other permissions are stored against each file/directory in a unix file system.
the three sets of permissions are the access privileges of the file owner, file group, and everyone else such as the world wide web, and the owner can change any of the privileges.
the ls -l command lists the three sets of permissions in the three fields of the file and or directory for the file owner, file group, and everyone else. the sets of file permissions for each field can be set to combinations of either r read (numeric value=4), w write (numeric value=2) and x execute (numeric value=1)
4. permissions?
r is the read permission, its numeric value=4, allows you to view the contents of a directory.
w is the write permission, its numeric value=2, allows you to create or delete the contents of a file in a directory.
x is the execute permission, its numeric value=1, allows you to access the contents of a file in a directory, such as programs.
it is the root "/" user who has full access privileges and never sees the words permission denied.
5. changing file permissions?
chmod permissions filename(s) program command is used in either symbolic or numeric mode to add + subtract - or set = the letter or numeric permission to any of the field. the fields are u owner, g group, o other or world wide web, and a all.
symbolic mode example:
chmod u+w filename
# change permission mode for the u owner with the addition of w write permission or multiple permissions.
example:
chmod uo+w, u-rx filename
# change the permission mode for the u owner and o other with the addition of w write permission, and
# change the permission mode for the u owner by subtracting the r read and x execute permission.
in numeric mode the three digit the three numeric values change the file permissions r read=4, w write=2, and x execute=1 in the three fields for the u owner, g group and o other.
example:
chmod 640 filename # gives an ls -l value of rw-r-----.
example: chmod 070 filename # gives an ls -l value of ---rwx---.
common permission settings are:
for data files
chmod 444, gives an ls -l r--r--r--,
chmod 644, gives an ls -l rw-r--r--,
chmod 666, gives an ls -l rw-rw-rw-.
for programs:
chmod 750, gives an ls -l rwxr-x---,
chmod 755, gives an ls -l rwxr-xr-x,
chmod 775, gives an ls -l rwxrwxr-x.
for directories:
chmod 755, gives an ls -l rwxr-xr-x,
chmod 775, gives an ls -l rwxrwxr-x,
chmod 777, gives an ls -l rwxrwxrwx.
6. changing file ownership?
using chown makeowner filename you may change the ownership of a file. a file can be located in a directory, but have nothing to do with the owner of that directory. you can only change permissions on files that you own, so change the ownership of files that you own last. once you change ownership you cannot change it back.
7. changing group ownership?
using chgrp groupname filename(s) program command to change the group ownership of a file.
if you are a member of a group and get a permission denied message when accessing that group, use the newgrp groupname filename(s) program command to switch your group.
8. a dangerous security loophole?
if a directory has full permissions you can cp filename1 to filename2 where as the owner of filename2 you can modify filename2.
then rm filename1 which can be done because it is writable.
then you can mv filename2 to where filename1 used to be, and the new filename1 will be owned by you.
then you can change the necessary ownership and permissions to look like the original filename1, and the only difference will be the i-node number. so the moral of this story is pay attention to directory permissions.
1. combining programs - pipes and filters?
most unix commands produce output that usually ends up on the screen but can be redirected to a file or another program.
to redirect output to a file, the output ">" character is used.
an example is:
ls -l > filename # redirects output to a file.
example:
who | wc # redirects output to another program.
who lists who is currently logged in on the sever and pipes "|" that information to wc word count, which counts the lines, words, and characters.
in a pipe | one program must create output that must be used as input to the other program.
3. standard input?
if at some point after pressing enter when running a program that program requires you to type anything on the keyboard then that program requires input. input "<"can come from a file or another program.
example:
wc < filename # wc receives input from the filename.
4. standard input and output?
the command line utilities that come with unix adhere to the philosophy that complex tasks can be performed by combining simple programs. this is called the roll you own approach.
example:
who | grep root | wc -l | lp
# who pipes "|" output to grep for how many times root is logged in and "|" pipes that output to wc for
# the word count of lines -l and "|" pipes that output to lp the line printer.
5. about filters?
a filter is a command line utility that takes standard input, performs some processing on the data it reads, and produces output based upon that input.
example:
wc is a filter whose process is counting words, lines and characters.
in comparison ls is not a filter cause ls takes no input.
a filter is used to process the data produced by other programs from the data in files.
6. common filters?
cat does not process data, instead it takes input and echoes it back out onto the screen.
more paginates data out to the screen one page at a time.
grep is used for the removal of lines that do not contain certain requested text.
sort sorts the information alphabetically, but it does not modify the file.
tee takes its input duplicates it, and writes it to two places.
example: who | tee wholist | wc -l
# who | pipes output to tee which sends its input as output to the wholist file and also | pipes output to
# wc which lists the lines -l of who is logged in on the server.
sed is a basic editor.
example: who | sed "s:pts/: terminal: "
# displays who is logged on the server and sed substitutes pts/ with the word terminal.
example: who | sed /may/d
# displays who is logged on the server and sed d removes all instances of may.
awk can process anything and supports its own scripting language.
7. searching for text in files?
grep textpattern filename(s) is used to search for text in standard input, and grep uses its own wildcards. grep's set of wildcards are consistent with all programs in unix that use regular expressions, like vi, ed, ex editors and sed programs. in grep use ' the single quotes around the object you are searching for.
use grep with find to locate files in all directories.
example: find . -name '*.txt' -exec grep 'hello' { } \;
# find searches . for -name of any .txt files and executes greps for { } the instances of the word hello.
8. standard error?
each program produces two types of output, standard output and standard error, and standard error is where you would typically find error messages.
to redirect standard error messages to a separate file you use "2>". standard output can be redirected to a regular file by using the "1>" but this is typically assumed by default.
example:
ls -l files xxxx > okmessages 2>
ls -l files sends its output to xxxx > okmessages and 2> sends its output to error messages file which are two separate outputs. one output is the regular files output sent > to okmessages and the xxxx gives error messages 2> its output which are separate output files.
it is possible to redirect all output to the same place.
example:
ls -l files xxxx > allmessages 2>&1
# 2>&1 indicates that the error messages are sent to the same place as the regular files.
ls -l files sends output to xxxx sends output > to allmessages 2>&1 where all messages are output in the allmessages file.
any form of output may be redirected to /dev/null if it is not wanted at all. /dev/null may be thought of as a black hole that data goes into and simply disappears.
example:
find / -name abc 2> /dev/null
# find in the root / directory the name abc and everything else that's sent to the screen goes to /dev/null
1. process control?
when a program is running it is a process, when it is not running it is no longer a process.
every process is assigned a unique process id, usually in the range from 1-30000. a process being run by the current logged in user can be examined by using the ps command.
ps -u [username] option displays all users for the given user, pid process id, tty terminal number, time the amount of time used up by the process, and cmd the command being processed.
ps -f or ps -l option displays more details, or a longer listing of details for the running process.
ps -e option displays all processes running on the system.
2. about processes?
ps -f gives the uid user id, pid process id, ppid parent process id, c scheduling information, stime is the time the process was started, tty terminal number, time the amount of time used up by the process, and cmd the command being processed with the command line options that are used.
ps -l option gives the f, s, uid user id number, pid process id, ppid parent process id, c scheduling information, pri priority, ni, addr, sz, wchan, tty terminal number, time the amount of time used up by the process, and cmd the command being processed.
multiple processes are run in a process stack.
3. running programs asynchronously?
asynchronously running programs means to run programs in the back ground. the process does not wait for the process to finish running before presenting another prompt and allowing you to start further processes.
to run a process asynchronously you append "&" an ampersand to the end of the command line. commands that take standard input from the keyboard cannot be run in the background because you cannot type information into a command that is running in the background.
commands that produce standard output or standard error can be run in the background, but its a good idea to redirect their output to a file or /dev/null, or that output will splash all over the screen while you are working. and finally asynchronous programs terminate when you log out, but we will remedy this later.
4. killing processes?
any running process may be stopped by using the kill process id. once the process terminates it gives a terminated message. some processes can corrupt data when they are killed, so try to avoid kill, when possible try ctrl-d.
kill will only succeed if you have permission to kill a process. some processes are programmed to ignore attempts to kill them. if the process refuses to die then use the kill -9 with the processes id. kill -9 process aborts a process and terminates it without warning.
5. jobs?
to pause a process use ctrl-z. to resume the most recently stopped process use fg foreground. use the jobs program to list and selectively resume stopped processes with the job number prefixed at the stopped process, as in fg 3.
an asynchronous program cannot be stopped because it is running in the background.
6. control over your process?
each process is assigned a priority by the operating system and the priority determines the amount of cpu time the process is given. the nice -priority command [arguments] is used to adjust the process priority. a higher priority will result in a process completing sooner at the expense of other processes.
priorities are numbers that range from --20 the highest priority to -19 the lowest priority, where -10 is the default priority. nice is used as follows.
example:
nice --20 find / -name outputfile # at a priority of --20 find within / root
# the outputfile file.
to prevent a process from terminating when you log out you use nohup the command [arguments] and append & an ampersand to the end of the program command.
example:
nohup backup /home &
# run uninterrupted back up for the home directory in the background. any standard output produced by the
# process will be written to a nohup.out file unless it is redirected elsewhere.
7. scheduling commands?
you can use the at program command to schedule commands to run at a particular time.
example:
$ at 2315 # at 11:15 change directory to home/charles tar backup to
# floppy disk 0 remove rf* and ctrl-d to quit.
> cd /home/charles
> tar cf /dev/fd0
> rm rf *
> ctrl-d
$
any output is emailed to the user.
1. vi a unix visual text editor?
a text editor does not contain formatting information, it just contains text. vi is used to edit text files such as html documents, configuration files, and source code like c, c++, ect. vi evolved from line based editing programs ed and ex, and shares many of the same editing commands.
vi was designed for keyboards that do not contain special characters and for terminals that do not use a mouse.
vi has two modes, command mode is where each key typed represents an editing command. and insert mode is where each key typed with the exception of esc, represents the text that you wish to insert into the document.
how this works is when you start vi you start in command mode and must enter a command to be taken to insert mode. insert commands such as a, A, i, I, o, O, s, S, c, C, ect, allows you to start typing. and when you are done you press esc, and are taken back to command mode, and when you are done you quit vi.
2. manipulating files?
vi can be started with an empty canvas by typing vi at the command line, or with a document to edit by typing vi and the filename(s).
3. basic editing?
in command mode you type a to get in insert mode and append text. enter some text, when you are done type esc to return to command mode.
:w write save the file only if a filename has been specified.
:w filename to write to or save as filename.
:q allows you to quit but only if you have not made any changes.
:wq allows you to save then quit.
:x or ZZ allows you to save the file if changes have been made, and then quits.
:q! allows you to abandon any changes and quit.
4. more vi command options?
:w! allows you to save changes you made to a read only file that you own.
:e filename allows you to open another file if no changes have been made to the file you are in.
:e! filename allows you to open the specified filename and abandon any changes to the file you are in.
:e# or ^6 to open the last file that you edited.
:n or :n! to open the next filename specified on the command line.
:rew allows you to go back to the first file of the filenames specified on the command line.
:f or ^g displays the current filename details, number of lines, percentage of the way through, and what number file you are in.
5. moving around?
if you are not sure that you are in command mode just press esc, and it will take you into command mode, and if you are in command mode it will do nothing at all.
space or the l key will move ahead one character, and can be prefixed with a number.
backspace or the h key will move you back one space, and can be prefixed with a number.
$ will move you to the last character on the line.
^ or 0 will move you to the first character on the line.
fcharacter will find and move you to the next instance of that character.
; is used to move to the next instance of the character assigned by the fcharacter.
enter j or + will move you down the next line.
k or - will move you up the next line.
^f to move forward one page.
^b to move back one page.
^d to go down half a page.
^u to go up half a page.
G means to go to the last line in the file.
1G means to go to the first line in the file.
numberG means to go to the line number appended to G.
6. other move commands?
/pattern command allows you to move to the next occurrence of the pattern.
n (N) command is used to move to the next instance of the pattern assigned by the /pattern.
w (b) command to move forward one word, and b to move backward one word.
% command allows you to find the matching parenthesis ( ) bracket [ ] and or brace { }.
] ] ([ [) double right bracket, double left bracket command allows you to move to the next or previous c function, or the beginning of the file and the end of the file.
malphabet command is used to bookmark a line with an alphabetical label.
'alphabet single quote proceeded by a letter command allows you to jump to the similarly alphabetically labeled bookmark.
7. basic editing commands?
press esc to return to command mode. and you are ready to enter some vi commands.
a (i) append text before (insert) text after cursor.
A (I) append text to the end and insert text at the (beginning) of the line.
o (O) open a new line after and open a new line (before) the current line.
s substitute the current character with text, and if prefixed with a number it is the number of letters that are substituted.
cw substitutes a word with text, and if prefixed with a number it is the number of words that are substituted.
c$ substitutes a line with text.
cfspecifiedcharacter finds and substitutes all the characters up to and including the specified character.
8. commands that don't take you into insert mode?
these commands allow you to modify the text and characters within the vi document that you are working on.
x (X) cuts the character underneath (before) the cursor, and if prefixed with a number it is the number of text that are cut.
dd means to cut the current line, and if prefixed with a number it is the number of lines that are cut.
dw cuts a word, and if prefixed with a number it is the number of words that are cut.
d$ cuts a line.
dfspecifiedcharacter finds and cuts all the characters up to and including the specified character.
p pastes the recently cut text, and if prefixed with a number it is the number of copies of that text that are pasted.
yy copies the current line.
yw copies a word, and if prefixed with a number it is the number of words that are copied.
y$ copies a line.
yfspecifiedcharacter finds and copies all the characters up to and including the specified character.
r is used to replace text with a character, and if prefixed with a number it is the number of letters that are replaced.
9. useful commands?
u is used to undo the last command.
U is used to restore the current line to the state it was before you began working on it, as long as you have not left that line.
. repeats the last edit that you made.
10. advanced editing?
>> (<<) right shift (left shift) the current line, and if prefixed with a number it is the number of lines that are shifted.
> and G, right shift G (left < and G shift) from the current line to the end of the file.
>'label (<'label) right shift single quote (left shift single quote) shifts to an arbitrarily labeled bookmark.
J joins the next line to the current line.
^l redraws the screen, this is necessary to do when a program running in the background spills data onto the screen that you are working on.
:m, ns/ abc/ xyz/g colon : m (from mark), n (to number) s substitutes the occurrences of / abc with / xyz/g from lines m to n. use 1, $ to make substitutions from line 1 to all lines, and the use of . dot is to make substitutions on the current line.
11. more useful commands?
:!command is used to run a shell command from within vi.
when referencing the current filename on the vi command line use %, and to reference an alternate file from those already open use #.
use :!! on the vi command line to repeat the last command. and use :sh on the vi command line to obtain a temporary shell, and type exit to return to vi.
12. configuring vi?
in vi use :set to list the current options set for vi, and :set all to list all your available options for vi.
one set of options is on/off, and another set of options is called value options, which are assignment option=number.
ai auto indent on/off noai does not cause new lines to inherit the indentation of the previous line.
ic ignore case on/off noic prevents searches from being case sensitive.
nu numbers on/off nonu does not display line numbers.
sw shift width value is assigned the number of spaces to shift with the right shift >> and left shift <<.
ts tab stop value is assigned the number of spaces to use when displaying the tab.
13. preferred vi options?
if you want your option to be in effect every time you start vi, create and put them in a file called exrc in your home directory, and make sure that every line of commands in that file starts with set.
1. the unix file system?
a unix file directory file simply contains a list of filenames and i-node numbers.
an i-node number is simply a reference to an i-node in an i-node table. in windows it is called a file allocation table fat or fat 32.
2. how files are stored?
conceptually files are arranged in directories and subdirectories.
an i-node is an information node. and the information you'll find in an i-node is the file size, creation, access and modification times, owner and group, permissions, file type (file, directory, device, etc.), link count, and starting block count. you use ls -i to display i-node numbers for any file.
the actual data for the file is stored in blocks, within the block table.
3. understanding links?
it is possible for a file to have more than one name in one directory, where each name including the directory is called a link. each link to a file share the same i-node, meaning that every link share the same permissions, etc. if there is more than one link, there is no concept of an original and duplicate link. therefore you may vi the one filename, to access and modify the data all other filenames that are linked to the filename.
rm simply unlinks the link that you specify, and all other links remain intact. in other words when all links to a file have been unlinked then the data that, that file contained is deleted, or the block is marked as available.
ls -l is used to examine how many links there are to a given file.
4. examining hard links?
if two files are referring to the same data, then they share the same i-node number.
if you are unaware of where the given links to a file are you can use find to search for the i-node number.
example:
find directory -inum 12345. # find, finds in the directory the specified i-node number.
5. hard linking files?
the ln filename to linkname [directory] program is used to create links to existing files.
ln is used identical to cp copy, except the file is not duplicated. instead a reference link to the file is created.
6. symbolic links?
to create a symbolic link to a file means that you are creating a separate file that contains a reference name to the original filename, whether or not the original file exists. with symbolic links you have the original file and a file that contains a link to the original file.
microsoft windows does not support hard links instead microsoft windows has a symbolic link called a short cut. the icon with the arrow tab in the corner is a symbolic link called a short cut.
to create a symbolic link use ln -s
when using the ls -l, the symbolic links are indicated at the beginning of the permissions settings by an l file type.
7. creating symbolic links?
ln -s filename to linkname is used to create a symbolic link file called linkname.
ls -l indicates a file type with linkname -> pointing to the filename, where you may vi the linkname shortcut, to access the original filename.
8. unix file types?
ls -l list file types.
the d file type is a directory or container for files.
the l file type is a symbolic link to another file.
the c file type is a character device or hardware and serial port.
the b file type is a block device or hardware for data memory like a floppy disk.
the p file type is the fifo or pipe for the communications file.
9. mounting?
microsoft dos and windows use partitions like a: c: and k: to designate each accessible file system, and a file system is a hierarchical structure of directories and files.
in unix all files that a user can access are found in a single directory tree "/" called root. this is done by mounting each available file system onto a subdirectory of the root file system. these mounted file systems can include, other hard disks, floppy disks, and network disks.
each filesystem is typically mounted on an empty directory called mnt. but anyone with root permission can mount the filesystems into mnt, or you can mount the filesystem onto other directories.
10. using mount?
mount program command displays the mounted directories.
before mounting a filesystem, you may want to determine the disk space on a filesystem by using the df (disk free) program command. you can also use the du program command (disk usage) to display the file usage and directory usage being taken up by the mounted filesystems in kilo blocks, but the size of each block can vary according to the bites in each unix device.
1. communication?
their are two programs that you can use to communicate with other programs, one is called telnet and the other is called mail.
telnet is a program used to obtain a login prompt on another unix computer. the computer that you are connecting to must be available to the local computer that you are using by a tcp/ip network including the internet.
in telnet once your type in the telnet command and receive the telnet> prompt you type in the open command and the name or ip address of the computer that you want to connect to, and use the quit command to quit the session.
2. using mail?
mail is a rudimentary unix email program and is used in two modes.
mail username [@emailaddress] command is used to send email.
example:
charles[@home.com]
after typing the email address you press enter, and it requests a subject. after you press enter again, it waits for you to type your message. when the message is done type ctrl-d and it will ask to cc a carbon copy. make your choice if any and when you press enter you get your shell prompt back, which means the email was properly sent.
to read your email you simply type the mail command and press enter, and get your mail prompt & type ? question mark to view the help commands. type h and it will give you the header numbers. type the number you wish to select if any, press enter and that email opens for you to read. once you quit the messages will be sent to your local inbox which means you will not be able to access those emails from any other program. to leave the email untouched type x to exit the mail program.
11. customizing your shell environment?
usually the system administrator changes the login shell. to change the shell that starts when you login, use the usermod command program, which is pretty much self explanatory.
1. edit the /etc/passwd file?
you can also edit the /etc/passwd file by opening it, locating the username you want and modifying the last part where the shell is identified.
alternatively you can edit the .profile file in your home directory by adding the line exec [newshell].
example:
exec /bin/csh # tells the unix shell to execute the c-shell program.
2. environment variables?
a shell environment variable is a setting that can be viewed and adjusted, each variable is of the form of variablename=value.
if you want to see the value of a variable, you type echo $variablename, and it returns the contents of that variable.
the set program command is used to show all environment variables that have been set, a shell name variable cannot have spaces in them. if an assigned value name has a space in it, it must be enclosed with double quotes.
3. what environment variables do?
many programs including the shell itself use shell environment variables as configuration options. and every program has its own particular set of environment variables that it looks at to determine the current configuration and those environment variables can usually be found at the end of every man-page.
variables that are to be used as configuration options should be exported, by the export variablename command.
lines, and columns variable specify the dimensions of the screen for full screen programs.
example:
echo $LINES $COLUMNS # 24 lines 79 columns.
HOME variable is the name of your home directory, and is the location where most config files tend to get created.
LOGNAME identifies the name for the user currently logged in.
MAIL environment variable fully specifies the directory that contains the mailbox. and when the shell periodically checks in that mailbox to see if there are any new mail.
MAILCHECK environment variable checks to see how many seconds should pass before it checks for new mail.
SHELL should be set to the actual shell that you are going to use to obtain a shell session.
example:
SHELL=vi # assigning the shell vi that SHELL is going to use for shell sessions.
and SHELL=telnet
TERM is use to determine the type of terminal, keystrokes and other terminal based options.
4. your path?
PATH is an environment variable used by the shell to determine where to look for executable programs.
when you type in an executable program command, the shell looks in the directories in the current path to determine the command that you typed. and if it does not find a command there, it gives a warning message, "command not found."
the shell environment variable path is interpreted as a list of directory names separated by ":".
the user can add their own directories to the list or even replace the entire directory list. to place a directory at the end of a path variable you type PATH=$PATH:/home/charles/bin or to place your current directory at the beginning of path you type PATH=.:$PATH
5. modify PATH?
you modify the path variable by assignment PATH=/bin:/usr/bin so that programs will run. the three directories that you will usually find are /bin:/usr/bin:/usr/local/bin, usr/local/bin are the executable programs developed by your company. you may run programs from your home directory by assigning the home directory to the beginning of the directory that you are currently in PATH=/home/charles/bin:$PATH.
6. your prompt?
PS1 is the environment variable command used to change your prompt.
ksh and bash shell offer dynamic prompts which are prompts that change with time and can include, the working directory, the current date and time, the current machine name, the current user, the command history, and the results of any program.
example:
PS1=\'date\' : # gives the current date and time.
example:
PS1="\$PWD> " # gives the present working directory in the prompt.
example:
PS1="\'who | wc -l\': " # gives the number of persons currently logged in
# to the server in the prompt.
7. your .profile?
because changing your path, your prompt, or any other shell variable will only have an effect during that shell session. as soon as you log out those environment variable changes that you made will be lost.
in a bourne compatible shell the dot .profile file is a file containing commands that are executed every time the user logs in. to make the changes you made to your environment variable run every time you log in put those changes in the .profile file.
system wide profile in /etc/profile file is where all the options that are run automatically are stored. the .profile is to logging into unix what autoexec.bat is to booting in dos. .profile is a shell script, therefore it is possible to use the current directory . .profile dot profile to run changes without having to log out and log in again.
8. command line editing?
a command line history is a shell feature that allows a user to use the arrow key on the keyboard to re-enter commands without having to retype them.
command line editing allows the shell feature to allow a user to use the arrow key on the keyboard to go over the current command or any other command and modify that command before enter is pressed. however, if you use sh you do not have command line editing history available.
9. ksh shell command line editing?
in ksh shell command line editing you have command line editing and history available by way of the vi commands.
to access the command mode in ksh you press esc, and to move around through the lines you use the h, j, k, l vi commands. to enter a command you press enter. but first you must set ksh editor to vi.
example:
ksh> EDITOR=vi. # assigning vi to the editor in the ksh.
10. other shell options?
umask is used to specify the permissions for the file created by the current shell. in umask you type in the permissions for the files that are going to be created later by the shell. except in umask you use the numeric mode to specify which permissions are going to be turned off.
in a umask of 022 all subsequent files will have a permission of 666 - 022 = 644 or rw-r--r--. note that in umask the x execute permission of 1 cannot be specified.
11. other shell customization options?
suppose you want a specific behavior to occur when the user logs in. a particular program is automatically started such as the accounts package or some third party software, and when that program exits, the user is presented immediately with another login prompt?
to cause this behavior to occur you add exec [programname] to the end of the dot .profile file. but because this program will be the only program that you will have running, you will have to use a different login name to adjust your .profile programs.
as an alternative to achieve the same result you can add the programname and exit on the end of the dot .profile program line.
12. the end?