Let's say i have a directory, at:

  • /tmp/uploads/

And inside got several recursive directories and files created by DIFFERENT USERS before.

So now, suddenly how do i make that above directory to be writable by root only?

Which means, the existing whatever users won't be suddenly able to write at all (but read only). So then only root will be able to write any further.

Very importantly, i can NOT lose the original ownership (who owns what) of the things inside.

Which also means, after all, i am trying to LOCK the directory for security reason.

Thank you.

  • What's the difference between a sudden write and a normal one? – Staven Jul 27 '15 at 11:16

Use attributes:

chattr -R +i files

(as root) will add the +i attribute recursively to your folders and files which will prevent ANY alternations. Note that root will also be locked and you would need to unset the i manually every time. Ownership and alike will be left unchanged.

  • Will this apply recursively please. And how do i 'unset the i manually' again? Use -i instead? Thanks much! – 夏期劇場 Jul 28 '15 at 4:21
  • 1
    The -R is for adding it recursively, for unsetting, you guessed correctly, -i is the thing to do. Check man chattr for a more detailed list of possible attributes. Please also note: if you have dir1/dir2/ where dir1 is +i, and dir2 is -i, any file can be written to dir2 (according to the permissions of course) – Fiximan Jul 28 '15 at 6:42

First change user and group to root

chown -R root:root /tmp/uploads

then change permissions so that only root can write

chmod -R 755 /tmp/uploads


If you only need to restore files owners, I would save your files and owner in a file (There sure are better ways to do this, but this is the first thing that comes to my mind). Be sure /tmp/old_file_owners doesn't exist before runing the next:

for i in $(find /tmp/uploads);do ls -ld $i|awk -vf=$i '{if (NF==9) {print $3 ":" $4 " " f}}'>>/tmp/old_file_owners; done

and when you want to restore your files' owners, just do a

IFS=$'\n';for i in $(cat /tmp/old_file_owners);do chown $i;done

If you want to keep original file permissions more info will need to be stored in your file, but you can use the previous as a reference and do your stuff like that.

  • Thanks but ... then how do i do when i can revert back the permissions? I mean, since you gonna lose the original owners, how do you later give them the permissions back? – 夏期劇場 Jul 27 '15 at 10:15
  • I updated a bit to my question above. :) – 夏期劇場 Jul 27 '15 at 10:17
  • You didn't specify anything about giving back permissions. They will be able to access the files in read mode, another thing is if you want to revert those files owner back to their original user. In that case you will need to store that information and use it when you want to revert the files owners. – YoMismo Jul 27 '15 at 10:17
  • Thanks, but so then this use-case is impossible, unless i change all to root-owned? :3 – 夏期劇場 Jul 27 '15 at 10:19
  • 2
    Read Fiximan's answer. That's a great solution without altering ownerships and permissions. – YoMismo Jul 27 '15 at 12:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.