Unix/Solaris Introduction | ||
Managing File and Directory | Goto Week Two Tutorial→ | Goto Week Three Tutorial→ |
Working with Shell | ||
File Archive and Remote transfer | ||
Searching and Process Manipulation |
1. UNIX/Solaris Introduction.
UNIX was originally developed in 1969 by AT&T Bell laboratories. It was modified and made distributable by University of California at Berkeley in the Berkeley Software Distribution (BSD) UNIX. Sun's SunOS is also originated from BSD UNIX. Later on AT&T, BSD, and other OS Systems incorporated and developed SVR4 UNIX. SVR4 is the basis of current Sun's SunSolaris.
UNIX comes with three major components: Kernel, Shell, Directory.
Kernel is the core of the any UNIX/SunOS system. It manages physical resources of the computer including CPU, devices, processes, file
systems, and memory.
Shell is an interface which gives user an access to interact between user and kernel. It is a command interpreter which accepts commands
that user enters, interprets those commands and sends them to kernal and it is executed by the kernel and output is dispalyed on the
standard output.
UNIX including SunSolaris support three major type of shells: Bourne Shell, Korn Shell, and C Shell. The Command-Line Interface (CLI) syntax comprises of:
command option(s) arguement(s)UNIX commands are basically come with the single word such as ls, pwd, date, who, uname so on. Option(s) gives more precise result from the command such as:
ls -lwhere -l options will give list of files and directories with detail informations such as file permission, owner, file size, file names so on.
An argument in the UNIX command will allow to filter or define precise result you want to get such as
ls -l <filename>this will allow user to search exact file name he/she wants to display. The CLI allows to enter multiple commands in a signle line by using semicollon seperator such as
ls -ltr <filename>; pwd;Shell executes command from left to right.
Few basic commands used in UNIX/SunSolaris.
Command Names | Description |
---|---|
pwd | Determines current working directory |
ls | Lists files and directories |
ls -la | Lists hidden and detail informaion of the files and directories |
ls -ld | Lists directory without its contents |
ls -R | Lists the content of direcory(s) recursively |
ls -F | Lists the file types |
file <filename> | Lists the file and directory types |
cd <directoryname> | Changes directory |
cd .. | Moving back to parent directory |
cd ../.. | Moving back to multiple level parent directory |
cd <directoryname> | Relative Path |
cd /<directoryname> | Absolute Path |
cd | Return to Home directory |
cd ~ | Return Home directory |
cd / | Retrun to root directory |
cat <filename> | Displays file contents |
more <filename> | View files one page/screen at a time |
head <filename> | Displays first 10 line of a file |
tail <filename> | Displays last 10 lines of a file |
wc -w <filename> | Counts words of a file |
lp <filename> | Submit print request |
lpstat -option <printer> | Displays the status of print request |
lp <filename> | Submit print request |
lp <filename> | Submit print request |
←Return Menu
2. Managing File and Directory.
Copy, move, and rename files and directories
$ cp options sources target $ cp -i file1 file2 $ cp -i file1 /home/dir1 $ pwd /export/home/user1 $ cp -r dir1 dir2 $ cp -r dir1 ../dir2/dir3 $ ls ../dir2/dir3 file1 file2 file3 $ mv file1 file2 (rename file1 to file2) $ mv file1 /export/home/user1 (move file1 to home directory) $ mv file1 ~ (move file1 to current directory) $ mv dir1 dir2 (move conetent of dir1 to dir2) $ mv dir1 newdir (rename directory) $ mkdir dir1 dir2 dir3 (create multiple directories) $ mkdir dir1/sub_dir1 (create a directory/sub_directory) $ rmdir dir1 (delete an empty directory) $ rm -r dir1 (delete directory and its files) $ touch file1 file2 file3 (create file(s) ) $ rm -i file1 (delete file1) |
Symbolic link is a pointer which contains the path name to another directory or file. It is used to link files that are residing in different file
system. It helps moving file to new location and provides a easy file name rather than its original name.
$ ln -s source_file target_file $ ln -s /export/home/user1/file1 file1_link (create symbolic link file) $ ls -F file1_link (view symbolic file) $ rm file_link (delete symbolic file) |
VI Editor to create/modify files
VI Editor is command line, interactive text editor which is used to create and modify text (ASCII) files in UNIX system. VI Editor comes with three major modes:
Command Mode Edite Mode LastLine ModeCommand Mode helps perform text change, delete, copy, move operation. Importantly, it will allow to search text string by using slash "/" and question mark symbol "?". Edit Mode will allow to add/type text and save the contents. Last Line Mode will allow advanced users to work extensively. Type collon ":" to go to LastLine Mode from the command line mode.
$ vi filename (create a file in vi editor) type i or a to add text in vi editor which takes you to Edit Mode press Escape to go to Command Line :wq (save and quit the vi editor) $ view (view file in read-only mode) |
Key Sequences | Cursor Movement |
---|---|
i, a | add/append text in Edit Mode |
b, w | back one word/forward one word |
G, 1G | Last/First line of the file |
Control-F, Control-B | One page forward/backward |
-R, r | Overrites/Replaces characters |
x | delete a character |
dd | delete a line |
/String | Search string forward |
?string | Search string backward |
n, N | Search next occurence |
yy | Yanks a copy of a line |
p, P | paste abpve yanked copy under a line containing the cursor |
:wq! | Save and Quit from Edit Mode |
:q! | Quit without saving |
Shell Metacharacters and Korn Shell Variables
Korn Shell metacharacters are specific characters, usually symbols which have special meaning to the shell. Metacharacters are catagorized
in three parts:
→ Pathname Metacharacter
→ Filename Substitute metacharacter
→ Quota Metacharacter
Pathname Metacharacter simplifies the location changes within the directory structure. Pathname Metacharacter use symbols like Tilde (~) and Dash (-). Tilde "~" represents home directory. Filename substitution metacharacter allows to substitute shell metacharacter for other characters to ease to use commands. Symbols such as Asterisk (*), Question mark (?), Square braket ([]) are used as filename substitution metacharacters. Quota Metacharcter uses symbols such as single quota (' '), or double quota (" "), Back slash(/), dollar sign ($) etc. Quotation mark instructs shell to ignore or mask the all enclosed metacharacters. As we can use following commands to learn more about these Metacharacters.
$ cd ~/dir1 $ pwd $ /export/home/user/dir1 $ cd - (Navigate to previous directory) $ ls *.sh (List all files ending with .sh extension $ ls dir? (List all directory dir following one character i.e. dir1, dir2) $ ls [a-d]* (List set of files/directories starting with "a" To "d") $ (' ') (single forward quot ignore all enclosed metacharacters) $ (') (single backward quot execute and display the output) $ echo “Date: ‘date ‘” $ echo “The user pwd is: $(pwd). " $ (" ") (Ignore all except for single backward quot, backslash, and dollar sign) $ (\) (Do not interpret next character as metacharacters) $ (& (command) ) (Execute and display output of the command enclosed) $ echo $SHELL |
What is Korn Shell Variables
A temporary storage in memory is referred to a shell variable. Shell allows storage of values in variables. Variables contains information that is needed to function for other processes and it also allows to customize the shell.
Action: Command: -------- ----------------------------- Set a variable VAR=value, export VAR=value Unset variable unset VAR Display all variables set, env, export Values stored in variable echo $VAR
$ echo $SHELL $ set (Display all variables $ export (Display variables and thier values $ var=value (set variable and assign value) $ private=/export/home/private (set a variable private) $ unset var (delete the value(s) stored in the variable) $ unset private (unset the private variable) $ EDITOR, FCEDIT, HOME, LOGNAME, PATH, PS1, PS2, SHELL (default variables assigned) $ echo $EDITOR $ PS1 = “$LOGNAME@ ‘uname -n' \$PWD $ ” (Changing value of PS1 variable) $ echo $PATH $ PATH=$PATH:~ (Extend value of variable PATH) $ echo $PATH $ history -10 $ history -r (Display reverse history) |
What is File Descriptor?
File Descriptors work with the processes created by shell. File Descriptor determines where input originated and where output and error message are sent. Three major types of File Descriptors are:
File Des # File Desc Abbr Definitions --------- ------------------- --------------- 0 stdin Standard Command Input 1 stdout Standard Command Output 2 stderr Standard Command ErrorWhat is PIPE (|) command?
Pipe character is used to redirect standard output to the standard input of another command. A pipe character is inserted between two
commands. The first command writes the output to the standard output. The second command reads standard output from previous
command as standard input. Overall, use of pipe helps to connect multiple commands.
$ < (Read the input from a file) $ < <filename> $ > (Send output to a file) $ ls -ltr > filename (Redirect the list result to a file) $ command 2> filename (Redirect the Standard Error to a file) $ find /etc -type f -exec grep PASSREQ {} \; -print 2> /dev/null $ --Use of Pipe (|) Character $ command | command $ ls -ltr | wc -w $ ps -ef (list all the processes and details) |
View and modify file and directory permissions
There are three type of users in UNIX/Solaris. Owner, Group, and Other. /etc/passwd and /etc/shadow files contain user's permissions. Each type of user has three set of permissions: Read, Write, Execute. First permission set represents owner, second group and third other respectively.
Permission File Access ------------- --------------- Read (r) View permission Write (w) write/modify file/directory contents Execute (e) Execute executable files i.e. shell scriptTo modify file/directory permssion for users,
chmodcommand is used. chmod command will change permssion based on the symbolic mode and octal mode. Symbolic Mode uses combination of letters and symbols to change user's file/directory permissions. Octal Mode uses octal numbers to change the permissions of files and directories. Octal mode consists of three octal numbers from 0-7 which uses comination of numbers for the file/directory permssion.
Octal Value Permission ------------ --------------- 4 Read 2 Write 1 Execute
Octal table with combined set of permssions:
Permission Set | Octal Value | Binary Value |
---|---|---|
rwx | 7 | 111(4+2+1) |
rw- | 6 | 110(4+2+0) |
r-x | 5 | 101(4+0+1) |
r-- | 4 | 100(4+0+0) |
-wx | 3 | 011(0+2+1) |
-w- | 2 | 010(0+2+0) |
--x | 1 | 001(0+0+1) |
--- | 0 | 000(0+0+0) |
This table represents a combined octal values and set of permissions.
Octal Mode Permissions --------------- ---------------- 777 rwxrwxrwx 775 rwxrwxr-x 755 rwxr-xr-x 644 rw-r--r--
$ ls -l filename (view file and directory permission) $ -rwx------ (read, write, execute permission for owner) $ ls -n (view UIDs and GIDs) $ chmod symbolic_mode filename $ ls -l file1 $ chmod o+r file1 (Add read permission for "other" user) $ chmod o-r file1 (Remove read permission for "other" user) $ chmod octal_mode filename $ chmod 755 file1 $ ls -l file1 $ umask 027 |
How to manage jobs in Shell?
A job is a process that runs in the shell.
Command Set | Description |
---|---|
jobs | List all jobs currently running |
bg %135 | Run current job at the backgroud |
fg %135 | Pull jobs from background to foreground |
stops %135 | Stop a job running at background |
sleep 2000 & | Running sleep command at background |
alias | List all aliases currently running |
alias h=history | Create alias 'h" for history command |
alias info='uname -a;id; date' | Create alias 'info" for system info |
unalias h | Remove alias 'h" of history command |
What is Function & Options in Korn Shell?
A function is set of ogranized commands which helps perform multiple task quicker and in a efficient ways. For example:the following
function "num" creates a function that will help display total number of word counts redirected the output from first command
function num {ls -ltr | wc -w}
Option are referred to "Switches" in which controls the shell's behavior and Options are regarded as boolean value such as true/false or On/Off.
$ typeset -f (display all the current running fuctions) $ typeset +f (Display only fuction name) $ set -o (view current options) $ set -o option_name $ set -o noclobber (Turn on noclobber option) $ set +o noclobber (Turn Off noclobber option) $ ls -l > log (list and redirect the result to log file) |
What is a Shell Script?
Shell Scripts are nothing more than text files which contains UNIX-based commands and comments. This great advantage of script is an
ability to automate command sequences such that a script that can be repeatedly used while stopping or starting the system. Hash "#"
sign is used to comment text on shell to provide more information about the script. and comments begining with "#" hash sign will be
ignored by shell. Kernel reads "#!" first to indentify the program which interpret the line in the script. Shell Scripts are run line by line and
users need read and execution permission to be able to run a script
$ # this is comment (comment not read by shell) $ ls -ltr # list all the files with long list and permission (command and comments) $ chmod u+rx myScript.sh $ ./myScript.sh (Run the script) |
Create Conditional Commands if/fi
Shell Script uses variety of conditional commands while creating scripts. Most used conditional commands are:
Conditional Constructs: && and || Conditional Commands: if Conditional Commands: while Conditional Commands: case"&&" conditional construct is used to show that second command will be performed only if precceeding command is successful. For example:
mkdir /export/home/user1 && cd /export/home/user1
In this case, user will navigated to user1 directory only if first command is successfully performed. "||" double pipe is used in script to develop a logic that if first command fails then go and execute second commands such that:
mkdir /export/home/user1 || mkdir /export/home/user2
"if" command is used to evaluate first command, if passes then execute else go to next command and perform the same steps as the
previously steps.Syntax to perform if conditional commands is
if expression then command1 else command2 fi
Here is an example how we can perform if else conditional command to test if user has a read "r" permission on /etc/passwd file
$ if test -r /etc/passwd > then > echo "You have Read permission on /etc/passwd file" > else > echo "Sorry, you don't have Read permission on /etc/passwd file" > fi"while" command loops the commands until it meets its condition(s). Syntax to use while command is:
while expression do command done
4. File Archive and Remote Transfer.
Compressing & Archiving files and directories
By compressing and archiving files and directory will safeguard files and directories in an archive. It can be retrieved if they are
accidentally deleted or lost. It can be archived in removable media such as external hard drive, USB Flash drive so on. We use two popular
command to archive files and directories.
"tar" command and "jar" command. tar command archives file to and extract file from a single file called tar file. jar command combines
multiple files and in addition to that it also compresses files. The following table applies to both tar, and jar commands.
Command Set | Description |
---|---|
x | Extract file from tar |
v | Execute in verbose mode, write to the standard output |
f | Specifies the archive file |
c | Create a new tar file |
t | List the table of contents of tar file |
$ tar functions archivefile filename(s) $ tar cvf mytarfile.tar file1 file2 file3 (archive file1, file2, file3 in mytarfile.tar file) $ mt -f /dev/rmt/0 status (check system has tape device mounted) $ tar cvf /dev/rmt/0 . (archive everything on tape device from current directory) $ cp mytarfile.tar /export/home/user1/dir1 (copy mytarfile.tar to directory dir1) $ tar tf /dev/rmt/0 (View content of tar file in tape device) $ tar tf mytarfile.tar (view content of the tar files) $ tar xvf /dev/rmt/0 (retrieve/extract tar file from tape device) $ tar xvf mytarfile.tar (Extract files from mytarfile.tar) $ jar options destination filenames $ jar cvf /export/home/user1/bundle.jar * (Archive everything to specified directory) $ compress -v mytarfile.tar (compress mytarfile.tar using -v option) $ zcat mytarfile.tar.Z (View compressed file) $ uncompress -v mytarfile.tar.Z (uncompress mytarfile.tar.Z file) $ gzip file1 file2 (compress files using gzip command) $ gunzip file1.gz file2.gz (uncompress file using gunzip command) $ gzcat file1.gz (view commpressed file) $ zip file.zip file1 file2 file3 (compress file1, file2, file3 in file.zip file) $ unzip file.zip (extract from file.zip) |
Establish Remote login session
Remote login session allows user to access remote host from your current desktop allowing access to use resources in the remote computers. The ~/.rhost file provides user's access authentication for user to access the local host.
$ rlogin hostname $ uname -n (View the hostname) $ rlogin -l user1 hostname $ rlogin host3 (login to remote host to kill the corrupted session) $ pkill shell (kill the process remotely) $ telnet hostname (Type User/Passwd to remote host) $ vncserver options (Accessing X VNC window) $ rcp source_file hostname:destination_file $ rcp file1 remote_host:/tmp (copy file from localhost to remotehost) $ rcp remote_host:/tmp/file1 /export/home/user (copy file1 from remote host to local host) $ rcp -r ~/dir1 remote_host:/tmp (copy dir1 directory from local machine to remote machine) $ ftp hostname (Create ftp login session at remote_host) $ cd (Change the current directory at ftp remote session) $ lcd (Change the current directory at local system) $ mget (transfer multiple file from remote to local directory) $ mput (transfer multiple files from local to remote) $ bye (ends the ftp session) |
5. Search and Process Manipulation.
Search for content in files and directories
$ grep option pattern filename $ grep -n success /home/user1 (search all the lines that contains string "success") $ egrep '[a-m]+root' /export/home/user1 (Search string root preceeding letter a to m) $ fgrep success *.sh (search string "success" in all files whose extension is .sh) $ egrep 'failure| success' file1 (display all the lines that contains either failure or success) |
Search for files and directories
$ find pathname expression action $ find ~ -name file1 (find a file by file name on home directory) $ find ~ -name file1 -ok rm {} \; (find a file "file1" and delete it) or $ find ~ -name *.sh -exec rm {} \; (find all files with .sh ext and delete them) $ find . -name file1 (find a file "file1" on current directory) $ find . -mtime +3 (find files Not modified in last 3 days on current directory) $ find ~ -size +20 (find files that is larger than block 20) $ find . -mtime +3 (find files Not modified in last 3 days on current directory) $ find / -name file1 (find file1 starting from root (/) directory) |
View and search for a specific process
When one logs in or open an application or run command, UNIX/Solaris systems create processes. It is called a daemon and these provide services and runs on the backgroud. Each process comes with an unique Process Identification called "PID" and which is used by the kernel to manage and track processes. Also, each process includes an User ID "UID" and Group ID "GID" which reflects who owns the process(s). Processes have children processes and child process is executed first than the parent process.
$ ps option(s) $ ps -ef | more (display all the current running processes with uid, pid) $ ps -e | grep lp (search list of ps and send to grep which searches a text "lp") $ pgrep lp (search process "lp") $ ptree 135 (List process in tree hierachy structure following the child processes) $ find . -mtime +3 (find files Not modified in last 3 days on current directory) $ kill -option PIDs $ pkill -option PIDs $ kill -9 135 |
Processes respond to signal messages such that if you hit "Control+C", this will send signal 2 which means interrupt the current process. So signal are identified by signal number and thier names. The following table will display some very essential signals, their names, and associated actions:
Signal # Signal Name Definition -------- -------------- ---------------------- 1 SIGHUP Hang Up or drop terminal conection 2 SIGINT Interrupt (Control+C) 9 SIGKILL Kill processes 15 SIGTERM Terminate a process
No comments:
Post a Comment