0% found this document useful (0 votes)
22 views

UNIX Material

The document discusses several UNIX/Linux commands and their usage including pwd, mkdir, cd, rmdir, cat, more, head, tail, ls, nl, cp, mv, and rm. Pwd prints the current working directory. Makedir creates new directories. Cd changes directories. Rmdir removes empty directories. Cat prints and concatenates files. More and head display part of files. Tail displays the last part of files. Ls lists files and directories. Nl numbers lines. Cp copies files. Mv moves or renames files. Rm removes files.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

UNIX Material

The document discusses several UNIX/Linux commands and their usage including pwd, mkdir, cd, rmdir, cat, more, head, tail, ls, nl, cp, mv, and rm. Pwd prints the current working directory. Makedir creates new directories. Cd changes directories. Rmdir removes empty directories. Cat prints and concatenates files. More and head display part of files. Tail displays the last part of files. Ls lists files and directories. Nl numbers lines. Cp copies files. Mv moves or renames files. Rm removes files.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 35

UNIX UTILITI

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:

mkdir dir1 dir2 dir3


Options:
-p - When the -p option is specified, mkdir creates all of the directories in the path
mkdir with -p option
:~]$ mkdir -p a/b/c
:~]$ cd a/b/c
:~/a/b/c]$

Creating multiple directories at a time


[exam5@rvrlinuxserver exam5]$ mkdir a b c
[exam5@rvrlinuxserver exam5]$ ls -l
drwxrwxr-x 2 exam5 exam5 4096 Jul 1 12:10 a
drwxrwxr-x 2 exam5 exam5 4096 Jul 1 12:10 b
drwxrwxr-x 2 exam5 exam5 4096 Jul 1 12:10 c
unix commands are case sensitive so every command should be written in lower case letters.

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:

-p - remove DIRECTORY and its ancestors.

-v - displays a message when directory is removed

Usage:

:~$ rmdir a/b/c


…..removes the directory 'c' inside the directory 'b'

:~$ rmdir -p a/b/c


……..removes inherent directories a,b,c

:~$ rmdir -v a/b/c


rmdir: removing directory, `a/b/c

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

Syntax: cat [options] [files]

To create a file,
syntax: cat > file
example: cat > ram.txt
Enter the matter
And press control - d to quit

To display the file,


syntax: cat file
example: cat ram .txt
hai
hello
To append data to the file
Syntax: cat >> filename
Example: cat>>ram.txt
welcome

To concatenate the multiple files,


syntax: cat file1 file2 file3 >> newfile
example: cat ram1 ram2 ram3 >> newram

more:
The more command is used to display the text one screen at a time.

Syntax: more [options] filename

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:

-n:print Last NUMBER lines instead of first 10


+n:prints from nth line to till end of file.
-nc:prints last n characters.

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.

Listing files and directories:ls


'ls' utility is used to lists the information about file or a directory .Its synopsis is given as
Syntax
ls [OPTION]... [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

ab.txt copy.txt file1 n2 unix b count.txt file1.txt l2 n3 s1 c d1 file2


lab n5 s2 compare1.txt d2 file2.txt link1.txt narmi sort1 compare.txt f1.txt file3
link.txt t1
Long listing of files use –l option
~]$ ls –l
-rw-rw-r-- 1 exam6 exam6 38 Jun 23 15:03 file3
-rw-rw-r-- 1 exam6 exam6 61 Jun 23 15:06 file4
-rw-rw-r-- 1 exam6 exam6 28 Jun 23 15:24 file5
drwxrwxr-x 2 exam6 exam6 4096 Jun 23 15:28 unix

Open Last Edited File Using ls -t


To open the last edited file in the current directory use the combination of ls, head and vi
commands as shown below.
ls -t sorts the file by modification time, showing the last edited file first. head -1 picks up this
first file.

Display One File Per Line Using ls -1


To show single entry per line, use -1 option.
Display Files Recursively Using ls -R
To show all the files recursively, use -R option. When you do this from /, it shows all the
unhidden files in the whole file system recursively.

Display File Inode Number Using ls –i


Sometimes you may want to know the inone number of a file for internal maintenance. Use -i option to
display inode number.

Visual Classification of Files With Special Characters Using ls -F


Instead of doing the ‘ls -l’ and then the checking for the first character to determine the type of
file. You can use -F which classifies the file with different special character for different kind of
files.
 Directory(/).
 normal file(nothing).
 link file (@).
 Executable file (*).

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.

Eg: cp pink red


Here the contents of the file red are erased and the contents of pink are copied to
the file red.

cp file1 file2 file3 ... dir

Copies the files to the target directory (last argument).


Eg: cp pinky mani sydney
Here syndey is a new directory. By using this command we are copying the files
pinky mani from prathyu directory to sydney directory directly.

cp –i file1 file2(interactive)

Asks for user confirmation if the target file already exists.

Eg: cp –i pinky mani


Here we are using –i to conform whether to copy or not.
cp : overwrite ‘mani’
Enter ‘y’ to copy or enter ‘n’ to reject the request.

cp –f file1 file2(noninteractive)

If the target file already exists,this command over writes it without asking for user
confirmation.

cp –r <source_dir> <target_dir> (recursive)


Copies the whole directory.

Eg: cp –r sydney aus


By using this command all the files in the sydney are copied into aus.

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

Moves the given file or directory.


Eg. mv tom cam
The name of the file changes from tom to cam.

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’.

rm -i file1 file2 file3


This command deletes files, asks user confirm.
Eg: rm –i laxmi1 laxmi2
rm : remove regular file ‘laxmi1’? n
rm : remove regular file ‘laxmi2’? y
This command asks user to confirm whether to delete the given files
‘laxmi1’ and ‘laxmi2’. Here the file ‘laxmi1’ is not confirm to delete and the file ‘laxmi2’ is
confirmed to delete. So the file ‘laxmi1’ will not be deleted and file ‘laxmi2’ will be deleted.

rm –r file1 file2 file3


rm –r dir1 dir 2 dir 3
This command delete multiple files or directories at a time without user’s confirm.

Eg: rm –r ram laxman


This command deletes the files ‘ram’ and ‘laxman’ without users confirm.

Find :
• It recursively examines a directory tree to look for matching some criteria and then takes some
action on the selected file

• The find command is used to locate files on a Unix system.

Syntax : find where-to-look criteria what-to-do

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.

Syntax : $ cmp options file1 file2

Example :

cmp s1 s2 (* if two files are same)

cmp: EOF on s1 (o/p)

$cmp s1 s2 (* if two files are different )

s1 s2 differ: byte 10, line 2 (o/p)

options:

-l:provide listing of all differences with character number, octal value of character in first file, octal value
of character in second file.

-s: suppress output.This option won’t display any output.

To display output use echo $?

Possible outcomes are 0:if both files are same

1:if both files are different

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:

Assuming a file named file containing the lines:

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

This gives the output:


:bar:ba
:two:th
ha:beta
quick
To output the fifth field through the end of the line of each line using the colon character as the
field delimiter:
% cut -d ":" -f 5- file
This gives the output:
Quux
five:six:seven
epsilon:zeta:eta:teta:iota:kappa:lambda:mu
the quick brown fox jumps over the lazy dog

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

Finally, the use of the -d option is illustrated in the following example:


$ paste –d” $” names.txt numbers.txt
Mark Smith$555-1234
Bobby Brown$555-9876
Sue Miller$555-6743
Jenny Igotit$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:

chmod [options] mode file(s)


symbolic method:

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:

 u the owner user


 g the owner group
 o others (neither u, nor g)
 a all users

example:

u+r : adding read permission to owner

g-x: removing execute permission for group.

a=w: assinging write permission to all.

Octal method:

In this method we use octal values instead of symbols.

The octal values are as follows

Read: 4

Write: 2

Execute:1

Example:

Chmod 756 file1

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:

chgrp oldgroupname newgroupname

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:

chgrp oldownername newownername

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

-j field number : Equivalent to -1 fieldnumber and -2 fieldnumber


-a filenumber : Prints non matched lines in a file

-i : ignore case while joining

> cat emp.txt > cat dept.txt


10 mark 10 hr
10 steve 20 finance
20 scott 30 db
30 chris
> join emp.txt dept.txt

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

> join -1 2 -2 1 emp.txt dept.txt


10 mark 1 hr 1
10 steve 1 hr 1
20 scott 2 finance 2
30 chris 3 db 3
Example 3:

> join -j 3 emp.txt dept.txt


1 mark 10 10 hr
1 steve 10 10 hr
2 scott 20 20 finance
3 chris 30 30 db
Example 4:
So far we have joined files with space delimiter. Here we will see how to join
files with a colon as delimiter. Consider the below two files.
> cat emp.txt
mark:10
steve:10
scott:20
chris:30

> cat dept.txt


10:hr
20:finance
30:db
The -t option is used to specify the delimiter. The join command for joining the
files is

> join -t: -1 2 -2 1 emp.txt dept.txt


10:mark:hr
10:steve:hr
20:scott:finance
30:chris:db

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.

> cat emp.txt


mark,A
steve,a
scott,b
chris,C
> cat dept.txt
a,hr
B,finance
c,db

> join -t, -i -1 2 -2 1 emp.txt dept.txt


A,mark,hr
a,steve,hr
b,scott,finance
C,chris,db
sort
command is used to sort a file, arranging the records in a particular order. By default, the sort
command sorts file assuming the contents are ascii. Using options in sort command, it can also be
used to sort numerically.

Syntax:

Sort [option] filename

$ cat file
Unix
Linux
Solaris
AIX
Linux
HPUX

Example1. sort simply sorts the file in alphabetical order:

$ sort file
AIX
HPUX
Linux
Linux
Solaris
Unix

Example2. sort removes the duplicates using the -u option:

$ 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.

Let us consider a file with numbers:

$ 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.

Example 4. To sort a file numerically:

$ sort -n file
5
19
20
49
200
-n option can sort the decimal numbers as well.

Example5. sort file numerically in reverse order:

$ sort -nr file


200
49
20
19
5
'r' option does a reverse sort.

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

$ sort -n file1 file2


5
5
18
19
20
25
48
49
200
200

Sort, merge and remove duplicates

$ sort -nu file1 file2


5
18
19
20
25
48
49
200

Example7 Files with multiple fields and delimiter:


Let us consider a file with multiple fields:

$ cat file
Linux,20
Unix,30
AIX,25
Linux,25
Solaris,10
HPUX,100

Example8. sorting a file containing multiple fields:

$ 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.

Example9. sort file on the basis of 1st field:

$ sort -t"," -k1,1 file


AIX,25
HPUX,100
Linux,20
Linux,25
Solaris,10
Unix,30
This is being more explicit. '-t' option is used to provide the delimiter in case of files with delimiter. '-k'
is used to specify the keys on the basis of which the sorting has to be done. The format of '-k' is : '-
km,n' where m is the starting key and n is the ending key. In other words, sort can be used to sort on
a range of fields just like how the group by in sql does. In our case, since the sorting is on the 1st
field alone, we speciy '1,1'. Similarly, if the sorting is to be done on the basis of first 3 fields, it will be:
'-k 1,3'.

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.

Syntax: uniq [option] filename

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

-u option is to print only unique lines in file.


$ uniq -u test
xx

-f option skips comparison of first specified ‘N’ fields of lines in file.


$ cat test2
hi hello Linux
hi friend Linux
hi hello LinuxUnix
The following uniq command using option ‘f’ skips comparing first 2 fields of lines in file.
Here, starting 2 fields i.e. ‘hi hello’ in 1st line and ‘hi friend’ in 2nd line would not be compared
and then next field ‘Linux’ in both lines are same so would be shown as duplicated lines.
$ uniq -f 2 test2
hi hello Linux
hi hello LinuxUnix

-s option skips comparison of first specified ‘N’ characters of lines in file.

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]

The options of tr command are:


-c : complements the set of characters in string.
-d : deletes the characters in set1
-s : replaces repeated characters listed in the set1 with single occurrence
-t : truncates set1

Examples:

1. Convert lower case letters to upper case

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.

$tr -c "u" "a"


unix
uaaa

The -d option can be used to delete characters.


$ tr -d "aeiou"
welcome to unix
wlcm t nx
-s option is to squeeze more than one occurrence of continuous characters with single
occurrence.
$ tr -s "a" "i"
hai
hi
wc
WC command in unix or linux is used to find the number of lines, words and characters in a file.
The syntax:

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

-l option is used for Printing count of lines

wc -l unix_wc.bat
5 unix_wc.bat

-w option is used for displaying the number of words.

wc -w unix_wc.bat
13 unix_wc.bat

-c option is used to print count of bytes,

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

tee option filenames

option:

-a: append to the given file don’t overwrite it.

Example:

ls –l *.txt|wc –l|tee count.txt

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

About other people


 w --- tells you who's logged in, and what they're doing. Especially useful: the 'idle' part.
This allows you to see whether they're actually sitting there typing away at their
keyboards right at the moment.
 who --- tells you who's logged on, and where they're coming from. Useful if you're
looking for someone who's actually physically in the same building as you, or in some
other particular location.
 finger username --- gives you lots of information about that user, e.g. when they last
read their mail and whether they're logged in. Often people put other practical
information, such as phone numbers and addresses, in a file called .plan. This
information is also displayed by 'finger'.
 last -1 username --- tells you when the user last logged on and off and from where.
Without any options, last will give you a list of everyone's logins.
 talk username --- lets you have a (typed) conversation with another user
 write username --- lets you exchange one-line messages with another user
 elm --- lets you send e-mail messages to people around the world (and, of course, read
them). It's not the only mailer you can use, but the one we recommend. See the elm page,
and find out about the departmental mailing lists (which you can also find in
/user/linguistics/helpfile).

About your (electronic) self


 whoami --- returns your username. Sounds useless, but isn't. You may need to find out
who it is who forgot to log out somewhere, and make sure *you* have logged out.
 finger & .plan files
of course you can finger yourself, too. That can be useful e.g. as a quick check whether
you got new mail. Try to create a useful .plan file soon. Look at other people's .plan files
for ideas. The file needs to be readable for everyone in order to be visible through 'finger'.
Do 'chmod a+r .plan' if necessary. You should realize that this information is accessible
from anywhere in the world, not just to other people on turing.
 passwd --- lets you change your password, which you should do regularly (at least once a
year). See the LRB guide and/or look at help password.
 ps -u yourusername --- lists your processes. Contains lots of information about them,
including the process ID, which you need if you have to kill a process. Normally, when
you have been kicked out of a dialin session or have otherwise managed to get yourself
disconnected abruptly, this list will contain the processes you need to kill. Those may
include the shell (tcsh or whatever you're using), and anything you were running, for
example emacs or elm. Be careful not to kill your current shell - the one with the number
closer to the one of the ps command you're currently running. But if it happens, don't
panic. Just try again :) If you're using an X-display you may have to kill some X
processes before you can start them again. These will show only when you use ps -efl,
because they're root processes.
 kill PID --- kills (ends) the processes with the ID you gave. This works only for your
own processes, of course. Get the ID by using ps. If the process doesn't 'die' properly, use
the option -9. But attempt without that option first, because it doesn't give the process a
chance to finish possibly important business before dying. You may need to kill
processes for example if your modem connection was interrupted and you didn't get
logged out properly, which sometimes happens.
 quota -v --- show what your disk quota is (i.e. how much space you have to store files),
how much you're actually using, and in case you've exceeded your quota (which you'll be
given an automatic warning about by the system) how much time you have left to sort
them out (by deleting or gzipping some, or moving them to your own computer).
 du filename --- shows the disk usage of the files and directories in filename (without
argument the current directory is used). du -s gives only a total.
 last yourusername --- lists your last logins. Can be a useful memory aid for when you
were where, how long you've been working for, and keeping track of your phonebill if
you're making a non-local phonecall for dialling in.

Connecting to the outside world


 nn --- allows you to read news. It will first let you read the news local to turing, and then
the remote news. If you want to read only the local or remote news, you can use nnl or
nnr, respectively. To learn more about nn type nn, then \tty{:man}, then \tty{=.*}, then \
tty{Z}, then hit the space bar to step through the manual. Or look at the man page. Or
check out the hypertext nn FAQ - probably the easiest and most fun way to go.
 rlogin hostname --- lets you connect to a remote host
 telnet hostname --- also lets you connect to a remote host. Use rlogin whenever possible.
 ftp hostname --- lets you download files from a remote host which is set up as an ftp-
server. This is a common method for exchanging academic papers and drafts. If you need
to make a paper of yours available in this way, you can (temporarily) put a copy in
/user/ftp/pub/TMP. For more permanent solutions, ask Emma. The most important
commands within ftp are get for getting files from the remote machine, and put for
putting them there (mget and mput let you specify more than one file at once). Sounds
straightforward, but be sure not to confuse the two, especially when your physical
location doesn't correspond to the direction of the ftp connection you're making. ftp just
overwrites files with the same filename. If you're transferring anything other than ASCII
text, use binary mode.
 lynx --- lets you browse the web from an ordinary terminal. Of course you can see only
the text, not the pictures. You can type any URL as an argument to the G command.
When you're doing this from any Stanford host you can leave out the .stanford.edu part
of the URL when connecting to Stanford URLs. Type H at any time to learn more about
lynx, and Q to exit.

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

You might also like