Practical Application for Introduction to Linux: Managing a Linux Filesystem

Instructor: Daniel Arnold

Daniel has a bachelor's in Computer Science, is a CISSP and CEA. He is a cyber competition coach and speaks on Info Security at conferences.

In this lesson we will have an opportunity to do some practical work in a Linux Filesystem. The skills emphasized will involve creating a file, determining its current attributes, and changing those attributes according to a given specification.

Lesson Overview and Knowledge Required

In this lesson, you will create a file in the Linux filesystem and then conduct operations to view and reset attributes on the file. Attributes are qualities of a file that we can control that will permit or restrict certain actions being performed on the file. Discussion will be provided on some of the more commonly edited file attributes as well as on two commands we'll use in the terminal - lsattr (used to list attributes) and the chattr (used to change attributes). Understanding the example attributes as well as usage of the lsattr and chattr commands will be required for completing the lesson.

Program Code

Creating a file is our first task. There are of course many ways in which files can be created in a Linux environment but the simplest way, for our file attribute demonstration purposes, is to use the touch command. First we'll open a Terminal session. Then we'll create a file called testfile as shown in Figure 1 below:

Figure 1: Create a File with touch
Figure 1: Create a file with touch

Now our next task is to explore the file attributes available. A few are shown below:

a: operations to append to the file are permitted

c: file will be compressed on the disk automatically

d: file cannot be backed up using the dump command

e: file is mapped on continuous blocks on the disk using extents

i: file is immutable and operations to change, rename or delete the file will not be permitted

Now we'll explore to determine which attributes are enabled for our file by default. This requires the lsattr command to list attributes. It is demonstrated below in Figure 2:

lsattr command
Figure 2: lsattr command

We see here that only the e attribute is active indicating the file is to be stored on extents (i.e. it will be written only to contiguous blocks of storage to prevent fragmentation). The series of dashes surrounding the lone attribute are the placeholders where we would see the other attributes if they were enabled. Let's turn a few on now and see what that looks like. To set attributes, we'll need to use the chattr command. It will also require the sudo command prefix. The sudo command is the superuser do that allows us to run a command with admin credentials. To actually set a given attribute, we place a plus character (+) in front of the attribute we want to set and then follow that up with the target file name. See an example setting the append attribute on the testfile file in Figure 3 below:

Figure 3: chattr command
Figure 3: chattr command

We followed up with another lsattr and can see that the append attribute was in fact set for the file.

Removing an attribute works in a similar fashion. We still use the chattr command and the command syntax is the same and also requires the sudo prefix. For removal we simply use the minus character (-) instead of the plus sign. Let's now remove the append attribute. See Figure 4:

Figure 4: chattr to remove attribute
Figure 4: chattr removal example

To unlock this lesson you must be a Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use

Become a member and start learning now.
Become a Member  Back
What teachers are saying about
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 160 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Create an account to start this course today
Try it risk-free for 30 days!
Create An Account