0% found this document useful (0 votes)
59 views

Experiment No. - 6 - Managing Directories 1. Objective(s) : The Activity Aims

The document discusses various UNIX commands for managing directories and files, including commands to create and delete directories, change the current working directory, move files between directories, rename and link files, delete files, and copy directories. The intended learning outcomes are for students to be able to choose the appropriate commands to manage files and directories.

Uploaded by

Jong Franco
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views

Experiment No. - 6 - Managing Directories 1. Objective(s) : The Activity Aims

The document discusses various UNIX commands for managing directories and files, including commands to create and delete directories, change the current working directory, move files between directories, rename and link files, delete files, and copy directories. The intended learning outcomes are for students to be able to choose the appropriate commands to manage files and directories.

Uploaded by

Jong Franco
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Experiment No.

___6__   
MANAGING DIRECTORIES   

1. Objective(s):   
​The activity aims
1.1 ​To discriminate different UNIX commands used to manage directories and the files they
contain
1.2 ​To analyze the UNIX commands used to create and delete directories ​1.3
To test different UNIX commands used to manage files within directories​ ​2. 
Intended Learning Outcomes (ILOs):   
The students shall be able to:
2.1. To choose the commands used to manage files and directories as needed.

3. Discussion (not more than 300 words):   

In the previous activity, you are presented with commands to display the contents of your
directory as well as the contents of a file.

While working in a computing environment, you will create various files and retrieve them at a
later time. How do you organize them? It is therefore necessary for you to learn how to
organize your files. In the real world, files are organized in a filing cabinet. Each file is labeled
intuitively and inserted in correct folders or drawers. Similarly, in a computing environment,
you must name your files appropriately and store them in proper directories and
subdirectories for easy retrieval.

UNIX provides tools to create directories and subdirectories, as well as copy and move files.
It also provides tools to delete unneeded files and directories. Since you may be working with
hundreds, even thousands of files, UNIX provides a tool to find your files

4. Resources:   
Personal computer with Unix or unix-based operating system

5. Procedure:   
5.1. Start a terminal session with your unix or unix-based operating system.
5.2. Run and tests the commands that falls in the categories of directory management.

5.2.1 Working with the Directory Structure

Recall the UNIX directory structure given as part of an exercise. You will also create
structure as extension of your home directory, similar to the structure that UNIX system
itself uses.

Remember that UNIX uses a base directory called the root directory. And all other
directories are subdirectories of the root directory. The root directory is signified by the
slash (/) symbol

The directory that you login to during the login process is your home directory, e.g.
/home/<loginname>. This is your designated working directory; all other subdirectories and
files should be stored in this directory.

5.2.2 Displaying the Current Directory with pwd

When working with files and directories, oftentimes you want to know which directory you
are currently using. After issuing many directory-level commands and moving your working
directory positions several times, you may loose track of which directory you are currently
using. UNIX provides the pwd command to do this, pwd means print your working
directory. It displays the full path of the working directory.

Try typing pwd <return>; which directory are you currently in?

The exercise above shows that the working directory is /home/ariel


5.2.3 Changing the Current Directory with cd

The directory you login to during the login process is called you home directory. This
directory has the format /home/<loginname>. In the figure shown above, the home
directory is /home/ariel.

If you want to move to a different directory, use the cd command plus the directory name
that you want to use as in the exercise below:

The above exercise changes from /home/dir to /usr

5.2.4 Creating, Moving and Deleting Directories

You create files when you use applications, when you copy other files, when you redirect
output to files. You can choose to collect all these files in your home directory, or you can
create additional directories with descriptive names to hold together files with the same
categories and simplify management of these files.

You are not, however, locked in the original organization that you created. You can still
copy or move these files to different directories. You can even empty the directories that
you have created or delete them completely.

The following sections help you gain understanding of how to create directories and
manipulate them and their contents.

5.2.5 Creating New Directories with mkdir

To create new directories, issue the command mkdir, make directory. The command takes
the name of the directory that you want to create as argument. The name of the directories
follows the UNIX naming convention for files.

The exercise below creates the shell directory.


5.2.6 Moving Files Between Directories with mv
In the previous section, you learned how to copy files. When you copy files, you create
another file with exactly the same contents. At times, however, you simply want to transfer
a file to a different location. You can accomplish this by using the mv command.

In the exercise below, two files prog1 and prog2 were created then moved to the shell
directory. Please note that the wildcard character (?) was also used in place of the
numbers 1 and 2.

5.2.7 Renaming Files with mv


You can also use the mv command to rename existing files. The process of renaming a file
is actually the process of moving a new name into the file’s directory entry. The general
form of the mv command when renaming a file is:

mv filename-to-be-renamed new-filename
where filename-to-be-renamed is the name of the file in the directory before the mv
command is issued. The second argument new-filename is the name of the file after the
command is executed.

The exercise below shows how “prog1” is renamed to “proga”.

5.2.8 Linking Several Names to One file with ln


To save disk space, you can maintain only one copy of a file and still have several entries
for it. Use the ln command to ​link a​ dditional directory entries to an existing file or a
directory entry.

To understand the concept of links, note that UNIX tracks a file’s identity by using the file’s
i-number in the i-node table. The file’s name and i-number are both sotred in a directory
entry. The directory’s i-number entry, in effect, points UNIX to the i-node that holds a file’s
storage information. When you give a file-name argument, UNIX looks at the names in the
directory and finds the named entry. UNIX then uses the associated i-number to access
the contents of the file. It is therefore possible for two or more directory entries to “point” to
the same i-number (same file) even when the names used for the file are different.

In the exercise below, the file named progln is linked to proga.

The figure below shows the files prog2 and progln has the same inode number. Use the
command ls –il to display the inode number or the stat prog2 progln command to give the
statistics of the files.

5.2.9 Deleting Files with rm


Disk space can be used up quickly, so it is often necessary to clean up your directories by
erasing files you no longer need. Even if your system hs plenty of space, you need to
perform good “housecleaning” on the contents of your directories from time to time. To
erase the files that you no longer need, use the rm (remove files) command.

Note that once a file is removed from the directory, it cannot be recovered. Thus it is
recommended to take precautionary measures when erasing files, oftentimes it is a good
practice to issue an ls command on the files that you want to delete before actually
deleting them.

The exercise below shows the use of rm command to remove files.


5.2.10 Copying Directories and Subdirectories with cp

At times, you may want to save some files and make copies of them so that you can
modify the copies or the original. Use the cp with the –r option to copy entire contents of a
directory to a specified directory.

The exercise below shows how to copy the entire contents of the shell directory to shellbak
directory.

Two files, file1 & file2 were created inside the shell directory. Then, the cp -r shell
shell_bak command, copied the entire contents of the shell directory to the shellbak
directory. Note that UNIX created the shellbak directory since it does not exist prior to the
execution of the command.
Course: Experiment No.: ​ ​Group No.: Section: ​ ​Group Members: 
Date Performed:​ ​ Date Submitted: ​ ​ Instructor:     
6. Data and Results:   

7. Conclusion:   

8. Assessment (Rubric for Laboratory Performance):  

You might also like