Subscribe

Join the Mailing List
Enter your name and email address below:
Name:
Email:
Subscribe  Unsubscribe 

Friday, May 30, 2008

Basic Unix commands

Common UNIX Commands
This document presents a brief description of commonly used UNIX commands. The list is a small
subset of the available commands and utilities. For more information on these commands and others not
listed here, consult the on-line manual pages (see the man command).
Most people only learn UNIX to do specific things, like to publish a Web Page. To that effect, below are
listed the commands that are needed to do the things necessary on camalott.com. Just click, and it will
take you to those commands.
Common Commands and their MS-DOS equivalents.
1. ls = dir
2. mkdir = mkdir
3. cp = copy
4. mv = move
5. pwd = (tells you where you are)
6. cd = cd
7. exit = (exit from camalott.com)
Note: Enter the commands below exactly as shown (including spaces and upper- or lowercase).
Arguments or parameters to the commands are shown in italics and should be replaced with your own
values at the time that you execute the command (e.g. the name of a file is an argument to the cat
command rather than the word, file).
alias alias-term command-string
The alias built-in shell command allows the entering of shorter or easy-to-remember names to
execute longer or hard-to-remember commands. The command is entered to set alias-term equal to
command-string. If command-string has spaces in it, single quotes should be around the string. For
example, entering alias dir=’ls -al’ will allow ls -al to be executed whenever the dir
command is entered (thus easing the transition to UNIX for the MS-DOS users). Each user can put
these alias commands in their .bashrc file to have the aliases automatically set each time the user
logs in. Entering alias by itself will list all the aliases currently set for the user.
cal
The cal command displays the current month. If you specify the year, e.g. cal 1996, it will display
all the months for that year. The -j specifies the display to be in the Julian calender, e.g. cal -j
1996.
cat file
The cat command displays the contents of the file named by file. If the file is large, all but the last
screenfull of lines will scroll off the screen too quickly to read. To display the file a screenful at a
time, pipe the output to the more or less commands.
cd directory
The cd command moves you (changes your current working directory) to directory. Entering cd
without the directory argument will move you to your home directory.
Examples:
cd /usr/bin
moves you to the bin directory inside of the usr directory.
cd ..
moves you to the parent directory of your current directory.
Back to Common Commands...
chmod permissions path
The chmod command changes the access permission associated with a file or directory ("file" will
be used here to refer to either a file or a directory).
Each file has three types of access: read (r), write (w) and execute (x). In a ls -al file listing, the
abbreviations appear in the columns on the left. To see the contents of a file, the file must have
read (r) permission. To change the contents of a file (e.g. saving changes after editing), write (r)
permission must be enabled. To execute a command by entering the name at the UNIX prompt, the
file must have execute (x) permission. To access a directory, the execute (x) permission must be
granted for that directory. If a given type of access is not permitted, it will show up as a dash (-)
rather than r, w or x in the ls -al listing.
The access to a file can be controlled separately for three sets of users: the owner of the file (u), a
limited group of users (g), and everyone on the system (o). In a ls -al file listing, the first three
columns (starting in column two of the listing) are the r, w and x access allowed for the owner, the
second three are the access allowed for the group and the third three are the access allowed for
everyone else.
Permissions can be specified in numeric format or using the abbreviations above. For the numeric
format, three numbers are specified where each number represents the access granted for one of
the three sets of users. Each permission number is determined by adding up the value associated
with each type of access: r = 4, w = 2 and x = 1. For example, the value 7 grants all access, the
value 5 grants only read and execute access but not write access and the value 0 does not allow
any access to the file. The numeric access specification is an absolute one; all three types of access
for all three sets of users are reset according to the new permissions.
Examples:
chmod 640 .bash_profile
grants read and write permission (6) to the owner of the file, read-only permission to the
group, and no permissions for everyone else. The would appear in a file listing as
rw-r-----.
chmod 777 .
grants all access to the owner, group, and everyone for the current working directory.
The permissions can also be specified using abbreviations rather than numbers. Using this method,
some of the permissions can be changed without affecting others. The permissions format is <+ or -> . The + adds the access indicated to the file without affecting the other
permissions. The - removes the access from the file.
Examples:
chmod u+rw report
adds read and write permission to the file, report, for the owner of the file. Access for the
group and everyone is unchanged.
chmod +x somecommand
adds execute permission to the file, somecommand, for all three sets of users.
chmod go-rwx private.file
removes all access to the file, private.file, for the group and everyone and leaves the owner’s
access unchanged.
See file and directory permissions in UNIX Concepts
chown username path
The chown command changes the ownership of the file or directory, path, to user, username. Both
the user-ownership and the group-ownership may be changed at once by specifying user.group for
the username parameter. One must have root privileges to do this so this command will not be too
useful for most users. If you really want to own someone else’s file that you have read-access to,
make a copy of the file using the command, cp.
cp file1 file2
The cp command creates an identical copy of the file, file1, and names the copy, file2. Note that
the ownership of file2 will be set to whoever does the cp command.
Back to Common Commands...
date
The date command displays the current date and time. Use date -u to see the time in Greenwich
Mean Time (GMT), universal time.
diff file1 file2
The diff command compares the contents of two text files and displays the differences. The lines
preceded with <> are from file2. Each section of differences
is separated by ---.
du options
The du command summarizes disk usage. du entered on the command line with no options will
return the size of all the directories beneath the current working directory. du with the -s option
will return a summary of all the directories suppressing all output but the total. du with the -a
option will return not only the directories but the individual files.
This command will report the amount of the 5 MB of disk space you have used if issued from your
home directory
exit
The exit command terminates the current UNIX shell. If you started this UNIX session (shell) with
either the rlogin or telnet command from within another session, exit returns you to your previous
session. If not, exit usually just closes your window (either an xterm window or terminal program
window) or returns you to a login: prompt.
Back to Common Commands...
chfn [Note: currently disabled]
The chfn, or change finger command allows you to modify the information in the password file
that people can obtain about you using finger.
Information that can be change is:
Name
Office
Office Phone
Home Phone
You may change the information to whatever you like, as often as you like, by telneting to
Camalott and running this program.
This modifies the information that is in the /etc/passwd file that has the permissions to allow it to
be world readable. UNIX reads this file to determine things like who you are, what is your home
directory, the command shell you use, what program or processes you are allowed to run, your
username and your password (which is encrypted for your protection).
finger name@address [Note: currently disabled]
The finger command displays information about user accounts with names (usernames or proper
names) matching name on the machine identified by address. The argument name may be a known
username or a first or last proper name without any spaces although this varies from machine to
machine. When fingering @camalott.com, use the userid, e.g.finger jtpolk@camalott.com.
The finger command may not work if the remote machine is not a UNIX machine (although most
multi-user machines have finger capability these days). If only the @address, portion of the
argument is used, all of the users currently logged into the remote machine may be displayed,
e.g.finger @camalott.com. If no argument is specified, all of the users currently logged into
your machine are shown (see also the who command).
An electronic mail address can often be verified or determined using the finger command. For
example, if you do not know if a person’s address is correct, use the finger command with what
you think is their email address.
If you are concerned about the information that can be obtained by others about you, use the chfn
command to change that information.
fortune
The fortune command has no value other than to give you tidbits of wisdom! e.g.
Leibowitz’s Rule: When hammering a nail, you will never hit your finger if you hold the
hammer with both hands.
"Speed is subsittute fo accurancy."
The more things change, the more they stay insane.
You! What PLANET is this!
-- McCoy, "The City on the Edge of Forever", stardate 3134.0
If you want such wisdom every time you logon camalott.com, simply add it to your .bash_profile
which is read by your command shell (bash) when you login to camalott.com.
ftp hostname
The ftp command will connect you to another computer to either get a file(s) from the remote
machine or to put a file(s) onto a remote machine.
Once ftp has made a connection with the remote machine it will prompt your for your password
and username, just like you would with telnet. If you do not have an account on that machine.,
then login in with anonymous as your password and your email address in the form of username@
hostname as your password. This is known as anonymous ftp.
ftp> Once at this prompt the commands are:
ascii for enabling text transfers.
binary for enabling binary transfers (for .exe, .zip, .jpg, .gif, etc)
get file to get a file from the remote machine to your machine.
put file to put a file from your machine to the remote machine.
cd directory to change directory on the remote machine. See cd.
lcd directory to change the directory on the local machine.
Note: lcd followed by a period ’’.’’ will report the current working directory on the local
machine.
pwd to display the full path of your current working directory on the remote machine. In
other words, it tells you what directory you are in.
ls lists the contents of the current working directory on the remote machine. See ls.
! ls or ! dir will list the files on the local machine in the current working directory.
? will list all the commands available with your ftp program.
close will close the current connection but not end ftp allowing you to use the open
command to connect to another remote machine.
open hostname from the ftp> prompt will open a ftp session with a remote machine.
quit closes the connection with the remote machine and ends the ftp program.
Little known tip: To see the contents of a text file on a remote machine without having to
download it first to the local machine, the command is:
get filename -.
The ’’-’’ pipes the contents of the file to the screen.
grep pattern file [Note: currently disabled]
The grep command searches one or more files, specified by file, for the text string specified by
pattern. For example, grep ’dna’ seqfile1 will list all lines found in the file, seqfile1, which
contain the text "dna". Grep is usually used to search for text in several files at once, e.g. grep
’dna’ * will perform the same search on all the files in your current directory (see wildcard
characters for an explanation of *).
head file
tail file
The head and tail commands list the first (head) or last (tail) ten lines of your file. The number of
lines listed may be varied by including -n number-of-lines option. For example, head -n 50
report1 will list the first fifty lines of the file report1.
kill id-number
The kill command terminates the process with the id, id-number. The process id can be determined
with the ps command. Generally, the kill command is the last method tried to terminate a running
program. If a normal kill (a kill without any kill-level option specified before the id-number) does
not remove the process, including the -9 option will usually annihilate it (unless it is a zombie
process -- in which case it doesn’t exist so it can’t be killed (confused yet?)) e.g. kill -9 18201
is a "sure kill" of process number 18201.
less file1
The less command displays the contents of the text file, file1, a screenful at a time, pausing at the
end of each screen until the user presses one of a few special keys. When the display pauses, the
user may press:
to display to the next page of text,
b to display to the previous page of text,
to search for that word in the remaining text of the
file,
n to find the next occurrence of that word,
> to go all the way to the bottom of the file,
<> to display to the next page of text,
to display only one more line of text,
/ followed by a word and the to search for that word in the remaining text of the
file,
? to see all the operations available in more,
q or CONTROL-C to quit displaying the file and return to the command prompt.
more may also be used at the end of a "pipe" to cause the output from another command to be
paused a screen at a time. This is useful when a UNIX command produces a lot of output too fast
for the user to see before the output scrolls off the screen. For example, the ls -al command often
produces long directory listings which are better viewed a screen at a time by entering ls -al |
more.
For a more powerful version of this command see less.
mv name1 name2
The mv command moves and/or renames the file or directory, name1. If name2 is an existing
directory, name1 will be moved (not copied) into the directory, name2. If name2 is not an existing
directory, name1 (whether it be a file or directory) will simply be renamed into name2. It is
possible to move and rename name1 at the same time if name2 is a new name within an existing
directory. Any file file already existing with name2 will be overwritten by the renaming or moving
of name1 unless mv -i is used, which asks the user for confirmation of destruction of existing
files.
Back to Common Commands...
passwd
The passwd command changes your UNIX login password. After entering the command passwd,
you will be asked to enter your old password, then the new password that you want to change to,
and then the same new password again. You are asked to enter the new password twice to assure
that you have typed it correctly. None of the passwords you enter will be displayed on the screen
as you type them and your password is encrypted for your protection.
Camalott Users will want to read special instructions.
ps
The ps command displays a list of the processes currently running on the machine that you are
logged into. If no arguments are entered with the ps command, only the "important" processes,
that you own (i.e. that you are running) are displayed. The -a option includes processes owned by
others in the list. The -g option includes all processes, not just "important" processes. The -u
option provides more information for each processes than is printed by default.
Examples (note that the dash in a ps option is not required):
ps
List all the processes running that you own (minimal information).
ps u
List all the processes running that you own (lots of information).
ps a
List all the processes running on this machine no matter who owns them.
To terminate a process, see the kill command.
pwd
The pwd command displays the full path of your current working directory. In other words, it tells
you what directory you are in.
Back to Common Commands...
rlogin address This command has largely been superceded by telnet. See it instead.
The rlogin command logs into another machine specified by address.
The -l username option allows you to specify a username to attempt the remote login with. If this
option is omitted, your current username is used by default. Examples:
rlogin camalott.com
logs you into the machine named camalott.com with your current username
rlogin camalott.com -l smith
logs you into the machine named camalott.com as user smith (of course, you must know smith’s
password). You can close the connection from the remote machine by entering the command,exit.
rm yourfile
The rm command removes the file, yourfile, permanently from the filesystem. This cannot be
undone so be careful when using wildcards in the file specification. Use rm -i to force
user-confirmation of each file to be removed. Using rm -r somedir will remove the subdirectory
somedir as well as all the subdirectories and files within that subdirectory.
Note: Be careful! If you mean a to delete bunch of files with the extension of *.bak using the
command of rm *.bak and instead enter rm * .bak (adding a space between the wildcard ’’*’’ and
the file name extension .bak) it will first remove ALL files then go looking for a file named .bak
and issue an error message that it cannot find it.
rmdir dir1
The rmdir command deletes the empty subdirectory, dir1. To delete non-empty subdirectories,
see rm -r.
talk name@address
The talk command allows you communicate with another user using your terminal. When first
called, talk sends the message:
Message from TalkDeamon@his_machine...
talk: connection requested by your_name@your_machine.
talk: respond with: talk your_name@your_machine.
And they respond with:
talk your_name@your_machine
To end the session hit CTRL-C.
To find out if someone you wish to talk to is available use the who or finger commands.
tail file
See head.
telnet address
The telnet command logs into another machine specified by address.
The -l username option allows you to specify a username to attempt the remote login with. If this
option is omitted, your current username is used by default. Examples:
telnet camalott.com
logs you into the machine named camalott.com with your current username
telnet camalott.com -l smith
logs you into the machine named camalott.com as user smith (of course, you must know smith’s
password). You can close the connection from the remote machine by entering the command,exit.
vi yourfile
vim yourfile
The editor vi is on every UNIX machine, so it should be mentioned. It’s also known, on Linux
machines as elvis, and there is a version called vim (Vi IMproved). I perfer vim over vi (aka elvis)
because there is some on-line help. Before using either vi or vim, I would strongly recommend
that one telnet into camalott.com and issue the following command:
vim /u/b/bbcomp/vitutor/tutor
It is a short tutorial on the editor(s) and almost all the basic commands are the same. There are
things you have to know first.
The editor has two modes, command mode and insert mode. One allows you to enter
commands, like to quit, save the file, etc, and the other allows you to enter text.
To switch to command mode, you hit the escape key until it beeps.
To enter insert mode, you hit the letter i.
To save a file you are editing, you enter command mode and then hit the : (colon) key, then
the letter w and the enter key
:w
To save a file you are editing and quit the program, you enter command mode and then hit
the : (colon) key, then the letters wq and the enter key
:wq
To quit the program you enter command mode and then hit the : (colon) key then the letters
q! and the enter key
:q!
On line help for vim is accessed by entering the command mode and then hit the : (colon)
key, then h and the enter key
:h
This is the editor that you want to use for editing your .bash_profile and other dot files in you
home directory. See the also man pages for either elvis or vim.
wc yourfile
The wc command counts the number of words or characters and lines in your file. If the -l option
is used, only the number of lines is counted.
who
The who command displays a list of who is logged on to the system and where they are logged on
from. See also finger.

No comments:

Horoscope