1

I have changed the directory attribute immutable by chattr +i /testdir; but I need to create files in that directory.

Or is there any way that nobody can delete the directory but the owner can still write in that directory?

3 Answers 3

3

The way to get what you need is:

chattr +a /testdir

Set the permissions on the directory itself so that only the owner has write permissions.

The a switch makes it so that the files can be created and modified inside but not deleted.

The manpage for chattr only says what a does for files but it does indeed do the above when used on a directory as I found out myself when searching for a way to do the same.

1
  • Folder will be append-only, meaning that once you create a file in it, you can never delete it. Only solutions around this are (a) either to remove the a attribute before deleting files, or (b) use Romeo's solution and make a 2-level directory tree, with the first directory's permissions set to r-xr-xr-x. It's a pity that there's no "un-removable directory" flag that prevents a folder from being removed, while permitting to alter/remove its content.
    – ChennyStar
    Jun 5, 2023 at 18:40
1

The command

chattr -i /testdir

will remove this flag. Then you create the new file and again

chattr +i /testdir

you should set only r and x of parent directory for group and other to prevent removing the directory

0

The immutable attribute set on a directory only affects this directory and its immediate (e.g., 1st level) content. Not the content of a sub-directory.

So, if you only need to add files to this directory, without ever having to modify or to delete files from it; then using the a attribute like Nasir Riley showed is the way to go.

On the other hand, if you want to be able to have a full control of the directory, while preventing the directory from being renamed/deleted, then you could use a subfolder, and make only the upper-level folder immutable, for example :

$ mkdir d1
$ mkdir d1/d2
$ sudo chattr +i d1

You can then verify that you have full control over d1/d2 :

$ touch d1/d2/f1
$ mv d1/d2/f1 d1/d2/f2
$ rm d1/d2/f2 

While at the same time the d1 folder is immutable :

$ sudo rm -rf d1/
rm: cannot remove 'd1/d2': Operation not permitted

$ sudo mv d1 d3
mv: cannot move 'd1' to 'd3': Operation not permitted

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .