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
Making a File Undeletable
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.
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.
Re: Making a File Undeletable
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.
There is no need to make simple things complicate.
- GarryRicketson
- Posts: 5644
- Joined: 2015-01-20 22:16
- Location: Durango, Mexico
Re: Making a File Undeletable
Simply change the permissions back to read/write, when you need to writeI know making the file read only works but I may need to write to it.
Is this possible, and if so how?
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
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/
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
$
Code: Select all
rm *.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
$
Code: Select all
$ ls
a-test.txt b-test.nodel b-test.txt
$ rm *.txt
$ ls
b-test.nodel
$
"What we expect you have already Done"
==========
Old Website
======================
For the Birds
==================
What Does a Parrot Know About PTSD?
==========
Old Website
======================
For the Birds
==================
What Does a Parrot Know About PTSD?
Re: Making a File Undeletable
Bash has except-option, but IMHO, it's better to plan so, that termporary files and permanent are not mixed.
Re: Making a File Undeletable
there are possibly many work-arounds for this.
for example, you could make the file hidden. AFAIK simple 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).
for example, you could make the file hidden. AFAIK simple
Code: Select all
rm *
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).
- Head_on_a_Stick
- Posts: 14114
- Joined: 2014-06-01 17:46
- Location: London, England
- Has thanked: 81 times
- Been thanked: 133 times
- GarryRicketson
- Posts: 5644
- Joined: 2015-01-20 22:16
- Location: Durango, Mexico
Re: Making a File Undeletable
Thanks H_O_A_S, this is a new command to me, I had never heard of it until now, so I looked atVonZorch 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----.
Code: Select all
man chattr
"but I may need to write to it." Of course , if and when they need to write to itA 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.
they could use "sudo" or "su", I suppose.
Seems like a option though, and not to complicated.
"What we expect you have already Done"
==========
Old Website
======================
For the Birds
==================
What Does a Parrot Know About PTSD?
==========
Old Website
======================
For the Birds
==================
What Does a Parrot Know About PTSD?
Re: Making a File Undeletable
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.
Re: Making a File Undeletable
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.
Re: Making a File Undeletable
Easiest way is don't delete it in the first place.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.
...
resigned by AI ChatGPT
-
- Posts: 1406
- Joined: 2009-09-10 20:15
- Location: Saint-Petersburg, Russian Federation
- Been thanked: 11 times
Re: Making a File Undeletable
Then you will not edit this file and change it, according "man chattr".Head_on_a_Stick wrote:Code: Select all
# chattr +i $file
Perhaps, "chatrr +u", but I not understand, will this work in Linux or not.
Peter.
- GarryRicketson
- Posts: 5644
- Joined: 2015-01-20 22:16
- Location: Durango, Mexico
Re: Making a File Undeletable
Code: Select all
man chattr
Err,??? It is supposed to be a Linux command, I would expect it to work on a Linux system,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.
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.
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.Then you will not edit this file and change it, according "man chattr".
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.
"What we expect you have already Done"
==========
Old Website
======================
For the Birds
==================
What Does a Parrot Know About PTSD?
==========
Old Website
======================
For the Birds
==================
What Does a Parrot Know About PTSD?
- 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
Being able to write to a file is exactly equivalent to deleting a file, consider:VonZorch wrote:I know making the file read only works but I may need to write to it.
Code: Select all
# dd if=/dev/zero of=$file
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