Lab1 PDF
Lab1 PDF
Introduction to Unix
Objectives
This Lab is for new users of the Linux operating System. The students assumed to have some prior
experience with computers, but not necessarily with Linux. The followings are the primary
objectives of this lab session:
1.
2.
3.
4.
What is Linux
Understanding Linux File System
Create and maintain files and directories of files.
Be familiar with PICO to create and modify a text file
bin
dev
home
etc
usr
local
ugics
st111111
st222222
gradics
src
facics
st333333
Part of file-system
In Linux, everything is treated as a file. A directory is a file. It is a file that contains a list of files and
information belonging to those files. This would include things like who owns (created) the file,
how long it is, and who can use it. Since a directory is simply a list of files, it can contain any file in
it, including other directories.
Examples:
Use the above diagram and assume that your current working directory (your current position in
the file system) is ugics:
1. A file named salam.txt is placed inside the directory st111111.
The absolute (full) name is: /home/ugics/st111111/salam.txt
The relative name is:
st111111/salam.txt OR
./st111111/salam.txt
2. A directory named ahmed is placed inside the directory gradics.
The absolute (full) name is: /home/gradics/ahmed
The relative name is:
../gradics/ahmed
3. A file named csh is placed inside the directory bin.
The absolute (full) name is: /bin/csh
The relative name is:
../../bin/csh
4. A directory named st123456 is placed inside the directory ugics.
The absolute (full) name is: /home/ugics/st123456
The relative name is:
st123456 OR
./st123456
3. Linux Commands:
1. Listing files and directories
ls (list)
When you first login to any Linux machine, your current working directory is your HOME
directory. Your home directory has the same name as your username.
Examples:
Example 1: To find out what is in your home directory, type:
% ls
There may be no files visible in your home directory, in which case, the Linux prompt will be
returned. Alternatively, there may already be some files inserted by the System Administrator
when your account was created.
ls does not, in fact, cause all the files in your home directory to be listed, but only those
ones whose name does not begin with a dot (.). Files beginning with a dot (.) are known as
hidden files and usually contain important program configuration information. They are hidden
because you should not change them unless you are very familiar with Linux!!!
Example 2: To list all files in your home directory including those whose names begin with a
dot, type:
% ls -a
The a indicates an option to show all files in your home directory.
2. Making Directories
mkdir
(make directory)
We will now make a subdirectory in your home directory to hold the files you will be creating
and using in this course.
Example:
To make a subdirectory called ics102 in your current working directory type
% mkdir ics431
Now to see the directory you have just created, type
% ls
Please note that creating a subdirectory will not change your current position in the file system
tree. Thus, after creating ics431, your current directory stills remain you HOME directory.
Exercises (1b):
1. Make another directory inside the ics431 directory called Lab1 then make Lab1
your current working directory.
2. Now type the command:
% cd ..
followed by:
% ls
In which directory you are located now?
3. Type the following command
% cd /etc/fs
followed by:
% ls
In which directory you are located now?
4. Now enter the command:
% cd
In which directory you are located now?
5. Write a single Linux command to make Lab1 your current working directory.
4. Pathnames
pwd (print working directory)
The pwd command is very useful to know your current position in the file system. It displays
the full path name of your current working directory.
Example:
Now, type
% pwd
What is your current working directory?
Be sure your current working directory is Lab1 before moving to next
section
Exercise (1c):
Modify the above file such that the content will be as follow:
Hello world,
My name is Abdullah Khaled
Im a student in KFUPM. Im learning Linux OS
So this is my first lab
cat (concatenate)
The command cat can be used to display the contents of a file on the screen.
Example:
To verify your wok in previous section, you can view the content of hello.txt by typing:
% cat hello.txt
The cat command is useful for displaying short files of a few lines. To display longer files
use less or more commands.
7. Copying Files
cp (copy)
The cp command allows you to copy a file from one location to another location. There are
different syntaxes to use cp command as shown below:
Syntax 1: ( copy a file to another file)
% cp file1 file2
where file1 is the name of an existing file and file2 is the name for the new copy of that
file. The original file will remain unchanged and a copy will be placed in file2. If file1
and file2 are not in current directory, then you have to specify its pathname.
Syntax 2: (copy a file to another directory)
% cp file directory
where file is the name of an existing file and directory is the name for the destination
directory. The original file will remain unchanged and a copy will be placed in that
directory.
Be sure your current working directory is Lab1 before moving to examples
Create a new directory named backup inside your current directory
Examples:
Example1: To create a backup of hello.txt by copying it to a file called salam.txt, type:
% cp hello.txt salam.txt
salam.txt
..
Why!!? Now to see the file you have just copied, type:
% ls ..
Example3: To copy the file named salam.txt from directory ics431 to your current
directory named as salam.bak, type
% cp ../salam.txt salam.bak
Why!!? Now to see the file you have just copied, type:
% ls
Example4: To put a copy of salam.txt into your backup directory, type
% cp
salam.txt
backup
Why!!? Now to see the file you have just copied, type:
% ls backup
Exercise (1d):
Copy the file named Hello.txt to your home directory? Verify your work.
8. Moving Files
mv (move)
To move a file from one place to another, use the mv command. This has the effect of moving
rather than copying the file, so you end up with only one file rather than two.
It can also be used to rename a file, by moving the file to the same directory, but giving it a
different name.
Example:
We are going to move the file salam.bak to your backup directory, type
% mv
salam.bak
backup
Now what is the content of your current directory? What is the content of your backup
directory?
Examples:
Example 1: We know that your backup directory contains two files named salam.txt and
salam.bak. We are going to delete the file salam.txt and the file salam.bak.
% rm backup/salam.txt
% rm backup/salam.back
Example 2: If you successfully deleted the two files, you can delete the backup directory. To
delete the backup directory, type:
% rmdir backup
Exercises (1e):
1. Delete the file named salam.txt from directory ics431
2. create a directory named tempdir by using mkdir then remove it.