Sie sind auf Seite 1von 6

Unix Commands

Logging in
•Find a computer that is available for your use. If a workstation is available, a login prompt
should be displayed. For example, on the Umber workstation the login prompt looks like this:
login:
•Type your account name. If you make a mistake, type control-U and start over.
•Press the Return key. This tells the computer that you have finished entering your account
name. The computer should then prompt you with "Password:"
•Type your password and press the Return key. The letters you type should not appear on the
screen; this helps you to keep your password a secret.
•If you log in successfully, you should see a Unix prompt appear at the left side of your screen

How to log out


•From the Unix prompt, type "logout". Or press “ctrl d” key
from keyboard.

Commands for Files and directories


ls (list)
When you first login, your current working directory is your home directory. Your home
directory has the same name as your user-name. This command shows the list of all files in
the current directory.

$ ls (short listing)

Switches –
-l long listing
-a All hidden files.
-r Display in reverse order
-R Recursive listing (Tree structure)

pwd (present working directory)


Pathnames enable you to work out where you are in relation to the whole file-system. For
example, to find out the absolute pathname of your home-directory, type cd to get back to your
home-directory and then type
$ pwd
more filename ---
shows the first part of a file, just as much as will fit on one screen. Just hit the space bar to
see more or q to quit. You can use /pattern to search for a pattern.
e.g. $ more f1

mv filename1 filename2 –
moves a file (i.e. gives it a different name, or moves it into a different directory
e.g. mv f1 f2

cp filename1 filename2 --- copies a file.


e.g. cp f1 /home/mca/d2

rm filename --- removes a file.


-i – remove with confirmation
-r – remove whole tree structure of directory file.

e.g. rm f1

rmdir dir name - this is used to remove director if it is empty.


e.g. rmdir d1

lp file name – It is used to take the print out of a file.


e.g. lp f1

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'.
passwd --- lets you change your password.
e.g. $ passwd
old password :
new password :

Types of Users –
There are three kind of users in Unix.
•Owner (u)
•Group (g)
•Others (o)
We can create different kind of groups inside unix shell with different kind of permissions
and utilities.
Owner is a user who is logged in. Group users are the users who belong to same group
created by administrator, and others are the all users other than your login.
Permissions –
There are 3 classes of file permission. For three kind of users. To change permission of the
file. command used is –chmod
Permission Symbol Octal no.
read r 4
write w 2
Execute x 1

chmod – there are two modes to work with chmod command.


1. Relative mode
2. Absolute mode
1. chmod <usertype> - <permission> <filename>
e.g. – there is a file <test> with permissions –rw-rw-r--. User wants to add write permission
of others.
$ chmod o+w test + is used to add permissions
- is used to subtract the permissions
•chmod <sum of octalno of all user permissions> <filename>
e.g-
$ chmod 666 test

Pipes –
Pipes are the utilities through which user can make composite commands in such a manner
as output of one command will be treated as input for next command.

Command 1 | command 2
e.g. ls –l |more
Filters –
Are the utilities to search a particular pattern in the input.
e.g. – grep “this” file1 (will search all the lines match with the pattern “this”)

grep – it is a type of filter which is used to search a particular (one) pattern in a file.
grep “pattern” <filename>
e.g. – grep “is” data (will search all the lines contain “is” word)

egrep – it is a type of filter which is used to search more than one pattern in a file.
egrep “pat1|pat2…” <filename>
e.g. – egrep “is | that” data

File Comparisson –
1. cmp – this is a command used to compare file character wise and stop comparison if it finds
first difference.
cmp <file1> <file2>

2. comm – used to compare two files. But both files should be sorted. It gives individual lines
from 1st file, 2nd file and same lines of both files.
comm <file1> <file2>
diff – used to compare two files line by line if they are sorted and give only difference.
diff <file1> <file2>
Cut – This is a filter command used to cut no. of characters and fields from a file.
-c Character cut
-f Field Cut
-d IFS
Cut –c(no of characters) <file>
e.g. cut –c1-3 data “will display 1 to 3 starting characters of each line
from data file.”
Vi data
A:200:100:20
B:10:20:30
C:12:11:23
Display 2nd and 3rd field from data file

$ cut –f2,3 –d “:” data

Sort – used to sort data input in ascending or descending order.


Sort <filename>

Switch –
-o used to store output of sort command into a file
sort –o file1 file1 # will store sorted output in file1 itself
-r used to sort file in reverse order
sort –r file1
-n used for numeric sort
sort –n file1

Write sort command to sort data file by salary which is 2nd column in file

Sort –n+1 –t “:” data

paste – used to join 2 or more outputs using or Not using IFS

Paste –d <IFS> file1 file2 -----


File1 – A:1000 file2 - 20:30
B:2000 40:20
C:3000 10:20

Paste both files using IFS “|”


$ paste –d “|” file1 file2
tail – used to display last 10 lines from a file

$ tail -<line nos> file


tail -5 file1 “display last 5 lines from file1”

head – used to display first 10 lines from a file.


$ head - <no of lines > file
head -4 file1 “display start 5 lines from file1”
Composite commands using pipe(|)- join 2 or more commands in such a way that
output of one command treated as input for next command.
e.g.
1. Write command to display list of ordinary files only
ls –l | grep “^-”
2. Write a command to display permissions of files with their name
ls –l | tr – s “ “ | cut –f1,9 –d “ “

ln – used to create symbolic link for a file. Symbolic link is a technique through which one file can
be accessed at many places by creating logical links in such a way –
- physically it will be stored at one place
- logically accessible by many places.
- changes to one link will reflect to the all links
Note - ls –l will automatically update link column
ln <source file> <link file>

$ ln f1 /d2 “create a link of f1 into d2 directory with same name”

Communication in Unix –
Unix provides two kind of communication
•on-line communication – In which it is required that both users i.e. sender or receiver should
be login.

commands –
•Write – command to send online messages to any user
write <username>
•Wall – command used to broadcast any message to all users working with unix. It is used by
administrator.
•Mesg – this is used to off or on the permission for online communication.
mesg <permission>
e.g. mesg y

•Off- Line Communication – in which it is not required that receiver should be logged in. the
messages stored into the inbox or mailbox of the user.
•Mail – this is used to send off line messages like email.
mail <username>

Receive mail – to receive mail


•Type mail command at prompt\
$mail # it will show mail list

•To select any mail type mail no 1 or 2 or 3 etc


•to give reply type r
•To delete particular mail type d<mailno>d1 or d2 etc.
•To exit from mail box type q or quit

Sending Mail
Say I want to send a short note to the person with the email address
nobody@december.com. Here is an example of how I would do this:
$ mail nobody@december.com
Subject: Hi
This is just a short note to say hello. I don't have anything else right now. .
Cc:

what is a symbolic link


A symbolic link, also termed a soft link, is a special kind of file that points to another file,
much like a shortcut in Windows or a Macintosh alias. Unlike a hard link, a symbolic link
does not contain the data in the target file. It simply points to another entry somewhere in
the file system. This difference gives symbolic links certain qualities that hard links do not
have, such as the ability to link to directories, or to files on remote computers networked
through NFS. Also, when you delete a target file, symbolic links to that file become
unusable, whereas hard links preserve the contents of the file.
To create a symbolic link in Unix, at the Unix prompt, enter:
ln -s source_file myfile
The ln command then creates the symbolic link. After you've made the symbolic link, you
can perform an operation on or execute myfile, just as you could with the source_file. You
can use normal file management commands (e.g., cp, rm) on the symbolic link.

Note: If you delete the source file or move it to a different location, your symbolic file will not
function properly. You should either delete or move it. If you try to use it for other purposes
(e.g., if you try to edit or execute it), the system will send a "file nonexistent" message.

hard link
A hard link is essentially a label or name assigned to a file. Conventionally, we think of a file
as consisting of a set of information that has a single name. However, it is possible to create
a number of different names that all refer to the same contents. Commands executed upon
any of these different names will then operate upon the same file contents.
To make a hard link to an existing file, enter:
ln oldfile newlink
This will create a new item in your working directory, newlink, which is linked to the contents
of oldfile. The new link will show up along with the rest of your filenames when you list them
using the ls command. This new link is not a separate copy of the old file, but rather a
different name for exactly the same file contents as the old file. Consequently, any changes
you make to oldfile will be visible in newlink.

You can use the standard Unix rm command to delete a link. After a link has been removed,
the file contents will still exist as long as there is one name referencing the file. Thus, if you
use the rm command on a filename, and a separate link exists to the same file contents, you
have not really deleted the file; you can still access it through the other link. Consequently,
hard links can make it difficult to keep track of files. Furthermore, hard links cannot refer to
files located on different computers linked by NFS, nor can they refer to directories.

Das könnte Ihnen auch gefallen