Scheduled Maintenance: We are aware of an issue with Google, AOL, and Yahoo services as email providers which are blocking new registrations. We are trying to fix the issue and we have several internal and external support tickets in process to resolve the issue. Please see: viewtopic.php?t=158230

 

 

 

Making a File Undeletable

If none of the specific sub-forums seem right for your thread, ask here.
Post Reply
Message
Author
VonZorch
Posts: 2
Joined: 2018-07-16 09:05

Making a File Undeletable

#1 Post by VonZorch »

I have a file in a folder that I may need to empty.
When I do empty the folder I want to ensure that I keep that specific file.
I know making the file read only works but I may need to write to it.
Is this possible, and if so how?
I'm using the current stable Debian version, my sources.list points to stable.
I have ACL activated for my /home.

arzgi
Posts: 1194
Joined: 2008-02-21 17:03
Location: Finland
Been thanked: 31 times

Re: Making a File Undeletable

#2 Post by arzgi »

The simplest way would be to move that file somewhere else, and then copy it back

There is no need to make simple things complicate.

User avatar
roseway
Posts: 1528
Joined: 2007-12-31 22:50
Location: Kent, UK
Has thanked: 3 times
Been thanked: 4 times

Re: Making a File Undeletable

#3 Post by roseway »

... or assign the ownership of the file to root?
Eric

User avatar
GarryRicketson
Posts: 5644
Joined: 2015-01-20 22:16
Location: Durango, Mexico

Re: Making a File Undeletable

#4 Post by GarryRicketson »

I know making the file read only works but I may need to write to it.
Is this possible, and if so how?
Simply change the permissions back to read/write, when you need to write
to it, after you are done, change it back to read only.
===== edited ====
It is not very clear, exactly the situation. If you are asking if there is a option in the permission settings : see,

Code: Select all

man chmod
As far as I know, there is no setting that will allow read ,write but not delete, on linux and Unix like systems.
With a better description of exactly what you need to do, IE: what kind of directory, and files, maybe we could give some other ideas.
One example, I have a directory that has some scripts to make screen shots,thumbnails, and upload them to a server.
I need that directory to be empty, of any images when I run the script, but at the same time I do not want to delete or move the scripts out of the directory.
In my case, I don't delete the images, but move them to another directory, but any way, I used the file name extensions to control this.
For example:

Code: Select all

 mv *.png /images/ 
It could be 'rm' as well, to delete them. "images" is a sub dir, in the same dir.
Using *.png, only removes the images, the other files are the script names, with no extension, those do not get moved, or deleted. Hope that makes sense. To try to clarify:

Code: Select all

$ cd resize
$ ls
XP-3-tmb.png images       make-tmb-png scrnshot-2   tmb
XP-3.png     make-tmb     scrnshot     scrnshot-3
$ 
 
If I ran the command

Code: Select all

rm *.png
Only the .png
files get deleted, the others are left alone.
So, you could maybe give the files that you will want to be deleting later a special extension in the name, and the one file you do not want to get deleted,
do NOT use that extension in it's name.
Here, for example, I made a directory, I made some files, use the extension .del for one, but this could be applied to many files. The other files, I used .txt

Code: Select all

$ ls
a-test.txt b-test.del b-test.txt
$ rm *.del
$ ls
a-test.txt b-test.txt
$ 
or in reverse:

Code: Select all

$ ls
a-test.txt   b-test.nodel b-test.txt
$ rm *.txt
$ ls
b-test.nodel
$ 
 
All the .txt files are deleted, but not the .nodel

arzgi
Posts: 1194
Joined: 2008-02-21 17:03
Location: Finland
Been thanked: 31 times

Re: Making a File Undeletable

#5 Post by arzgi »

Bash has except-option, but IMHO, it's better to plan so, that termporary files and permanent are not mixed.

VonZorch
Posts: 2
Joined: 2018-07-16 09:05

Re: Making a File Undeletable

#6 Post by VonZorch »

Thanks all, I suspected it wasn't possible.

User avatar
pylkko
Posts: 1802
Joined: 2014-11-06 19:02

Re: Making a File Undeletable

#7 Post by pylkko »

there are possibly many work-arounds for this.

for example, you could make the file hidden. AFAIK simple

Code: Select all

rm *
kind of commands do not remove hidden files in linux/bash shell (you need something like {*,.*}). But I have not tested it.

you could also use some kind of snapshot scheme where the contents of the folder are reverted to an earlier state (mimicking deletion).

Perhaps a use a link to the real file outside the folder?

Or create a service that automatically replaces the file. So write a first .service file that automatically checks the file and backs it up to another folder. Then another service that periodically checks if the file is present and brings it back if not. Actually it could even bring it back always without the if-condition. This would incur a disk I/O and processor penalty, but if the file is small (a text file for example) it should be insignificant (linux running normally will be writing all kinds of logs and files all the time anyway).

User avatar
Head_on_a_Stick
Posts: 14114
Joined: 2014-06-01 17:46
Location: London, England
Has thanked: 81 times
Been thanked: 133 times

Re: Making a File Undeletable

#8 Post by Head_on_a_Stick »

Code: Select all

# chattr +i $file
deadbang

User avatar
GarryRicketson
Posts: 5644
Joined: 2015-01-20 22:16
Location: Durango, Mexico

Re: Making a File Undeletable

#9 Post by GarryRicketson »

VonZorch wrote:I have a file in a folder that I may need to empty.
When I do empty the folder I want to ensure that I keep that specific file.
I know making the file read only works but I may need to write to it.
---- snip----.
Thanks H_O_A_S, this is a new command to me, I had never heard of it until now, so I looked at

Code: Select all

man chattr 
A file with the 'i' attribute cannot be modified: it cannot be deleted
or renamed, no link can be created to this file and no data can be
written to the file. Only the superuser or a process possessing the
CAP_LINUX_IMMUTABLE capability can set or clear this attribute.
"but I may need to write to it." Of course , if and when they need to write to it
they could use "sudo" or "su", I suppose.
Seems like a option though, and not to complicated.

User avatar
pylkko
Posts: 1802
Joined: 2014-11-06 19:02

Re: Making a File Undeletable

#10 Post by pylkko »

Yes, this would be a solution, were it not for the fact that the OP appears to be rejecting this solution in the actual question. Changing it to immutable is what they mean with "read-only", AFAICS.

User avatar
pylkko
Posts: 1802
Joined: 2014-11-06 19:02

Re: Making a File Undeletable

#11 Post by pylkko »

I think the best solution is to use a hidden file, as 1) it will not be deleted with rm 2) it can be written to and 3) it meets the requirements of the first post.

User avatar
bw123
Posts: 4015
Joined: 2011-05-09 06:02
Has thanked: 1 time
Been thanked: 28 times

Re: Making a File Undeletable

#12 Post by bw123 »

VonZorch wrote:I have a file in a folder that I may need to empty.
When I do empty the folder I want to ensure that I keep that specific file.
...
Easiest way is don't delete it in the first place.
resigned by AI ChatGPT

peter_irich
Posts: 1405
Joined: 2009-09-10 20:15
Location: Saint-Petersburg, Russian Federation
Been thanked: 11 times

Re: Making a File Undeletable

#13 Post by peter_irich »

Head_on_a_Stick wrote:

Code: Select all

# chattr +i $file
Then you will not edit this file and change it, according "man chattr".
Perhaps, "chatrr +u", but I not understand, will this work in Linux or not.

Peter.

User avatar
GarryRicketson
Posts: 5644
Joined: 2015-01-20 22:16
Location: Durango, Mexico

Re: Making a File Undeletable

#14 Post by GarryRicketson »

Code: Select all

man chattr
CHATTR(1) General Commands Manual CHATTR(1)

NAME
chattr - change file attributes on a Linux file system

SYNOPSIS
chattr [ -RVf ] [ -v version ] [ mode ] files...

DESCRIPTION
chattr changes the file attributes on a Linux file system.
Err,??? It is supposed to be a Linux command, I would expect it to work on a Linux system,
How ever, the "u" option , no,...
BUGS AND LIMITATIONS
The 'c', 's', and 'u' attributes are not honored by the ext2, ext3,
and ext4 filesystems as implemented in the current mainline Linux
kernels.

"Current mainline kernels", so maybe on some older, or "off beat " kernels yes.
On Debian 7 wheezy, the same, the "u" option is not available.
Then you will not edit this file and change it, according "man chattr".
As a normal user no, as root or using "sudo", the attribute can be changed, I am not sure if it could be written to by root, with out changing the attribute first,...have not really tried it.
As I mentioned earlier, I just move the file or files I do not want to delete to another dir, after I am done deleting the files I want to delete, I move them back, if need be. If the file is extremely important, I all ways have a copy made and stored on a usb device, just in case some how the one on the PC gets deleted, it is generally not that complicated to just simply not delete any files that I do not want to delete.
But this is one of the bigger reasons I never did like windows, they do have files that even a administrator can not delete, and in many cases those files are NOT good for the system at all, even harmful, it is extremely frustrating to not be able to delete a file on your own system, PC, when you want to,...but I suppose that is another topic.

User avatar
Head_on_a_Stick
Posts: 14114
Joined: 2014-06-01 17:46
Location: London, England
Has thanked: 81 times
Been thanked: 133 times

Re: Making a File Undeletable

#15 Post by Head_on_a_Stick »

VonZorch wrote:I know making the file read only works but I may need to write to it.
Being able to write to a file is exactly equivalent to deleting a file, consider:

Code: Select all

# dd if=/dev/zero of=$file
Whilst $file may still be there, the contents will not ;)

So my advice would be to render the file immutable (to prevent accidental deletion by root), run `chattr -i` when you want to edit it then change it back afterwards.
deadbang

Post Reply