UNIX Material
UNIX Material
PWD
“pwd” prints the fully resolved name of the current working directory.
Syntax:
pwd [OPTION]
[exam5@rvrlinuxserver exam5]$ pwd
/users/exams/exam5
mkdir
In order to create a new directory(which are also referred to as folders in some operating
systems), you use the mkdir command.
Syntax:
$mkdir [option] directory_name(s)
directory_name is the name of any directory that the user is asking mkdir to create.Any number
of directories can be created simultaneously.Thus, for example, the following would create three
directories in the current directory with the names dir1, dir2 and dir3:
CHANGING DIRECTORIES:cd
The cd(change directory) shell command changes a shell's current working directory to
directoryName.If the directoryName argument is omitted,the shell is moved to is owner's home
directroy.
Syntax:
cd [directoryName]
1. changes current working directory in to specified directory.
:~$ ]cd a
:~/a]$
2. using 'cd' (or) 'cd ~' command with out any arguments it changes to home directory
:~/a/b/c]$ cd (or) :~/a/b/c]$ cd ~
:~]$
3.'cd ..' command with out any arguments it changes to parent directory
:~/a/b/c]$ cd ..
:~/a/b]$
REMOVING DIRECTORY:rmdir
The rmdir utility removes all of the directories from the list of directory names.A
directory must empty before it can be removed.
Syntax:
rmdir [option] directoryName
Options:
Usage:
cat:
The cat command is used to
i) create a file
ii) print the contents of a specified file and
iii) to append line to already existing file
iv) concatenate multiple files into one larger new file
To create a file,
syntax: cat > file
example: cat > ram.txt
Enter the matter
And press control - d to quit
more:
The more command is used to display the text one screen at a time.
Options Meaning
+num Start at line number num
-num specifies an integer which is the screen size (in lines).
+\pattern Locate first occurance of pattern and starts output from two lines before the
pattern till end of file.
head
Displays the first <n> lines (or 10 by default) of the given file.
With more than one File,precede each with the header giving the file name.
with no file,or when File is -,read standard input.
Syntax
Head [option]…[file]…
Options:
-c :print the size bytes
-n:print first NUMBER lines instead of first 10
For example,
head essay.001
->displays the first 10 lines of the file essay.001 To see a specific number of lines, do this:
head -n 20 essay.001
->This displays the first 20 lines of the file
tail
->Displays the last <n> lines (or 10 by default) of the given file.
->With more than one File,precede each with the header giving the file name.with no
file,or when File is -,read standard input.
Displays the last 10 lines of the given file and continues to display new lines when
they are appended to the file.
Syntax:
tail [option]…[file]…
Options:
For example,
tail essay.001
->displays the last 10 lines of the file essay.001 To see a specific number of lines, do this:
tail -n 20 essay.001
->This displays the last 20 lines of the file.
Options & Usage:'ls' with no arguments list all of the files in the current directory in
alphabetical order,excluding files whose name starts with a period.
[exam5@rvrlinuxserver exam5]$ ls
nl
nl is a UNIX utility for numbering lines, either from a file or from standard input,
reproducing output on standard output.
It has a number of switches:
a - number all lines
t - number lines with printable text only
Example:
$ nl tf
1 echo press cr
2 read cr
3 done
COPY(cp)
The cp command is used to copy a file in the same directory or to copy a
file to and from other directories. The format of the cp command is:
Syntax:
cp file1 file2
This syntax is used to copy one file to another.
cp –i file1 file2(interactive)
cp –f file1 file2(noninteractive)
If the target file already exists,this command over writes it without asking for user
confirmation.
Move(mv)
The mv command is used to move a file from one location to another. The
format of the mv command is:
Syntax:
mv oldname newname
mv –i oldname newname
If the new file already exits, asks for user confirm.
Eg: mv –i timmy kinny
mv: overwrite ‘kinny’
This command renames the oldfile ‘timmy’ with the newfile ‘kinny’ if it
does not exisits. If the filename ‘kinny’ does not exists then it asks user to confirm.
mv –f oldname newname
If the new file already exits, does not ask for user confirm.
Eg: mv –f off on
This command renames the old file ‘off’ with the new filename ‘on’(which is created
while executing the command) without users confirm.
Remove(rm)
The rm command is used to delete a file.
Syntax: rm filename
This command removes the given file
Eg: rm abc
This command removes the file ‘abc’ from the respective directory.
rm --*
This command deletes all the files that are having the same filename in the starting.
Eg: rm m*
This command deletes all the files starting with ‘m’.
Find :
• It recursively examines a directory tree to look for matching some criteria and then takes some
action on the selected file
Example:
find will display the pathnames of all files in the current directory and all sub directories. The commands
find . –print
(2 ) To locate all c files in current directory
$find . -name “*.c” –print
(3) To display the names of those files which are created in the last “n” days and are in the current
directory .
$find . –ctime n –print
(4) To display the names of those files which are modified in the last “n” days and are in the current
directory
$find . –mtime n –print
find –print
find .
Here's an example find command using a search criterion and the default action:
find . –name filename
diff :
• The diff command shows line by line difference between two files . The first file is compared to
the second file.
• The differences are identified such that the first file could be modified to make it match with the
second file.
Syntax :
• $ diff file1 file2 The each difference is displayed using the following format
range1 action range2
<text from file1
……………….
>text from file2
• Action can be
(1 )change ( c )
(2 ) append (a)
(3) delete (d)
1. Change : It indicates what action should be taken to make file1 the same as file2
2. Append : it indicates what lines are needed to added to file1 to make it same as file2 *(it
occur only when file1 is shorter than file2)
3. Delete : it indicates what lines must be deleted from file1 to make it same as file2 *(it
occur only when file1 is longer than file2)
4. Examples :
1. 6 c 6 : change :replace line 6 in file1 with
line 6 in file2
2. 25 a 26,27 : append :at the end of file1
(after25th line),insert line 26 and
27 from file2
3. 78,79 d 77 : delete: the extra lines at the end
of file 1 should be deleted
Cmp:
• It compares two files of any type and writes the results to the standard output
• By default, cmp is silent if the files are the same; if they differ, the byte and line number at
which the first difference occurred is reported.
Example :
options:
-l:provide listing of all differences with character number, octal value of character in first file, octal value
of character in second file.
cut
In computing cut is a Unicx command line utility which is used to extract sections from
each line of input — usually from a file
Extraction of line segments can typically be done by bytes (-b), characters (-c), or fields
(-f) separated by a delimiter (-d — the tab charater by default). A range must be provided in each
case which consists of one of N, N-M, N- (N to the end of the line), or -M (beginning of the line
to M), where N and M are counted from 1 (there is no zeroth alue). Since version 6, an error is
thrown if you include a zeroth value. Prior to this the value was ignored and assumed to be 1.
Examples:
foo:bar:baz:qux:quux
one:two:three:four:five:six:seven
alpha:beta:gamma:delta:epsilon:zeta:eta:teta:iota:kappa:lambda:mu
the quick brown fox jumps over the lazy dog
To output the fourth through tenth characters of each line:
% cut -c 4-10 file
paste
paste is a Unix command line utility which is used to join files (parallel
merging) by outputting lines consisting of the sequentially corresponding lines of each file
specified, separated by tabs, to the standard output. It is effectively the horizontal equivalent to
the utility cat command which operates on the vertical plane of two or more files.
Usage:The paste utility is invoked with the following syntax:
paste [options] [file1 ..]
Description: Once invoked, paste will read all its file arguments. For each corresponding line,
paste will append the contents of each file at that line to its output along with a tab. When it has
completed its operation for the last file, paste will output a newline character and move on to the
next line.
Options:The paste utility accepts the following options:
-d delimiters, which specifies a list of delimiters to be used instead of tabs for
separating consecutive values on a single line. Each delimiter is used in turn; when the list has
been exhausted, paste begins again at the first delimiter.
-s, which causes paste to append the data in serial rather than in parallel; that is,
in a horizontal rather than vertical fashion.
Examples
For the following examples, assume that names.txt is a plain-text file that contains the following
information:
Mark Smith
Bobby Brown
Sue Miller
Jenny Igotit
and that numbers.txt is another plain-text file that contains the following information:
555-1234
555-9876
555-6743
867-5309
The following example shows the invocation of paste with names.txt and numbers.txt as well as
the resulting output:
$ paste names.txt numbers.txt
Mark Smith 555-1234
Bobby Brown 555-9876
Sue Miller 555-6743
Jenny Igotit 867-5309
When invoked with the -s option, the output of paste is adjusted such that the information is
presented in a horizontal fashion:
$ paste -s names.txt numbers.txt
Mark Smith Bobby Brown Sue Miller Jenny Igotit
555-1234 555-9876 555-6734 867-5309
Grep
• Grep stands for “global regular expression print “
• A regular expression is a pattern consisting of sequence of character that is matched against
text.
• Grep is a family of programs that is used to search the input file for all lines that matches a
specified regular expression and writes them to the standard output
Syntax :
$ grep options regexp filename
Example :
$ cat sa
hello unix programming
unix utilities
file handling
process handling
disc utilities
$ grep unix sa /* search for the word unix */
hello unix programming
unix utilities
Options :
(1) -n – it assign line numbers on the standard out put stream.
Example :
$ grep –n unix sa
1:hello unix programming
2:unix utilities
6:unix advanced
(2) –w –it display whole word on the standard output stream .
Ex:
$grep -w unix sa
hello unix programming
unix utilities
unix advanced
$grep -wn unix sa
1:hello unix programming
2:unix utilities
6:unix advanced
(3 ) –v : reverse the filter
Ex:
$ grep -v unix sa
hello world
programming
file handling
disc utilities
(4) –l:displays files from the given list, which contains given regular expression
Ex:
Grep –l unix f1 f2 f3 f4
(5) –i: ignores the case while comparing the pattern.
fgrep:
fixed global regular expression.
It is the fast version of grep.It searches only strings, won’t support regular expressions.
Syntax:
fgrep option string filename
fgrep contains all options which are supported by grep(-n,-I,-l,-v,-w)and –x
-x :outputs only lines that are exactly equal to given string
egrep:
Extended Global Regular Expression.
Syntax:
egrep option regularexpression filename
Egrep supports all regular expressions supported by grep and it supports other regular expressions like
*,?,+,|
*:matches previous atom zero or more times.
+:matches previous atom one or more times.
?:matches previous atom zero or one time.
|:Alternation.
chmod
It is used to change mode(permissions) of a perticular file.
Permissions:
Every file on the system has associated with it a set of permissions. Permissions tell
UNIX what can be done with that file and by whom. There are three things you can (or can't) do
with a given file:
read it,
write (modify) it and
execute it.
Syntax:
The 'mode' part specifies the new permissions for the file(s) that follow as arguments. A
mode specifies which user's permissions should be changed, and afterwards which access types
should be changed. The permissions start with a letter specifying what users should be affected
by the change, this might be any of the following:
example:
Octal method:
Read: 4
Write: 2
Execute:1
Example:
Here the value for owner is 7(4+2+1),so owner has permissions r,w,x.
The octal value for group is 5(4+1), so group has read,execute permissions.
The octal value for others is 6(4+2), so others has read, write permissions.
chgrp
The chgrp (from change group) command is used by unprivileged users on UNIX like systems
to change the group associated with a computer file.
Syntax:
chown
The chown (from change owner) command is used by unprivileged users on UNIX
like systems to change the owner associated with a computer file.
Syntax:
join
Join command is one of the text processing utility in Unix/Linux. Join command
is used to combine two files based on a matching fields in the files. If you know
SQL, the join command is similar to joining two tables in a database.
Syntax:
join [options] file1 file2
Options:
-1 field number : Join on the specified field number in the first file
-2 field number : Join on the specified field number in the second file
10 mark hr
10 steve hr
20 scott finance
30 chris db
Example2:
>cat emp.txt > cat dept.txt
mark 10 1 10 hr 1
steve 10 1 20 finance 2
scott 20 2 30 db 3
chris 30 3
From the above, you can see the join fields are the second field from the emp.txt
and the first field from the dept.txt. The join command to match these two files
is
Example 5:
If the join fields are in different cases, then the join will not be performed
properly. To ignore the case in join use the -i option.
Syntax:
$ cat file
Unix
Linux
Solaris
AIX
Linux
HPUX
$ sort file
AIX
HPUX
Linux
Linux
Solaris
Unix
$ sort -u file
AIX
HPUX
Linux
Solaris
Unix
The duplicate 'Linux' record got removed. '-u' option removes all the duplicate records in the file.
Even if the file have had 10 'Linux' records, with -u option, only the first record is retained.
$ cat file
20
19
5
49
200
Example3. The default sort 'might' give incorrect result on a file containing numbers:
$ sort file
19
20
200
49
5
In the above result, 200 got placed immediately below 20, not at the end which is incorrect. This is
because the sort did ASCII sort. If the file had not contained '200', the default sort would have given
proper result. However, it is incorrect to sort a numerical file in this way since the sorting logic is
incorrect.
$ sort -n file
5
19
20
49
200
-n option can sort the decimal numbers as well.
Example6
sort can sort multiple files as well.
Let us consider examples with multiple files, say file1 and file2, containing numbers:
$ cat file1
20
19
5
49
200
$ cat file2
25
18
5
48
200
$ cat file
Linux,20
Unix,30
AIX,25
Linux,25
Solaris,10
HPUX,100
$ sort file
AIX,25
HPUX,100
Linux,20
Linux,25
Solaris,10
Unix,30
As shown above, the file got sorted on the 1st field, by default.
Note: For a file which has fields delimited by a space or a tab, there is no need to specify the "-t"
option since the white space is the delimiter by default in sort.
Who
Displays who is logged on to the system
Displays the username, line, and time of all currently logged-in sessions.
For example:
$who
y13cs900 pts/8 Jul 4 13:24 (10.1.9.84)
y13cs907 pts/12 Jul 4 13:25 (10.1.9.33)
y13cs892 pts/11 Jul 4 13:25 (10.1.9.81)
y13cs910 pts/3 Jul 4 13:25 (10.1.9.45)
y13cs921 pts/14 Jul 4 13:26 (10.9.10.60)
y13cs891 pts/15 Jul 4 13:27 (10.1.9.80)
y13cs922 pts/19 Jul 4 13:33 (10.1.9.27)
uniq
The uniq command can eliminate or count duplicate lines in a presorted file. It reads in
lines and compares the previous line to the current line. Depending on the options specified on
the command line it may display only unique lines or one occurrence of repeated lines or both
types of lines.
Options:
-c Precedes each line with a count of the number of times it occurred in the input.
-d Deletes duplicate copies. Only one line out of a set of repeated lines is
displayed.
-u Displays only lines not duplicated (uniq lines).
-fn Ignores the first n fields of an input line when comparing for duplicate lines. A
field is a string of nonblank characters. A blank is a space or tab.
-sn Ignores the first n characters of an input line when comparing for duplicate
lines.
Example1:
$ cat test
aa
aa
bb
bb
bb
xx
when uniq command is run without any option, it removes duplicate lines and displays unique
lines as shown below.
$ uniq test
aa
bb
xx
Example2
-c option is to count occurrence of lines in file.
$ uniq -c test
2 aa
3 bb
1 xx
-d option is to print only duplicate repeated lines in file. As you see below, this didn’t display the
line “xx”, as it is not duplicate in the test file.
$ uniq -d test
aa
bb
tr:
tr stands for translate or transliterate. The tr utility in unix or linux system is used to translate,
delete or squeeze characters.
syntax:
tr [options] set1 [set2]
Examples:
The following tr command translates the lower case letters to capital letters in the give string:
$ tr "[a-z]" "[A-Z]"
linux dedicated server
LINUX DEDICATED SERVER
The -c option is used to replace the non-matching characters with another set of characters.
wc [options] filenames
options
-l : Prints the number of lines in a file.
-w : prints the number of words in a file.
-c : Displays the count of bytes in a file.
-L : prints only the length of the longest line in a file.
Example:
cat unix_wc.bat
Oracle Storage
unix distributed system
linux file server
debian server
Oracle backup server
wc -l unix_wc.bat
5 unix_wc.bat
wc -w unix_wc.bat
13 unix_wc.bat
wc -c unix_wc.bat
92 unix_wc.bat
-L option is used to print the number of characters in the longest line from a file.
wc -L unix_wc.bat
23 unix_wc.bat
If you dont specify any option to the wc command, by default it prints the count of lines, words
and characters. This is shown below:
wc unix_wc.bat
5 13 92 unix_wc.bat
tee:
It copies standard input to standard output at the same time copies it to one or more files.
Syntax
option:
Example:
Here the output of ls –l is given as input to wc command.wc command counts the lines and
displays the output on standard output as well as send to count.txt file.
tty:
print filename of terminal connected to standard input.
Example:
$tty
/dev/pts/24
Miscellaneous tools
webster word --- looks up the word in an electronic version of Webster's dictionary and
returns the definition(s)
date --- shows the current date and time.
cal --- shows a calendar of the current month. Use e.g., 'cal 10 1995' to get that for
October 95, or 'cal 1995' to get the whole year.
You can find out more about these commands by looking up their manpages:
man commandname --- shows you the manual page for the command