51. File Permissions - File Attributes
Contents
To make our files more secure, Linux provides a feature called file attribute. By configuring the attributes of the file, we can specify whether it is read-only or undeleted, or in other statuses.
List File Attributes
Here are some options for lsattr command:
Option | Meaning |
---|---|
-R | Output all attributes of files, directories and subdirectories’ under a specified directory. |
-a | Output all attributes of files, directories, directory itself and the parent directory of a specified directory. |
-d | Output only the directory itself instead of everything under it. |
-l | Display long names of the option rather than the acronyms. |
-v | List version or general numbers of files and directories. |
Let’s first prepare some files and directories:
|
|
The lsattr command iterates all files and directories contained in the specified directory by default but does not iterate through the files or directories under its subdirectories:
|
|
Iterate Recursively
With the -R option, we can iterate all the contents of the directory and everything under its subdirectories:
|
|
Include Directory Itself and Parent Directory
With the -a option, we can iterate all of the contents under the specified directory, including itself and its parent directory:
|
|
Directory Itself Only
With the -d option, we can display only the directory itself, not everything under it:
|
|
Long Name Instead of Char Abbreviation
With the -l option, we can display the long name of the option rather than the acronyms:
|
|
Version or Generation Number
With the -v option, we can list version or general numbers of files and directories.
|
|
Change File Attributes
Here are some options for chattr command:
Option | Meaning |
---|---|
-R | Recursively change the attributes of the directory and all its contents. |
-V | Change the attributes verbosely. |
Here are all the mode options for chattr command: (Most options work only on older file system types.)
Option | Meaning |
---|---|
a | Append only. |
A | No atime updates |
c | compressed |
C | No copy on write |
d | No dump |
D | Synchronous directory updates |
e | Extent format |
F | Case-insensitive directory lookups |
i | immutable |
j | Data journalling |
P | Project hierarchy |
s | Secure deletion |
S | Synchronous updates |
t | No tail-merging |
T | Top of directory hierarchy |
u | Undeletable |
Mode options supported with xfs type file systems:
Option | Meaning |
---|---|
a | Append only. |
A | No atime updates |
d | No dump |
i | immutable |
S | Synchronous updates |
Mode options supported with ext4 type file systems:
Option | Meaning |
---|---|
a | Append only. |
A | No atime updates |
d | No dump |
D | Synchronous directory updates |
e | Extent format |
i | immutable |
j | Data journalling |
S | Synchronous updates |
u | Undeletable |
Mode operations:
Option | Meaning |
---|---|
+ | Appendant |
- | Deletion |
= | Assignment |
We can use the df command followed by the -T option to view the file system type of the partition in which the root path resides:
Assign Attributes
With the equal sign(=), we can assign attributes to files or directories:
|
|
Or we can set the attribute verbosely:
|
|
When we try to open and save the file, vi prompts that it is a read-only file and can not save the changes:
|
|
|
|
Even if the save command is followed by an exclamation point, it is also impossible to save the file:
|
|
Append Attributes
With the plus sign(+), we can append attributes to files or directories:
|
|
Remove Attributes
With the minus sign(-), we can remove attributes from files or directories:
|
|
Author Dong Chen
LastMod Sat Mar 23 2019