Quick Unix Tutorial.doc
Short Description
Download Quick Unix Tutorial.doc...
Description
Quick Unix Tutorial Examples of Basic Commands Action appent to file
Command cat >> cat >>
Examples cat >> cat >> file1
combine 2 files cat
cat file1 file2 > file3
copy files
cp
cp myfile copymyfile
create a file
cat
cat > newfile
edit files
vi
vi file
list files
ls
ls bin/
move a file
mv
mv file1 doc/chapter1
remove a file
rm
rm unwantedfile
rename a file
mv
mv oldfilename ne wfilename
view files
cat pg more less view
cat file cat file pg file2 file3 view file6 file7
Directories
Command
Examples
change to another directory cd
cd example/first/
create a directory
mkdir
mkdir example1
find out where you are
pwd
pwd
go to your home directory
cd
cd
remove an emplty eirectory rmdir
rmdir jun
Redirection of Output or nput ! redirects the output of a command command to a file !! redircts the output of a command to to the end of an existing file " taes the the input of a command form form a file! not the the terminal terminal
#ummar$ of Basic Commands • • •
apropos apropos"1# apropos"1# locate locate commands by eyword looup arc% arch"1$# display the architecture of the current host cal cal"1# cal"1# display display a calendar cal %month& year o month number between 1 and 12 o year number between 1 and '''' Examples& cal '(() print calendar for year 1''6 cal ' '((* print calendar for (anuary 1''7
• •
• • • • • • • • • • • •
• • • • •
•
• • • • • • • • •
cancel lp"1# lp"1# send/cancel send/cancel re)uests to an *+ print service cat cat"1# cat"1# concatenate concatenate and display files ",o ",o view files! create files! append to files and combine files# cat %options& %files& Examples& cat files read file"s# cat ! file create file "reads form terminal- terminate input with .# cat !! file append to file "reads form terminal- terminate input with .# file2 to to file1 file1 cat file+ !! file' appends contents of file2 cd cd"1# shell built0in functions to change the current woring directory c%dir cd"1# c%dir cd"1# shell shell built0in functions to change the current woring directory c%grp chgrp"1# chgrp"1# change change the group ownership of a file c%mod chmod"1# chmod"1# change change the permissions mode of a file c%own chown"1# chown"1# change change owner of file clear clear"1# clear clear"1# clear clear the terminal screen cp cp"1# copy files date date"1# date"1# print print and set the date dc dc"1# arbitrary precision destop calculator dos+unix dos2unix"1# dos2unix"1# convert convert text file from format to format e,ect eject"1# eject"1# eject eject media such as 405 and floppy from drive exit exit"1# exit"1# shell shell built0in functions to enable the execution of the shell to advance beyond its se)uence of steps file file"1# file"1$# file"1$# determine determine the type of a file by examining its contents %ead head"1# head"1# display display first few lines of files lp lp"1# lp"1# send/cancel send/cancel re)uests to an *+ print service lpstat lpstat"1# lpstat"1# print print information about the status of the *+ print service ls ls"1# ls"1$# list the contents of a directory ls %options& %directories& the current woring directory used if no directories specified few options8 o 0a list all entries includeing hidden files files "starting with 9# o 0i print inode numbers o 0l long list "mode! lins! lins! owner! owner! group! group! si:e! timeof last last modification! modification! and name o 0t sort by modification time o 0x multi0column list! sorted across each row -ail! -ail! mailx mailx"1# mailx"1#!! mail mail"1# rmail interactive rmail interactive message processing system to read mail or send mail to users mail %options& users Examples& mail with no options! to read your mail mail user to send mail to user mail user ; filename mail a file to another user mkdir mdir"1# mkdir mdir"1# mae mae directories more more"1# browse or page through a text file mv mv"1# mv"1# move move files nispasswd"1# change change ou can use the rmdir command to remove a directory $ma"e sure it is empty first& -ry to remove the backups directory >ou will not be able to since UNIX will not let you remove a non#empty directory
01ercise ,b 3reate a directory called tempstuff using mkdir , then remove it using the rmdir command
,$2 Displaying the contents of a file on the screen clear (clear screen) !efore you start the net section, you may li"e to clear the terminal window of the previous commands so the output of the following commands can be clearly understood
7t the prompt, type
% clear
-his will clear all tet and leave you with the 8 prompt at the top of the window
cat (concatenate) -he command cat can be used to display the contents of a file on the screen -ype
% cat science.txt
7s you can see, the file is longer than than the siCe of the window, so it scrolls past ma"ing it unreadable
less -he command less writes the contents of a file onto the screen a page at a time -ype
% less science.txt
4ress the space+bar: if you want to see another page, and type 9: if you want to *uit reading 7s you can see, less is used in preference to cat for long files
head -he head command writes the first ten lines of a file to the screen
2irst clear the screen then type
% head science.txt
-hen type
% head - science.txt
(hat difference did the #D do to the head commandB
tail -he tail command writes the last ten lines of a file to the screen
3lear the screen and type
% tail science.txt
E )ow can you view the last 1D lines of the fileB
,$! Searching the contents of a file
Simple searching using less Using less, you can search though a tet file for a "eyword $pattern& 2or eample, to search through science$t1t for the word :science:, type
% less science.txt
then, still in less, type a forward slash %: followed by the word to search
/science
7s you can see, less finds and highlights the "eyword -ype n: to search for the net occurrence of the word
grep (don:t ask 4hy it is called grep) !rep is one of many standard UNIX utilities It searches files for specified words or patterns 2irst
clear the screen, then type
% !rep science science.txt
7s you can see, !rep has printed out each line containg the word science
r has it BBBB
-ry typing
% !rep "cience science.txt
-he !rep command is case sensitive5 it distinguishes between .cience and science
-o ignore upper/lower case distinctions, use the #i option, ie type
% !rep -i science science.txt
-o search for a phrase or pattern, you must enclose it in single *uotes $the apostrophe symbol& 2or eample to search for spinning top, type
% !rep -i #spinnin! top# science.txt
.ome of the other options of grep are
+# display those lines that do N- match +n precede each matching line with the line number +c print only the total count of matched lines
-ry some of them and see the different results ou should now have two files ne contains si fruit, the other contains four fruit
(e will now use the cat command to @oin $concatenate& list" and list, into a new file called biglist -ype
% cat list list $ bi!list
(hat this is doing is reading the contents of list" and list, in turn, then outputing the tet to the file biglist
-o read the contents of the new file, type
% cat bi!list
.$. 7edirecting the Input (e use the J symbol to redirect the input of a command
-he command sort alphabetically or numerically sorts a list -ype
% sort
-hen type in the names of some animals 4ress Aeturn: after each one
do! cat bird ape &' control d to stop
-he output will be
ape bird cat do!
Using J you can redirect the input to come from a file rather than the "eyboard 2or eample, to sort the list of fruit, type
% sort 0 bi!list
and the sorted list will be output to the screen
-o output the sorted list to a file, type,
% sort 0 bi!list $ slist
Use cat to read the contents of the file slist
.$2 3ipes -o see who is on the system with you, type
% who
ne method to get a sorted list of names is to type,
% who $ names.txt % sort 0 names.txt
-his is a bit slow and you have to remember to remove the temporary file called names when you have finished (hat you really want to do is connect the output of the who command directly to the input of the sort command -his is eactly what pipes do -he symbol for a pipe is the vertical bar K
2or eample, typing
% who 1 sort
will give the same result as above, but *uic"er and cleaner
-o find out how many users are logged on, type
% who 1 wc -l
01ercise .b Using pipes, display all lines of list" and list, containing the letter +p+, and sort the result
7nswer available here
Summary
/ommand
-eaning
$ file command
redirect standard output to a file
$$ file command
append standard output to a file
0 file command
redirect standard input from a file
command1 1 command2
pipe the output of command1 to the input of commandF
cat file1 file2 $ file0 concatenate file1 and fileF to file0
sort
who
sort data
list users currently logged in
'.toneban"Gsurreyacu", H 9th ctober F000
UNIX Tutorial Four 2$" Wildcards The = 4ildcard -he character 2 is called a wildcard, and will match against none or more character$s& in a file $or directory& name 2or eample, in your uni1stuff directory, type
% ls list2
-his will list all files in the current directory starting with list$$$$
-ry typing
% ls 2list
-his will list all files in the current directory ending with $$$$list
The ? 4ildcard -he character 3 will match eactly one character .o ?ouse will match files li"e house and mouse, but not grouse -ry typing
% ls 3list
2$, Filename con#entions (e should note here that a directory is merely a special type of file .o the rules and conventions for naming files apply also to directories
In naming files, characters with special meanings such as % = > , should be avoided 7lso, avoid using spaces within names -he safest way to name a file is to use only alphanumeric characters, that is, letters and numbers, together with L $underscore& and $dot&
@ood filenames
Aad filenames
pro@ecttt
pro@ect
myLbigLprogramc
my big programc
fredLdavedoc
fred M davedoc
2ile names conventionally start with a lower#case letter, and may end with a dot followed by a group of letters indicating the contents of the file 2or eample, all files consisting of 3 code may be named with the ending $c, for eample, prog"$c -hen in order to list all files containing 3 code in your home directory, you need only type ls 2.c in that directory
2$. @etting Belp &n+line -anuals -here are on#line manuals which gives information about most commands -he manual pages tell you which options a particular command can ta"e, and how each option modifies the behaviour of the command -ype man command to read the manual page for a particular command
2or eample, to find out more about the wc $word count& command, type
% man wc
7lternatively
% whatis wc
gives a one#line description of the command, but omits any information about options etc
B
'ore eamples of environment variables are
•
U.;A $your login name&
•
)'; $the path name of your home directory&
•
).- $the name of the computer you are using&
•
7A3) $the architecture of the computers processor&
•
$the name of the computer screen to display X windows&
•
4AIN-;A $the default printer to send print @obs&
•
47-) $the directories the shell should search to find a command&
Finding out the current #alues of these #ariables$
;NIAN';N- variables are set using the setenv command, displayed using the printenv or env commands, and unset using the unsetenv command
-o show all values of these variables, type
% printenv 1 less
M$. Shell ariables 7n eample of a shell variable is the history variable -he value of this is how many shell commands to save, allow the user to scroll bac" through all the commands they have previously entered -ype
% echo Lhistory
'ore eamples of shell variables are
•
cwd $your current wor"ing directory&
•
home $the path name of your home directory&
•
path $the directories the shell should search to find a command&
•
prompt $the tet string used to prompt for interactive commands shell your login shell&
Finding out the current #alues of these #ariables$ .); variables are both set and displayed using the set command -hey can be unset by using the unset command
-o show all values of these variables, type
% set 1 less
So 4hat is the difference bet4een 3 set the value of history, you will need to add the set command to the cshrc file
2irst open the $cshrc file in a tet editor 7n easy, user#friendly editor to use is nedit
% nedit ~/.cshrc
7dd the following line 72-;A the list of other commands
set history V F00
.ave the file and force the shell to reread its cshrc file buy using the shell source command
% source .cshrc
3hec" this has wor"ed by typing
% echo Lhistory
M$5 Setting the path (hen you type a command, your path $or 47-)& variable defines in which directories the shell will loo" to find the command you typed If the system returns a message saying =command 3ommand not found=, this indicates that either the command doesn+t eist at all on the system or it is simply not in your path
2or eample, to run units, you either need to directly specify the units path $6%units"K2%bin%units&, or you need to have the directory 6%units"K2%bin in your path
>ou can add it to the end of your eisting path $the Lpath represents this& by issuing the command
% set path K Lpath ~/unitsE6/bin
-est that this wor"ed by trying to run units in any directory other that where units is actually located
% cd % units
-o add this path 4;A'7N;N->, add the following line to your cshrc 72-;A the list of other commands
set path K Lpath ~/unitsE6/bin
View more...
Comments