Help answer threads with 0 replies.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 03-23-2013, 11:34 PM   #1
Senior Member
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 3,485

Rep: Reputation: 51
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?
Old 03-23-2013, 11:40 PM   #2
Senior Member
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora
Posts: 2,630

Rep: Reputation: 495Reputation: 495Reputation: 495Reputation: 495Reputation: 495
did you rm * as john.c the user or what ever user has permissions to write to those files?
Old 03-23-2013, 11:45 PM   #3
Senior Member
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
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.

~$ 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.
1 members found this post helpful.
Old 03-23-2013, 11:54 PM   #4
Senior Member
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Wheezy/Jessie/Sid, Linux Mint DE
Posts: 4,493

Rep: Reputation: 635Reputation: 635Reputation: 635Reputation: 635Reputation: 635Reputation: 635
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.

1 members found this post helpful.
Old 03-23-2013, 11:58 PM   #5
Senior Member
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 3,485

Original Poster
Rep: Reputation: 51
Useful answers. They will be saved. Thanks.
Old 03-24-2013, 12:12 AM   #6
Senior Member
Registered: Aug 2009
Distribution: CentOS
Posts: 2,959

Rep: Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268
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").
Old 03-24-2013, 12:18 AM   #7
Registered: Sep 2004
Distribution: Slackware-14.0
Posts: 452

Rep: Reputation: Disabled
It should stop and ask you before deleting the file.
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.
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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
My name is John and I am new here kinetic1001101 Member Intro 1 09-20-2010 01:33 PM
[SOLVED] Is John on line? stf92 LQ Suggestions & Feedback 12 08-17-2010 05:48 AM
My name is John and I'm a Linuxholic pushback Member Intro 2 12-08-2008 12:05 AM
John Lumby John Lumby Member Intro 2 10-22-2008 11:31 AM
John The Ripper infinity0022 Linux - Security 1 02-24-2008 01:23 AM

All times are GMT -5. The time now is 02:12 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration