Unix Practical File
Unix Practical File
Unix Practical File
SUBMITTED TO :
SUBMITTED BY:
The interface to the kernel is a layer of software called the system calls. Libraries of common functions are built on top of the system call interface, but applications are free to use both. The shell is a special application that provides an interface for running other applications.
Ls command
ls command is most widely used command and it displays the contents of directory.
options
ls will list all the files in your home directory, this command has many options. ls -l will list all the file names, permissions, group, etc in long format. ls -a will list all the files including hidden files that start with . . ls -lt will list all files names based on the time of creation, newer files bring first. ls -Fxwill list files and directory names will be followed by slash. ls -Rwill lists all the files and files in the all the directories, recursively. ls -R | more will list all the files and files in all the directories, one page at a time.
Nohup command.
nohup command if added in front of any command will continue running the command or process even if you shut down your terminal or close your session to machine. For exmaple, if I want to run a job that takes lot of time and must be run from terminal and is called update_entries_tonight . nohup update_entries_tonight will run the job even if terminal is shut down in middle of this job.
Head command.
head filename by default will display the first 10 lines of a file. If you want first 50 lines you can use head -50 filename or for 37 lines head -37 filename and so forth.
Tail command.
tail filename by default will display the last 10 lines of a file. If you want last 50 lines then you can use tail -50 filename.
Wc command
wc command counts the characters, words or lines in a file depending upon the option.
Options
wc -l filename will print total number of lines in a file. wc -w filename will print total number of words in a file. wc -c filename will print total number of characters in a file.
Mv command.
mv command is used to move a file from one directory to another directory or to rename a file.
Some examples:
mv oldfile newfile will rename oldfile to newfile. mv -i oldfile newfile for confirmation prompt. mv -f oldfile newfile will force the rename even if target file exists. mv * /usr/bajwa/ will move all the files in current directory to /usr/bajwa directory.
Rm command.
To delete files use rm command.
Options:
rm oldfile will delete file named oldfile. rm -f option will remove write-protected files without prompting. rm -r option will delete the entire directory as well as all the subdirectories, very dangerous command.
Rmdir command.
rmdir command will remove directory or directories if a directory is empty.
Options:
rm -r directory_name will remove all files even if directory is not empty. rmdir sandeep is how you use it to remove sandeep directory. rmdir -p will remove directories and any parent directories that are empty. rmdir -s will suppress standard error messages caused by -p.
Sort command.
sort command sort the lines of a file or files, in alphabetical order. for example if you have a file named testfile with these contents
zzz aaa 1234 yuer wer qww wwe
Options:
-b ignores leading spaces and tabs. -c checks whether files are already sorted. -d ignores punctuation. -i ignores non-printing characters. -n sorts in arithmetic order. -ofile put output in a file. +m[-m] skips n fields before sorting, and sort upto field position m. -r reverse the order of sort. -u identical lines in input file apear only one time in output.
PS command
ps command is probably the most useful command for systems administrators. It reports information on active processes. ps options options.
-a Lists all processes in system except processes not attached to terminals. -e Lists all processes in system. -f Lists a full listing. -j print process group ID and session ID.
Execution output: $ test.debug + PS4=[${LINENO}]+ [8]+echo 1 1 [8]+echo 2 2 [8]+echo 3 3 [11]+exit Notice that line numbers 8 and 11 from the test.debug script are identified in the execution trace output.
Q4. EXPLAIN MULTIPROGRAMMING, MULTI-USER AND MULTITASKING OPERATING SYSTEM WITH EXAMPLES AND DIAGRAMS?
Multiprogramming Multiprogramming is a rudimentary form of parallel processing in which several programs are run at the same time on a uni-processor. Since there is only one processor, there can be no true simultaneous execution of different programs. Instead, the operating system executes part of one program, then part of another, and so on. To the user it appears that all programs are executing at the same time.
Multitasking Multi tasking Operating system is the one which is capable of running multiple processes or tasks, by sharing the common resources like CPU. These operating systems use the concepts task-scheduling and context-switching for implementing the multitasking ability. Task scheduling algorithms will identify the task to be run at a given time and also which is the next task to run. These tasks will be queued for processing. Context switching is a process, to store and restore the state of a CPU. When a process is executed the state of the CPU with respect to that process will be restored and while completing the time slice, the present state will be stored.
Multi-user Multi-user is a term that defines an operating system or application software that allows concurrent access by multiple users of computer. Time-sharing systems are multi-user systems. Most batch processing systems for mainframe computers may also be considered "multi-user", to avoid leaving the CPU idle while it waits for I/O operations to complete. An example is a UNIX server where multiple remote users have access (such as via Secure Shell) to the Unix shell prompt at the same time.
Q7. WHAT IS THE PURPOSE OF THE GREP COMMAND? HOW GREP WORKS EXPLAIN?
grep command searches the given file for lines containing a match to the given strings or words. By default, grep prints the matching lines. Use grep to search for lines of text that match one or many regular expressions, and outputs only the matching lines. The name, "grep", derives from the command used to perform a similar operation, using the Unix/Linux text editor ed: g/re/p grep command syntax grep 'word' filename grep 'string1 string2' filename cat otherfile | grep 'something' command | grep 'something'
Use grep to search file Search /etc/passwd for boo user: $ grep boo /etc/passwd You can force grep to ignore word case i.e match boo, Boo, BOO and all other combination with -i option: $ grep -i "boo" /etc/passwd
Use grep recursively You can search recursively i.e. read all files under each directory for a string "192.168.1.5" $ grep -r "192.168.1.5" /etc/
Use grep to search words only When you search for boo, grep will match fooboo, boo123, etc. You can force grep to select only those lines containing matches that form whole words i.e. match only boo word: $ grep -w "boo" /path/to/file
Use grep to search 2 different words use egrep as follows: $ egrep -w 'word1|word2' /path/to/file
Count line when words has been matched grep can report the number of times that the pattern has been matched for each file using -c (count) option: $ grep -c 'word' /path/to/file Also note that you can use -n option, which causes grep to precede each line of output with the number of the line in the text file from which it was obtained: $ grep -n 'word' /path/to/file
Grep invert match You can use -v option to print inverts the match; that is, it matches only those lines that do not contain the given word. For example print all line that do not contain the word bar: $ grep -v bar /path/to/file
UNIX / Linux pipes and grep command grep command often used with pipes. For example print name of hard disk devices: # dmesg | egrep '(s|h)d[a-z]' Display cpu model name: # cat /proc/cpuinfo | grep -i 'Model' However, above command can be also used as follows without shell pipe: # grep -i 'Model' /proc/cpuinfo
How do I list just the names of matching files? Use the -l option to list file name whose contents mention main(): $ grep -l 'main' *.c
Q8. HOW TO CONCATENATE TWO OR MORE STRINGS INTO ONE? EXPLAIN IN DETAIL?
We can give the answer in two ways. 1. Using shell programming 2. Using the normal basic UNIX commands. 1. Shell programming $str1 = "Amity" $str2 = "University" read $str1 read $str2 $str3 = $str1 $str2 echo $str3 Output is: Amity University 2. Normal Basic UNIX commands f1 and f2 are two files having the data, "Amity" and "University" respectively. $cat f1 f2 We will get the desired output.
Q9. WHAT IS THE RELATIONSHIP BETWEEN THE ENVIRONMENT AND INHERITANCE AND THE SHELL?
Basic features common to all the UNIX shells include piping, here documents, command substitution, variables, control structures for condition-testing and looping and filename wildcarding. Both bourne shell and C shell have been used as coding base and model for many derivative and work-alike shells with extended feature sets. Every new shell is inherited from the old with certain new features. The UNIX shell is most people's main access to the UNIX operating system and as such any improvement to it can result in considerably more effective use of the system, and may even allow you to do things you couldn't do before. The primary improvement most of the new generation shells give you is increased speed. They require fewer key strokes to get the same results due to their completion features, they give you more information (e.g. showing your directory in your prompt, showing which files it would complete) and they cover some of the more annoying features of UNIX, such as not going back up symbolic links to directories. Hence, the relationship between the environment, inheritance and shell.