0

On an ext4 filesystem, I have a base dir which itself has three dirs a, b and c, and each dir has various contents inside.

I set chattr +a base and then executed rm -r base. Then I found a, b, c were still there. Nice. Then I found they all became empty. I cried.

What happened?

Cyker
  • 235

3 Answers3

4

I don't see why you are surprised at this. A directory is just a file. A directory entry is a pointer to a file. When you set chattr +a on base you only affect base, so you can't remove entries from it. The directories a,b and c are files too but they are not affected by the attributes of base other than you can't remove them from base.

user9517
  • 117,122
1

chattr is used to change file attributes on linux file system.

So, when you use chattr +a it only affects the base directory itself and the subdirectories and files directly under it.

Why?

The term directory is used in a computer software context to refer to what appears to the user to be a container or folder that can hold files and other directories.

In Linux and other Unix-like operating system, everything on the system is treated as being a file, and a directory is thus considered to be just a special type of file that contains a list of file names and the corresponding inodes for each file and directory that it appears to contain. An inode is a data structure on a filesystem that stores all the information about a file except its name and its actual data.

Therefore, it can be useful to think of the word directory as being an abbreviation for the term directory file. Although perhaps technically redundant, it is convenient and common to use expressions such as files and directories when referring to the contents of a directory; an alternative is filesystem objects.

Ref: http://www.linfo.org/directory.html

When you use rm -r base, it runs recursively (-r option) and tries to remove all the files and subdirectories and their contents but it fails to remove the base directory and the directories and files directly under it. But it successfully removes the files under the subdirectories.

As for the a (append) attribute, you can add directories and files under it without any problem, but you can not delete or rename them. For me, it behaves perfectly as a file with it subdirectories and files directly under it.

What you wanted to achieve, to protect the directory and all the subdirctories and files under it, you should have used the -R option to set or add the attribute Recursively to each and all. As documentd here:http://linux.die.net/man/1/chattr

Diamond
  • 9,291
0

The "a" attribute can only be applied to files (once applied, they only can be open in append mode for writing). The behaviour of applying this attribute to directories is not defined.

dr_
  • 1,135