02
Programming Methods and Techniques I
UNIX files and directories management
Lucjan Janowski
2010-10-08
It is a translation of Michał Grega’s instruction.
Task 1 How to move in the UNIX directory system
1.
The basic information is where we are i.e. in which directory we are executing commands and programs. In
order to get this information
pwd
command is used. It is a very important information especially when you
are removing a group of files!
2.
In order to move into another directory you can use
cd
command. The
cd
command parameter is the
directory where you would like to go.
3. The are some special directories. The main directory is denoted by
/
, the home directory is denoted by
~
and
the upper directory is denoted by
..
.
4. In order to print what is in the current directory we can use
ls
command. Please read carefully what
a
,
l,s,k
options change.
5. As an exercise try to answer below questions:
a. Where is your home directory?
b. What kind of files and directories are located in the main directory
(/)?
c.
What is a difference between command
ls a
and
ls -a
?
d. What kind of information do we get using
ls -l
command?
An example:
After login to the system a user is in his/her home directory (
~
).
Executing command
cd mama
moves the user into the directory mama.
Executing command
cd tata
moves the user into the directory tata that is in mama directory.
Executing command
cd ..
moves the user to the upper directory i.e. into mama directory.
Executing command
cd ~
in any directory moves a user into his/hers home directory.
Task 2 Creating and removing directories and files
1. In order to create a directory use
mkdir
command. Note that you can
create more than one directory at once. Do you know how to do it?
2. Removing directories can be done by
rmdir
command. Please get familiar
with its options.
3. In order to change a file editing date you can use
touch
command.
4. As an exercise:
a. create a directory tree as shown on the figure on the right. How to do it quickly?
b. Create some files in different directories created in the previous point. What are the sizes and time
of the files?
c. Remove all the files and directories. How to do it quickly?
Task 3 Copying, moving, removing and linking files
1. The commands making possible copying, moving, removing and linking files are
cp
,
mv
,
rm
and
ln
respectively. Get familiar with their descriptions and options.
2. As an exercise use all those commands, note the difference between soft and hard linking (see above
example).
An example:
Note a difference in execution of the below commands.
touch zebra.txt
pico zebra.txt
(edit the zebra.txt file to make it not empty)
ln zebra.txt dow_tward
more zebra.txt
more dow_tward
rm zebra.txt
touch zebra.txt
pico zebra.txt
(edit the zebra.txt file to make it not empty)
ln –s zebra.txt dow_miek
more zebra.txt
more dow_miek
rm zebra.txt
more dow_tward
more dow_miek
Task 4 A file permission usage and change
1. By using the
s -l
command we can see a file permission described by characters
d, r, w, x, -
that are
expressed in 10 characters long chain. The interpretation of the characters is given in the below example.
2. A file permission can be changed using
chmod
command. Note that a file permission can be specified by
numbers or letters.
3. As an exercise just play with permission values
a. Execute
chmod 1 [plik], chmod 2 [plik], chmod 3[plik], chmod 4[plik], chmod 5
[plik] chmod 6 [plik], chmod 7 [plik], chmod 17 [plik]
etc. and see what is the
difference.
b. Remove all permissions from the file – can you remove it now?
An example:
After executing
chmod 347 zebra
the file zebra will have permissions:
--wxr--rwx
.
The first character means that it is a file (what will we see if it is a directory?)
The next 3 characters denote the owner permissions i.e. the owner cannot read the file, but he/she can write (
w
) and
execute the file (
x
).
The next 3 characters denote the group permissions i.e. the group members can read (
r
), but they cannot write and
execute zebra file.
The last 3 characters denote other users permissions i.e. they have full permission to zebra file since they can read, write
and execute it.
The numbers in
chmod 347 zebra
command code the file permission. The first number changes the owner permission,
the second the group permission and the last one the other users permission. The value is a sum of possible changes where
1 denotes executing permission, 2 denotes write permission and 4 is a read permission. Therefore, 3 = 1+2 is makes it
possible to execute and write a file.
Task 5 Changing a file owner and group
1. Sometimes an owner of a file has to be changed. To do so you can use
chown
command. If the group has to be
changed you should use
chgrp
command.
2. Can you change a file’s owner or group? Try the same operation after using
su
command.
3. Can you remove a file if you are not an owner? Can the answer vary for different files?
4. Can you change a file’s owner using a reference file?
Additional exercises
1. What to do to make a file size expressed in kilobytes?
2. What are the differences between soft and hard links? Do you know any possible usage of such links?
3. What are permissions of “mama” file after executing command
chmod 427 mama
?
4. Can you change a file’s group using
chmod
command ?
5. What set of permissions should be set to web files? Is it the same for all files?