Simple Linux Tutorial by Alan Bates

advertisement
Alan’s Quick Guide of Useful Things to Type at a UNIX/LINUX Command Prompt
Alan Bates
03/10/02 – adapted from UBC ERP manual by same author
References:
Redhat Linux 7.1 Bible by Christopher Negus
Unix for the Impatient Second Edition by Paul Abrahams and Bruce Larson
Unix Unleashed Fourth Edition by Robin Anderson and Andy Johnston
Linux+ Study Guide by Roderick Smith
There are also many other books about UNIX and LINUX available from the university
libraries.
Also, you will find a huge list of UNIX and LINUX tutorials on the internet if you enter
unix tutorial or linux tutorial into Google or some other search engine. There are so
many that it is not worth me listing a few here because I don’t have a favourite.
What are UNIX and LINUX?:
UNIX and LINUX are operating systems like Microsoft Windows. Basically, they are
software that provide an environment where you can easily use other software and
control some things about the computer’s hardware. Although most new versions of
UNIX and LINUX do have “Windows-like” environments where you can point and click
on files, the biggest difference you will notice between them and Windows is that it is
most efficient to type commands at a command prompt in UNIX and LINUX, kind of
like in DOS (if you remember it), rather than point and click on things with a mouse.
UNIX was first developed at AT&T Bell Laboratories in New Jersey, USA as an
operating system that could do many things that Microsoft operating systems couldn’t. It
was (and still is) particularly better for developing networks, allowing many users to
simultaneously use the same computer, allowing many programs to run at the same time
and many other things. Today, there are many different versions of UNIX such as Solaris
for SUNs and HP-UX for HP UNIX computers and they are all very similar, but each
have their own “special” features. LINUX was first developed by Linus Torvalds and
you can think of it as a version of UNIX that’s free. Today there are many different
distributions of LINUX which are essentially collections of free software including a
LINUX kernel which is its central component.
Useful things to type at a LINUX command prompt:
man - The first command I will tell you about is man because it allows you to find out
lots of information about almost all of the other commands you can use. So, if you type
man and then any of the other commands below, you will get the manual pages for the
command. You can scroll through man pages one line at a time with enter or the down
arrow or you can scroll a page at a time with the space bar. To exit man pages, press q.
Since we haven’t learned any other commands yet, try typing man man (and then enter).
This will give you the manual pages for the man command.
whoami - whoami tells you what account you are logged in as. This is more important
than it sounds because each user only has complete access to their own files (unless you
are logged in as root or the superuser in which case you have access to everything) and
sometimes to files that have been created by other users who are in their “group”. So, if
you do a lot of work as another user, you will likely find that you can’t access what you
did as yourself. Groups are created by a system administrator and you don’t have to
worry about them unless you decide you want one to exist because you are often sharing
files with another user.
pwd - Print working directory. pwd is basically the whereami command. It will tell you
what directory you are in. For example, if it says /home/alan/great_data, you are in the
great_data directory in the alan directory in the home directory on the / or root partition
of a hard disk. Notice that unlike DOS and Windows, directory trees use forward slashes
(/) instead of backslashes (\).
. - A single dot is shorthand for the directory that you are currently in. See examples of
use below.
.. - Two dots is shorthand for the directory above the one that you’re currently in. See
examples of use below.
cd - Change directory. If you don’t want to be where you are, use cd to go somewhere
else.
examples: cd .. (go one directory up/back), cd /broca/data5 (go to /broca/data5), cd
erpdata (go to a directory called erpdata that is in your current directory, notice there is
no forward slash before erpdata)
ls - List. Of course, cd requires that you know where to cd to, so you may want to see
what’s in some directory. ls lists most of the files and directories that are in some
directory.
examples: ls (lists most of the files and directories in your current directory), ls .. (lists
files and directories in the directory above your current directory, ls /data/fmri (lists
things in /data/fmri), ls ../erp (lists things in the directory called erp that is in the directory
above your current directory)
ls -la - This is just the ls command with two activated options. It’s useful because it
gives a more descriptive list of the files and directories in your current directory and also
lists “hidden” files.
mkdir - Make directory
examples: mkdir erpdata (makes a directory called erpdata in your current directory),
mkdir ../erpdata (makes a directory called erpdata in the directory that is one up/back
from your current directory)
rm - Remove. Removes a file. Once a file has been removed it is permanently gone.
There is no recycle bin. A good habit is to ls what you are planning to rm and check it
over.
examples: rm readme.txt (removes the file readme.txt)
rmdir - Remove directory. Removes a directory. The directory should be empty before
you use this command. Directories and all of their files can be removed recursively (all
at once) by implementing certain options of the rm command, but only experienced users
should try using such commands because they can easily destroy gigabytes of data
example: rmdir erpdata (removes a directory called erpdata in your current directory),
rmdir ../test/erpdata (removes a directory called erpdata that is in a directory called test
which is in the directory above your current directory)
cp - Copy. Copy a file or directory.
examples: cp readme.txt exciting_literature.txt (makes a copy of readme.txt, calls it
exciting_literature.txt and puts it in your current directory), cp readme.txt .. (puts a copy
of readme.txt in the directory above your current directory), cp readme.txt
/broca/data6/cool.txt (makes a copy of readme.txt in /broca/data6 and calls it cool.txt)
cp -r - Recursively copy.
example: cp -r some_directory another_directory (copies the directory called
some_directory and all of the files in it and names the new copy another_directory)
mv - Move. Move a file or directory
examples: mv thesis.phd revised_thesis.phd (makes a copy of thesis.phd in your current
directory and calls it revised_thesis.phd, also removes the original thesis.phd), mv
thesis.phd erpdata (if erpdata is a directory, moves thesis.phd into that directory)
gedit - Brings up a text editor called gedit. If you are using UNIX and not LINUX, try
dtpad or vi. Text editors are useful for keeping logs of your work. They are also useful
for writing scripts or batchfiles. A batchfile is basically just a big list of individual
commands (one command on each line). Once saved, batchfiles can be executed with the
csh command (see below) as long as permissions have been set so that you can execute
them (see chmod below).
example: gedit readme.txt (creates a text file called readme.txt and opens it up for
editing)
chmod - allows you to change file permissions
example: chmod +x p300.bat (allows you to e(x)ecute p300.bat as a batch file), chmod
a+w analyze.txt (allows (a)ll users to (w)rite to analyze.txt), chmod g-w readme.txt (takes
(w)rite permission away from your (g)roup), chmod a=rw (gives (a)ll users (r)ead and
(w)rite permission but not execute permission)
ls -la will show you what the permissions are for the files in a directory. The first three
letters are for the user, the next three are for the user’s group and the last three are for
others. Here are some examples:
rwxrwxrwx everybody has all permissions
rwxrwxr-x others do not have write permission
rwxrw-rw- the group and others don’t have execute permission
r-------- you only have read permission and (and that’s why you can’t move it, write to it,
execute it etc., this is a very common problem)
Here is a very useful table that I stole from Unix for the Impatient and changed a bit to
help you interpret permissions:
u Permissions for the file’s user
g Permissions for the file’s group
o Permissions for others
a Permissions for everyone
+ add these permissions
- take away these permissions
= set exactly these permissions
r Read
w Write
x Execute
start
--------rw------rwxrwxrwx
rwxrwxrwx
rwxrwx--rwxrwx---
command
chmod a=rw file.txt
chmod go+r file.txt
chmod a-x file.txt
chmod g-w, o-wx file.txt
chmod o=g file.txt
chmod o=g-xw file.txt
result
rw-rw-rwrw-r--r-rw-rw-rwrwxrx-r-rwxrwxrwx
rwxrwxr--
csh - executes a file that you have execute permission for
example: csh p300.bat (executes p300.bat, runs each command listed in the file in order)
./ - same function as csh but you don’t leave a space after it
example: ./p300.bat (executes p300.bat, runs each command listed in the file in order)
Tab key - The tab key provides a quick way to complete some piece of text that you’re
typing. For example, if there is a file called reallylongfilename.txt in the directory you’re
in and you type gedit re and then hit the Tab key it will complete the filename for you as
long as there are no other files or directories in that directory that begin with re. (In
Solaris, this is often the escape key)
up arrow key - Use the up arrow key to scroll through commands that you have used
previously. This is very useful if you are using many commands that only change by a
few characters or if you made a small mistake in the last thing you entered.
& - When added after a set of commands, it allows you to keep your terminal window
active.
example: gedit readme.txt & (creates a text file called readme.txt, opens it up for editing
and keeps your command prompt in your terminal window active)
the left and middle mouse buttons - you can highlight text with the left mouse button
and then paste a copy of it anywhere you want with the middle mouse button, be careful
not to erase the original text by hitting enter while the original text is still highlighted, if
you do, you can go to Edit ---> Undo to reverse your mistake
* - Wildcard. Represents any string of characters.
examples: mv *.avg erpdata (moves all files ending in .avg to a directory called erpdata),
cp subject*.* erpdata (copies everything beginning with subject that has a . in it to a
directory called erpdata)
telnet - Allows you to remotely log into another computer that is running a telnet service.
example: telnet granby.nottingham.ac.uk
ftp - file transfer protocol, a fast way to transfer files from one computer to another
example: when you are in the directory that you want to work with on the computer you
are currently logged into, type ftp granby.nottingham.ac.uk, login, type bin and hit enter
(specifies binary transfer), type prompt and hit enter (disables prompting for each file
transferred), change to the directory that you want to work with on granby, to move a file
to granby type put example_file.avg, to move a file from granby to the computer you
ftp'ed from type get example_file.avg, use mput to upload multiple files at the same time,
use mget to download multiple files at the same time, use lcd to change directories on the
computer you ftp’ed to, type quit when you are finished transferring files
ssh - Secure shell. Like telnet but more secure.
sftp - Secure ftp. Similar to ftp but more secure.
xhost – (***I’m not 100% sure that the example below works***) xhost allows you to
use another computer and have all of the displays sent to the monitor that you are sitting
in front of.
example: sitting at computer1, type xhost computer2.nottingham.ac.uk, type telnet
computer2.nottingham.ac.uk, login to computer2, type
DISPLAY=computer1.nottingham.ac.uk:0.0, type export DISPLAY, then type start kde,
you should now be using computer 2 from computer1. To open another terminal from
computer2, type xterm & .
> - Redirect, see an example of its usage in the example for cat
cat - Concatenate. Allows you to combine the contents of two or more files
example: cat readme1.txt readme2.txt > combined.txt (creates a text file that contains the
contents of readme1.txt followed by the contents of readme2.txt and calls it (redirects it
to) combined.txt instead of just displaying it on the screen
gzip - Compresses a file so that it takes up less storage space
example: gzip picture.abc (creates a file called picture.abc.gz that is a compressed version
of the original file, it cannot be used until you gunzip it)
gunzip or gzip -d - Uncompresses a file that you compressed using gzip
example: gunzip picture.abc.gz (gives you back picture.abc in usable form)
tar - Used to create a single file that is an archive of multiple files
example: tar -cvf archive.tar readme1.txt readme2.txt readme3.txt (puts all 3 readme files
into a single file called archive.tar which must be untared with tar -xvf archive.tar to get
the original files back)
df - Displays disk space and usage for hard disk partitions.
du -k - Displays file sizes in your current directory in kilobytes.
| - Pipe, sends an input trough a specified program that changes it in some way, see next
example
grep - Searches a file for a pattern and prints all lines that contain that pattern, see next
example
ps - Prints information about active processes
example: ps -ef | grep AlanB (prints information about all of the processes that were
initiated by the user AlanB
kill -9 - Kills a specified process. The -9 part is only needed for particularly persistent
processes.
example: kill -9 90210 (kills the process numbered 90210)
sed - searches a text file and replaces one string of characters with another wherever the
original string appears
example: sed -e 's/s01/s02/g' s01.plot > s02.plot (searches s01.plot for "s01" and replaces
each occurrence with "s02")
echo - Takes some input and “echoes” it as output. This command is useful for getting
batchfiles to tell you what stage they are at and also for checking variables like PATH.
example: echo Have a nice day! (Have a nice day! is printed to the screen), echo $PATH
(tells you what your PATH (list of directories where computer looks for programs you try
to run) is set to)
alias - Alias allows you to use whatever text you want for some predefined command.
example: alias texteditor=’gedit’ (now typing texteditor is the equivalent of typing gedit)
more - Print the text of some file (often just to the screen)
example: more list.txt (prints contents of list.txt to the screen)
What else is there?
Lots. There are many other useful things and potentially disastrous things to type at a
LINUX command prompt. I suggest learning by experience and referring to books
and/or the internet when you need to. You can also ask me or someone else in our group
for help. I’d be happy to hear any comments (especially criticisms) that you have about
this guide.
Alan
Download