Making a File Undeletable

If none of the more specific forums is the right place to ask

Making a File Undeletable

Postby VonZorch » 2018-07-16 09:27

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.
VonZorch
 
Posts: 2
Joined: 2018-07-16 09:05

Re: Making a File Undeletable

Postby arzgi » 2018-07-16 10:09

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.
arzgi
 
Posts: 284
Joined: 2008-02-21 17:03
Location: Finland

Re: Making a File Undeletable

Postby roseway » 2018-07-16 10:56

... or assign the ownership of the file to root?
Eric
User avatar
roseway
 
Posts: 1493
Joined: 2007-12-31 22:50
Location: Kent, UK

Re: Making a File Undeletable

Postby GarryRicketson » 2018-07-16 12:17

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
User avatar
GarryRicketson
 
Posts: 4956
Joined: 2015-01-20 22:16
Location: Durango, Mexico

Re: Making a File Undeletable

Postby arzgi » 2018-07-16 13:49

Bash has except-option, but IMHO, it's better to plan so, that termporary files and permanent are not mixed.
arzgi
 
Posts: 284
Joined: 2008-02-21 17:03
Location: Finland

Re: Making a File Undeletable

Postby VonZorch » 2018-07-16 17:42

Thanks all, I suspected it wasn't possible.
VonZorch
 
Posts: 2
Joined: 2018-07-16 09:05

Re: Making a File Undeletable

Postby pylkko » 2018-07-16 18:21

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
pylkko
 
Posts: 1314
Joined: 2014-11-06 19:02

Re: Making a File Undeletable

Postby Head_on_a_Stick » 2018-07-16 20:19

Code: Select all
# chattr +i $file
E Pluribus Unix
User avatar
Head_on_a_Stick
 
Posts: 7777
Joined: 2014-06-01 17:46
Location: /dev/chair

Re: Making a File Undeletable

Postby GarryRicketson » 2018-07-17 00:15

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
GarryRicketson
 
Posts: 4956
Joined: 2015-01-20 22:16
Location: Durango, Mexico

Re: Making a File Undeletable

Postby pylkko » 2018-07-17 02:43

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: 1314
Joined: 2014-11-06 19:02

Re: Making a File Undeletable

Postby wizard10000 » 2018-07-17 16:43

GarryRicketson wrote:they could use "sudo" or "su", I suppose.
Seems like a option though, and not to complicated.


root can't write to an immutable file without removing the +i attribute :(
we see things not as they are, but as we are.
-- anais nin
User avatar
wizard10000
 
Posts: 1285
Joined: 2011-05-09 20:02
Location: everywhere i go, there i am!

Re: Making a File Undeletable

Postby pylkko » 2018-07-17 17:06

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
pylkko
 
Posts: 1314
Joined: 2014-11-06 19:02

Re: Making a File Undeletable

Postby bw123 » 2018-07-17 17:50

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.
User avatar
bw123
 
Posts: 3189
Joined: 2011-05-09 06:02
Location: TN_USA

Re: Making a File Undeletable

Postby peter_irich » 2018-07-17 17:51

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.
peter_irich
 
Posts: 1228
Joined: 2009-09-10 20:15
Location: Saint-Petersburg, Russian Federation

Re: Making a File Undeletable

Postby GarryRicketson » 2018-07-17 19:10

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
GarryRicketson
 
Posts: 4956
Joined: 2015-01-20 22:16
Location: Durango, Mexico

Next

Return to General Questions

Who is online

Users browsing this forum: No registered users and 10 guests

fashionable