LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   chmod -w john.c'. Then 'rm *' deletes john.c too! (https://www.linuxquestions.org/questions/linux-newbie-8/chmod-w-john-c-then-rm-%2A-deletes-john-c-too-4175455319/)

stf92 03-23-2013 10:34 PM

chmod -w john.c'. Then 'rm *' deletes john.c too!
 
Hi: Suppose I want to delete all files in a directory except one, john.c. What I did was to reset the write bit in the permissions, 'chmod -w john.c'. Then 'rm *'. But john.c got deleted too. Any way out of this?

lleb 03-23-2013 10:40 PM

did you rm * as john.c the user or what ever user has permissions to write to those files?

shivaa 03-23-2013 10:45 PM

File owner can do this. It seems that you're the owner of the file.

So first change the ownership of the file, remove write permissions and then try to remove all other files.

Code:

~$ chown user:group john.c
~$ chmod 550 john.c
~$ rm -rf ./*

BEWARE: Be alert when you run rm -rf ./* command, and make sure that you're in correct directory.

jlinkels 03-23-2013 10:54 PM

Deleting john.c is possible because you hold write permissions on the directory. The proposal to change ownership of the file seems to be correct to me.

jlinkels

stf92 03-23-2013 10:58 PM

Useful answers. They will be saved. Thanks.

rknichols 03-23-2013 11:12 PM

Removing a file is an operation on the directory, so the directory permissions are what matters. If you have write permission in the directory, you can remove any file**. Write permission for the file is not required, but if you try to remove a file for which you do not have write permission, the rm command will, in the absence of the "-f" flag and if stdin is connected to a terminal, ask for confirmation before doing the removal. But that is really just a warning, and you do have the permission needed to remove the file.
**Directories with the "sticky" bit set in their permissions ("t" in the last character of the ASCII permissions) have an additional restriction. To remove a file from such a directory you must be the owner of the file or the owner of the directory. This bit is commonly set on publicly writeable directories like /tmp (perms "rwxrwxrwt").

harryhaller 03-23-2013 11:18 PM

It should stop and ask you before deleting the file.
Quote:

rm: remove write-protected regular file ‘/path/file.1’?
If the directory is also no-write, it'll ignore a 'y' response and refuse to delete the others as well.
Quote:

rm: remove write-protected regular file ‘/home/sysout/test.w/r.1’? y
rm: cannot remove ‘/path/file.1’: Permission denied
rm: cannot remove ‘/path/file.2’: Permission denied
rm: cannot remove ‘/path/file.3’: Permission denied


All times are GMT -5. The time now is 01:50 PM.