You are on page 1of 17

Created in Master PDF Editor - Demo Version

Mohammad Ali Jinnah


University, Karachi

Department Of Computer Science

Fall 2022

Operating Systems Lab

Instructor

Sajid Ali

Created in Master PDF Editor - Demo Version


Created in Master PDF Editor - Demo Version

LAB 02
Linux File System

Objective

After completing this lab, students should be able to:

Get familiarize with File system and security of Linux.

Peeking into Linux

The Linux has two modes of operation, terminal (also called BASH (Bourne Again Shell)), and
graphical user interface (GUI). Once we see how it works, we will jump onto command line
or BASH; for the purpose that we are interested not in using Linux but in programming in Linux
command prompt just lets you do this, since GUI is less customizable.
The file system on Linux is managed a little differently from the conventional setting you see
windows. In Linux, the main directory (my Computer in windows) is root (represented as /,
no alphabetical name, so /home means home directory in root folder). Just like windows it
has directories for specific purpose, like devices, documents, downloads etc. The physical
hard drive partitions are shown in figure where hda is commonly used for hard drive, sda for
flash drive. The common directory structure Linux is as follows

Directories of Linux

• / root directory, this directory contains all other directories


• /bin this contains programs needed for the early boot process, also contains commands
binaries
• /boot static files of the boot loader
• /dev this directory contains list of all devices installed on the system to be mounted
• /etc this folder contains system configuration files
• /etc/init.d contains boot scripts, the files necessary to boot a system
• /home this folder contains private directories of a user, it includes the functionality of
my Documents
• /lib shared libraries (for dynamically linked programs)
• /lost+found this folder is installed during installation of Linux, this contains broken
files recovered from unexpected system crash
• /mount mount point for removable media
• /mnt mount point for temporary file systems
• /opt optional software, add-ons, plug-ins etc. (for e.g. KDE, GNOME and Netscape)
• /proc this contains process files, contains running processes data
• /run this is a new directory introduced in the structure, for applications to save their
transient data, till the application finishes execution
• /sbin this folder contains the programs reserved for the system administrator and
needed for booting

Created in Master PDF Editor - Demo Version


Created in Master PDF Editor - Demo Version

• /srv data files for services provided by the system


• /sys this folder contains system files, where all device information for the kernel is
gathered
• /tmp temporary files (same as WINDOWS/TEMP folder)
• /usr contains all users’ application data, cookies browsing history etc.
• /usr/bin contains generally accessible programs, programs installed for all users on the
system
• /usr/include header files for the C compiler
• /usr/include/g++ header files for the C++ compiler
• /usr/local this folder contains local, distribution-independent files for user programs,
user preferences etc.
• /usr/sbin this folder contains programs reserved for system administrator
• /usr/share/doc this folder contains various documentation files
• /usr/share/man this folder has system manual pages
• /usr/srcsource code for Linux
• /usr/src/Linux kernel source code
• /var configuration files (such as those linked from /usr)
• /var/admsystem administration data
• /var/log system log files
• /var/tmptemporary files

Editors in Linux
One of the most confusing things about vim is that it has four modes:

Insert: To type text


The Insert mode is not default, you must press i to move into insert mode. Type some
text in the screen.
Press the <Esc> button to get out of insert mode into Command mode.

Command: To issue commands. Also called as Normal mode.


The command mode is used to move about, and to manipulate text, sometimes in
interesting ways.

Ex: To issue colon commands


The Ex mode is used to issue colon commands, which is used for operations like saving,
search & replace and configuring vim. Save the text you just typed in by going to the
Ex mode by typing: from the normal mode and typing :w

Visual To select text visually


The Visual mode is used to select text, press v to enter it and select some text, then you
can issue commands that will apply only to the selected area, type <Esc> again to return
to command mode

.
• vim (to start vim)Filename<Enter>

Created in Master PDF Editor - Demo Version


Created in Master PDF Editor - Demo Version

• i (to insert text)


• <type text>
• <Esc> (to come to command mode)
• v (to select some text)
• <Esc> (back to command mode)
• :w filename (to save the text to the file 'filename')
• :q (to quit the file)
• :q! (to quit without saving)
• vim filename (to open the file you just saved directly in vim)

However, it is best to learn vim by using it. You can quickly learn the basics of vim by using
the inbuilt vim tutorial, by typing vim-tutor in the terminal. Using the Ex: command :help from
inside Vim is often very useful.

Files in Linux

Linux Files Are Case Sensitive


Linux is case sensitive, this means that FILE1 is different from file1, and /etc/hosts is different
from /etc/Hosts (the latter one does not exist on a typical Linux computer). There is a difference
between two files, one with upper case W, and the other with lower case w.

Everything Is A File
A directory is a special kind of file, but it is still a (case sensitive!) file. Even a terminal window
(/dev/pts/4) or a hard disk (/dev/sdb) is represented somewhere in the file system as a file. It
will become clear throughout this course that everything on Linux is a file.

Creating & Viewing Files

The 'cat' command is used to display text files. It can also be used for copying, combining and
creating new text files. Let's see how it works.

Created in Master PDF Editor - Demo Version


Created in Master PDF Editor - Demo Version

To create a new file, use the command


1. cat > filename
2. Add content
3. Press 'ctrl + d' to return to command prompt.

To view a file, use the command -

cat filename

The syntax to combine 2 files is -

cat file1 file2 > newfilename

As soon as you insert this command and hit enter, the files are concatenated, but you do not
see a result. This is because Bash Shell (Terminal) is silent type. It will never give you a
confirmation message like "OK" or "Command Successfully Executed". It will only show a
message when something goes wrong or when an error has occurred.

To view the new combo file, use the command

cat newfilename

Note: Only text files can be displayed and combined using this command. Deleting
Files

The 'rm' command removes files from the system without confirmation. To

remove a file use syntax -

rm filename

Moving and Re-naming files

To move a file, use the command.

mv filename new_file_location

Suppose we want to move the file "sample2" to location /home/xyz/Documents. Executing the
command

mv sample2 /home/xyz/Documents

Created in Master PDF Editor - Demo Version


Created in Master PDF Editor - Demo Version

mv command needs super user permission. Currently, we are executing the command as a
standard user. Hence we get the above error. To overcome the error use command.
sudo command_you_want_to_execute

Sudo program allows regular users to run programs with the security privileges of the
superuser or root.

Sudo command will ask for password authentication. Though, you do not need to know the
root password. You can supply your own password. After authentication, the system will
invoke the requested command.

Sudo maintains a log of each command run. System administrators can trackback the person
responsible for undesirable changes in the system.

xyz@VirtualBox:~$ sudo mv sample2 /home/xyz/Documents


[sudo] password for xyz: ****
xyz@VirtualBox:~$

For renaming file:

mv filename newfilename

NOTE: By default, the password you entered for sudo is retained for 15 minutes per terminal.
This eliminates the need of entering the password time and again.

You only need root/sudo privileges, only if the command involves files or directories not
owned by the user or group running the commands

3 Ways to Create a Text File Quickly Through the Linux Terminal

Being a Terminal-savvy person, you may always be looking for ways to ditch the mouse.
Creating a text file is one task for which you can depend only on your keyboard on an Ubuntu
system. Three commands from the Linux command line are at your service for creating text
files. These include:

 The cat command


 The touch command
 The standard redirect symbol

The cat Command


The cat command is very helpful when dealing with text files in Linux. It helps you in
achieving three basic purposes:

 Creating a text file

Created in Master PDF Editor - Demo Version


Created in Master PDF Editor - Demo Version

 Printing contents of a text file in your Terminal


 Printing contents of a text file to another text file
Here, we will explore the first use of the cat command; creating a text file through the
command line.
Enter the following command in your Terminal:

$ cat > filename.txt


After entering this command, the next prompt will not appear; rather the cursor will display
for you to enter the text for the file you just created.

Example:

In this example, I have created a text file through the following command and then entered
some sample text:

$ cat > SampleTextFile.txt

Once you have entered all the text, hit enter to move to the next line and then use the Ctrl+D
control to tell the system that you are done with entering the text. The usual command prompt
will then appear for you to move on with further operations.

You can then use the ls command to see that your newly created text file will be there in the
system.

$ ls

Created in Master PDF Editor - Demo Version


Created in Master PDF Editor - Demo Version

Through the cat command, you can then view the contents of the file as follows:

$ cat filename.txt

Example:
You can see that the cat command shows the text I wrote while creating my sample file:

The touch command


Another way of quickly creating a text file through the Terminal is by using the touch
command. The touch command, however, does not let you enter text in the file at the time of
creation. After creating the file, you can enter the text through your favorite text editor. You
might prefer the touch command over the cat command in one scenario; when you want to
create multiple files at once through one command.

Let us first see how to create a single file first through the Linux touch command:

$ touch filename.txt Example:

$ touch sampletouchfile.txt

Use the ls command to see if the recently created file now exists on your system.

Create multiple files at once through the touch command

Created in Master PDF Editor - Demo Version


Created in Master PDF Editor - Demo Version

As mentioned above, the touch command takes the lead on the cat command on the basis that
you can create multiple files simultaneously through the former. Use the following syntax to
do so:

$ touch filename1.txt filename2.txt filename2.txt ….


For example, in the following command, I have created three files at once through the touch
command:

$ touch sampletouchfile1.txt sampletouchfile2.txt sampletouchfile2.txt

I also checked the presence of the three files through the ls command in the above example.

If you want to edit any of the files you created through the touch command, you can use any
of your favorite text editors. Here I am using the Nano editor to enter text to one of the files I
created. I used the following command to open the file through the Nano editor.

$ nano sampletouchfile.txt

Created in Master PDF Editor - Demo Version


Created in Master PDF Editor - Demo Version

I then entered the text and saved it through pressing Ctrl+X and then by hitting Enter.

Using the Standard Redirect Symbol

The standard redirect symbol is usually used when redirecting the output of a command to a
file. However, it can also be used to create a single text file. The only difference is that while
creating a new file we do not specify any command before the redirect symbol.
The difference of using the standard redirect symbol for creating a text file is that unlike the
cat command, you can not enter text through this way. Also, unlike the touch command, you
can only create one file at a time through the redirect symbol.

Use the following syntax in order to create a text file through this symbol:

$ > filename.txt

You can then use the ls command to see if the newly created text file now exists on your
system.

Created in Master PDF Editor - Demo Version


Created in Master PDF Editor - Demo Version

You can enter text in the file through your favorite text editor. In the following example, I am
using the Vim editor to edit the file through the following command:

$ vim MyTextFile.txt

When you save and exit the file, your text file will have those contents saved.

File Permissions
Linux is a clone of UNIX, the multi-user operating system which can be accessed by many
users simultaneously. Linux can also be used in mainframes and servers without any
modifications. But this raises security concerns as an unsolicited or malign user can corrupt,
change or remove crucial data. For effective security, Linux divides authorization into 2
levels.

1. Ownership
2. Permission

The concept of permissions and ownership is crucial in Linux. Here, we will


discuss both of them. Let us start with the Ownership. Ownership of Linux files

Every file and directory on your Unix/Linux system is assigned 3 types of owner, given
below.

Created in Master PDF Editor - Demo Version


Created in Master PDF Editor - Demo Version

1. User
A user is the owner of the file. By default, the person who created a file becomes its
owner. Hence, a user is also sometimes called an owner.

2. Group
A user- group can contain multiple users. All users belonging to a group will have the
same access permissions to the file. Suppose you have a project where a number of
people require access to a file. Instead of manually assigning permissions to each user,
you could add all users to a group, and assign group permission to file such that only
this group members and no one else can read or modify the files.

3. Other
Any other user who has access to a file. This person has neither created the file, nor he
belongs to a usergroup who could own the file. Practically, it means everybody else.
Hence, when you set the permission for others, it is also referred as set permissions
for the world.

Now, the big question arises how does Linux distinguish between these three user types so
that a user 'A' cannot affect a file which contains some other user 'B's' vital information/data.
It is like you do not want your colleague, who works on your Linux computer, to view your
images. This is where Permissions set in, and they define user behavior.

Let us understand the Permission system on Linux.

Permissions

Every file and directory in your UNIX/Linux system has following 3 permissions defined for
all the 3 owners discussed above.

• Read: This permission give you the authority to open and read a file. Read permission
on a directory gives you the ability to lists its content.

• Write: The write permission gives you the authority to modify the contents of a file.
The write permission on a directory gives you the authority to add, remove and
rename files stored in the directory. Consider a scenario where you have to write
permission on file but do not have write permission on the directory where the file is
stored. You will be able to modify the file contents. But you will not be able to
rename, move or remove the file from the directory.

• Execute: In Windows, an executable program usually has an extension ".exe" and


which you can easily run. In Unix/Linux, you cannot run a program unless the execute
permission is set. If the execute permission is not set, you might still be able to
see/modify the program code(provided read & write permissions are set), but not run
it.

Created in Master PDF Editor - Demo Version


Created in Master PDF Editor - Demo Version

ls - l on terminal gives

Here, we have highlighted '-rw-rw-r--'and this weird looking code is the one that tells us
about the permissions given to the owner, user group and the world.

Here, the first '-' implies that we have selected a file.p>

Else, if it were a directory, d would have been shown.

The characters are pretty easy to remember.

Created in Master PDF Editor - Demo Version


Created in Master PDF Editor - Demo Version

r = read permission w =
write permission x =
execute permission
- = no permission

Let us look at it this way.

The first part of the code is 'rw-'. This suggests that the owner 'Home' can:

• Read the file


• Write or edit the file
• He cannot execute the file since the execute bit is set to '-'.

By design, many Linux distributions like Fedora, CentOS, Ubuntu, etc. will add users to a
group of the same group name as the user name. Thus, a user 'tom' is added to a group named
'tom'.

The second part is 'rw-'. It for the user group 'Home' and group-members can:

• Read the file


• Write or edit the file

The third part is for the world which means any user. It says 'r--'. This means the user can
only:

• Read the file

Created in Master PDF Editor - Demo Version


Created in Master PDF Editor - Demo Version

Changing file/directory permissions with 'chmod' command

Say you do not want your colleague to see your personal images. This can be achieved by
changing file permissions.

We can use the 'chmod' command which stands for 'change mode'. Using the command, we
can set permissions (read, write, execute) on a file/directory for the owner, group and the
world. Syntax:

chmod permissions filename

There are 2 ways to use the command - 1.

Absolute mode 2. Symbolic mode

Absolute(Numeric) Mode

In this mode, file permissions are not represented as characters but a three-digit octal number.

The table below gives numbers for all for permissions types.

Number Permission Type Symbol

0 No Permission ---

1 Execute --x

2 Write -w-

3 Execute + Write -wx

4 Read r--

5 Read + Execute r-x

6 Read +Write rw-


7 rwx
Read + Write
+Execute

Let's see the chmod command in action.

Created in Master PDF Editor - Demo Version


Created in Master PDF Editor - Demo Version

In the above-given terminal window, we have changed the permissions of the file 'sample to
'764'.

'764' absolute code says the following:

• Owner can read, write and execute


• Usergroup can read and write
• World can only read

This is shown as '-rwxrw-r-

This is how you can change the permissions on file by assigning an absolute number.
Symbolic Mode

In the Absolute mode, you change permissions for all 3 owners. In the symbolic mode, you
can modify permissions of a specific owner. It makes use of mathematical symbols to modify
the file permissions.

Operator Description

+ Adds a permission to a file or directory

Created in Master PDF Editor - Demo Version


Created in Master PDF Editor - Demo Version

- Removes the permission

=
Sets the permission and overrides the permissions
set earlier.

The various owners are represented as -

User Denotations

u user/owner

g group

o other

a all

We will not be using permissions in numbers like 755 but characters like rwx. Let's look into
an example

Created in Master PDF Editor - Demo Version

You might also like