Unix Commnds
Unix Commnds
-----------------------------------------
[U
| computing service university of cambridge > computing
service > information & documentation
index of commands
cat - display or concatenate files mkdir - make a directory
cd - change directory more - scan through a text file page by page
chmod - change the permissions on a file or mv - move or rename files or directories
directory nice - change the priority at which a job is
cp - copy a file being run
date - display the current date and time passwd - change your password
diff - display differences between text files ps - list processes
file - determine the type of a file pwd - display the name of your current
find - find files of a specified name or type directory
ftp - file transfer program quota - disk quota and usage
grep - searches files for a specified string or rm - remove files or directories
expression rmdir - remove a directory
gzip - compress a file sort - sort and collate lines
help - display information about bash builtin slogin - secure remote login program
commands telnet - remote login program
info - read online documentation
kill - kill a process
lpr - print out a file
ls - list names of files in a directory
man - display an on-line manual page
creates a new file newex containing copies of ex1 and ex2, with the contents of ex2
following the contents of ex1.
cd - change directory
cd is used to change from one directory to another.
cd dir1
changes directory so that dir1 is your new current directory. dir1 may be either the
full pathname of the directory, or its pathname relative to the current directory.
cd
sets the permissions on the file file1 to give the user read and write permission on
file1. no other permissions are altered.
alters the permissions on the file file1 to give the user execute permission on file1,
to give members of the user's group write permission on the file, and prevent any
users not in this group from reading it.
chmod u+w,go-x dir1
gives the user write permission in the directory dir1, and prevents all other users
having access to that directory (by using cd. they can still list its contents using ls.)
cp - copy a file
the command cp is used to make copies of files and directories.
cp file1 file2
copies the contents of the file file1 into a new file called file2. cp cannot copy a
file onto itself.
cp file3 file4 dir1
creates copies of file3 and file4 (with the same names), within the directory dir1.
dir1 must already exist for the copying to succeed.
cp -r dir2 dir3
recursively copies the directory dir2, together with its contents and subdirectories, to
the directory dir3. if dir3 does not already exist, it is created by cp, and the contents
and subdirectories of dir2 are recreated within it. if dir3 does exist, a subdirectory
called dir2 is created within it, containing a copy of all the contents of the original
dir2.
it is possible to alter the format of the output from date. for example, using the command
line
date '+the date is %d/%m/%y, and the time is %h:%m:%s.'
there are several options to diff, including diff -i, which ignores the case of letters
when comparing lines, and diff -b, which ignores all trailing blanks.
diff -cn
produces a listing of differences within n lines of context, where the default is three
lines. the form of the output is different from that given by diff, with + indicating
lines which have been added, - indicating lines which have been removed, and !
indicating lines which have been changed.
diff dir1 dir2
will sort the contents of directories dir1 and dir2 by name, and then run diff on the
text files which differ.
can tell if file1 is, for example, a source program, an executable program or shell
script, an empty file, a directory, or a library, but (a warning!) it does sometimes
make mistakes.
searches the current directory and all its subdirectories for files ending in .f, and
writes their names to the standard output. in some versions of unix the names of the
files will only be written out if the -print option is used.
find /local -name core -user user1 -print
searches the directory /local and its subdirectories for files called core belonging to
the user user1 and writes their full file names to the standard output.
ftp - file transfer program
ftp is an interactive file transfer program. while logged on to one machine (described as
the local machine), ftp is used to logon to another machine (described as the remote
machine) that files are to be transferred to or from. as well as file transfers, it allows the
inspection of directory contents on the remote machine. there are numerous options and
commands associated with ftp, and man ftp will give details of those.
a simple example ftp session, in which the remote machine is the central unix service
(cus), is shown below:-
ftp cus.cam.ac.uk
if the connection to cus is made, it will respond with the prompt:-
name (cus.cam.ac.uk:user1) :
(supposing user1 is your username on your local machine). if you have the same
username on cus, then just press return; if it is different, enter your username on cus
before pressing return. you will then be prompted for your cus password, which will not
be echoed.
after logging in using ftp you will be in your home directory on cus some unix
commands, such as cd, mkdir, and ls, will be available. other useful commands are:
help
creates a copy on your local machine of the file remote1 from cus. on your local
machine this new file will be called local1. if no name is specified for the file on the
local machine, it will be given the same name as the file on cus.
send local2 remote2
copies the file local2 to the file remote2 on cus, i.e. it is the reverse of get.
quit
finishes the ftp session. bye and close can also be used to do this.
some machines offer a service called "anonymous ftp", usually to allow general access
to certain archives. to use such a service, enter anonymous instead of your username
when you ftp to the machine. it is fairly standard practice for the remote machine to ask
you to give your email address as a password. once you have logged on you will have
read access in a limited set of directories, usually within the /pub directory tree. it is good
etiquette to follow the guidelines laid down by the administrators of the remote machine,
as they are being generous in allowing such access. see the computing service file
transfer pages for more detailed examples of using ftp.
when you use ftp the communications between the machines are not encrypted. if
available, the commands sftp (secure file transfer program) or scp (secure remote file
copy program) provide encrypted file transfer.
searches the file file1 for lines containing the pattern motif1. if no file name is
given, grep acts on the standard input. grep can also be used to search a string of
files, so
grep motif1 file1 file2 ... filen
will search the files file1, file2, ... , filen, for the pattern motif1.
grep -c motif1 file1
will give the number of lines containing motif1 instead of the lines themselves.
grep -v motif1 file1
will write out the lines of file1 that do not contain motif1.
compresses file2 and gives information, in the format shown below, on the
percentage of the file's size that has been saved by compression:-
file2 : compression 50.26 -- replaced with file2.gz
to restore files to their original state use the command gunzip. if you have a compressed
file file2.gz, then
gunzip file2
will send the file file1 to be printed out on the printer printer1. to see the status of
the job on the printer queue use
lpq -pprinter1
for a list of the jobs queued for printing on printer1. (this may not work for remote
printers.)
lists the names of the files and directories in the directory dir1, (excluding files
whose names begin with . ). if no directory is named, ls lists the contents of the
current directory.
ls -a dir1
will list the contents of dir1, (including files whose names begin with . ).
ls -l file1
gives details of the access permissions for the file file1, its size in kbytes, and the
time it was last altered.
ls -l dir1
gives such information on the contents of the directory dir1. to obtain the
information on dir1 itself, rather than its contents, use
ls -ld dir1
will display the manual page for command1, e.g man cp, man man.
man -k keyword
lists the manual page subjects that have keyword in their headings. this is useful if
you do not yet know the name of a command you are seeking information about.
man -mpath command1
is used to change the set of directories that man searches for manual pages on
command1
mkdir -p can be used to create a new directory, together with any parent directories
required.
mkdir -p dir1/dir2/newdir
will create newdir and its parent directories dir1 and dir2, if these do not already
exist.
starts by displaying the beginning of file1. it will scroll up one line every time the
return key is pressed, and one screenful every time the space bar is pressed. type ? for
details of the commands available within more. type q if you wish to quit more before
the end of file1 is reached.
more -n file1
will cause n lines of file1 to be displayed in each screenful instead of the default
(which is two lines less than the number of lines that will fit into the terminal's
screen).
changes the name of a file from file1 to file2 unless dir2 already exists, in which
case dir1 will be moved into dir2.
mv dir1 dir2
moves the files file1 and file2 into the directory dir3.
ps - list processes
ps displays information on processes currently running on your machine. this information
includes the process id, the controlling terminal (if there is one), the cpu time used so far,
and the name of the command being run.
ps
will only give details of where you have exceeded your disc quota on local disks,
whereas
quota -v
will display your quota and usage, whether the quota has been exceeded or not, and
includes information on disks mounted from other machines, as well as the local
disks.
instead, you will be asked if you wish to delete file1, and the file will not be deleted
unless you answer y. this is a useful safety check when deleting lots of files.
rm -r dir1
recursively deletes the contents of dir1, its subdirectories, and dir1 itself, and should
be used with suitable caution.
rmdir - remove a directory
rmdir removes named empty directories. if you need to delete a non-empty directory rm
-r can be used instead.
rmdir exdir
uses "dictionary order", in which only letters, digits, and white-space characters are
considered in the comparisons.
sort -r
sorts lines according to the arithmetic value of leading numeric strings. leading blanks
are ignored when this option is used, (except in some system v versions of sort,
which treat leading blanks as significant. to be certain of ignoring leading blanks use
sort -bn instead.).
you can then login using your user name on cus.cam.ac.uk. if you use the escape
character instead, you will enter telnet's command mode (you'll get the prompt
telnet > ), and the command quit will get you back to the command line of your
local machine.
as communications between the two machines are not encrypted when using telnet, it
is preferable to use ssh, if that is available.
the information in this document may be available in other forms. please telephone the
information group coordinator on 334598 or e-mail documentation@ucs.cam.ac.uk to
discuss what is available, any special requirements for presentation and how we could
meet them.
url of this document: http://www.cam.ac.uk/cs/docs/leaflets/u5/index.html
for more documentation and faqs see http://www.cam.ac.uk/cs/docs/
© 2007 university of cambridge computing service
information provided by documentation@ucs.cam.ac.uk