Tutorial: Basic Unix
1.1 Linux desktop
1. If you have never used Linux before, spent a few minutes familiarising
yourself with the Desktop environment.
- Try right- and left-clicking on different components (eg. on the background).
- Find out how to open a terminal window;
- how to navigate through directories;
- how to open an editor;
- how to logout;
- how to read help
- how to customise the desktop (change colours etc).
1.2 Editing files
2. Editing a file in Unix with pico:
- Open a terminal window.
- Edit a file with
pico FILENAME
where "FILENAME" is the name of a new file
containing just letters and numbers, no spaces.
- Type a few lines.
- Delete a line with Ctrl-k (that is the control key and k). Use the arrows to
navigate to the end of the file. Then type Ctrl-u to paste the line.
- Use Ctrl-x to exit pico.
- Open the same file again. (Note: depending on your shell, you may be able to
use arrow keys to re-execute a previous command and the tab key for filename or
command completion. Type pico and then the first letter of your file, then press
the tab key to see if name completion works.)
1.3 Working with files
3. Viewing existing files:
- Type the following commands one at a time
(where FILENAME is the name of the file you edited in the last exercise).
more FILENAME
cat FILENAME
Most likely you won't see much difference between "more" and "cat". You'll
need a larger file to see what the difference is.
man man > fileX
(This creates a larger file called "fileX" which has
the manual page for "man" as content.)
- Type the following commands one at a time and observe what they do:
more fileX
cat fileX
head fileX
tail fileX
4. Renaming, copying and comparing files
"ls" is used to list all the files in your current directory; "mv" is used
for renaming; "cp" for copying;
- Type the following commands one at a time and observe what they do:
ls
mv fileX fileY
ls
more fileX
more fileY
cp fileY fileZ
ls
more fileY
more fileZ
- Now edit fileZ and change the first line. Type
diff fileY fileZ
5. Viewing file properties
The following two commands show file properties
ls -l
file *
6. Removing files
"rm: is used to remove files. Several of the Unix commands have a "-i"
option, which inquires if you really want to execute the command before
it executes it.
rm -i fileZ
7. Review question
- So far you learned the commands "pico, more, cat, head, tail, ls, mv, cp, rm,
diff". Most of the functionality of these commands, such as editing, viewing,
renaming, copying, deleting can easily be achieved in any GUI environment.
But there is one command, which is not available in every GUI environment.
In fact, you may not even know how to do this on a PC. Which command is that?
1.4 Working with directories
8. Try the following commands. If you cannot determine what these do
type "man ls" and scroll down to where the options "-l", "-t" and "-a"
are explained. To exit a manual page type "q". Type "man pwd" for information
about "pwd".
ls
ls -l
ls -l -a
ls -l -t
pwd
9. Searching for information
The following commands can be used for finding files, programs and file content.
(This exercise assumes that you still have a copy of the man man page saved as
fileY, see exercise 3.)
find . -name 'fileY'
which ls
grep 'manual pages' fileY
10. Creating and deleting directories
Directories can be created with "mkdir" and deleted with "rmdir". By default
all activities apply to the current directory. "pwd" shows the current
directory. "cd" is used to change to a user's home directory. With
"cd DIRECTORYNAME" one can change to the directory named DIRECTORYNAME;
with "cd .." one can move to the parent directory of the current directory.
The current directory is referred to by "."; the parent directory by "..".
- Create the following directory structure under your home directory:
letters
work
progs
tutorial
misc
script
- Copy the file "fileY" from your home directory
to the misc directory, and use the ls command from your home directory
to verify the move. (You can use "ls directory1/directory2" to view a directory
which is further down in the hierarchy under your current directory.)
- Make the misc directory your current directory and move "fileY"
from the misc directory to the progs directory. (You can use a combination of
"..", "/", and the directory name to create a relative pathname for
the progs directory while you are in the misc directory.)
- Verify that you are still in the misc directory. List the contents of the progs
directory without leaving the misc directory. Copy the file "fileY" from your home
directory to the tutorial directory and rename it "file2".
- Make the directory "work" your current directory and
copy the file "file2" in the tutorial directory to the scripts directory.
- Use the rmdir command to remove the tutorial directory. If it fails, what
other command with options could you use? Try it.
- Go to your home directory and type "ls -R".
1.6 Summary
Below is a list of all the commands you learned so far. Read through
the list and check that you understand what each command does.
- displaying information
- pwd (present working directory show which directory you are in)
- ls (list directory contents)
- find (search for directories and files)
- which (search for programs)
- grep (search for file content)
- man (viewing manual pages)
- navigating and manipulating the directory tree
- cd (change to a new directory)
- mkdir (create a new directory)
- rmdir (delete a directory)
- manipulating files and directories
- mv (move)
- cp (copy)
- rm (remove)
- viewing and editing files
- pico (an editor)
- cat (display file content)
- more (display file content a page at a time)
- head (display first lines of file)
- tail (display last lines of file)
- diff (compare different files or directories)
- file (show file properties)
1.7 Users and System Information
Here are a few more Unix commands. These are useful for displaying information.
Execute each of these commands and see if you can figure out what they mean
(if not have a look at the corresponding man pages). There is much more to
Unix administration than these commands, but viewing information about the
system and its users is an important first step.
- users (list current users)
- who (display who is logged in)
- w (display who is logged in and they are doing)
- last | more (display last logins of users)
- logname (display logname of current user)
- hostname (display hostname)
- printenv (print current user's environment)
- quota (display current user's quota)
- df (display free disk space)
- du (display disk usage statistics)
- top (show system usage information)
- ps (display process information)
Unix Challenge
Here are some questions for those people in need of a more difficult
challenge. Do not worry if at this state you are struggling to reach here,
as in later tutorials even questions like these will become obvious to you.
-
List the commands which have been recently executed and
contain 'rm'.
-
Write a Unix command that prints all files (not directories) which
have been changed during the last 24 hours. (More difficult:
Write a Unix command that copies all files (not directories) which
have been changed during the last 24 hours to a directory called
"backup").
-
Change the 'rm' command so that instead of removing files it
moves them to a hidden directory called 'trashcan'
under the user's home directory.