Monday, April 29, 2024
20
rated 0 times [  20] [ 0]  / answers: 1 / hits: 4342  / 2 Years ago, fri, march 4, 2022, 12:27:42

I have a script, this script creates a dump in /path/of/directory and deletes it instantly. I want to copy that dump before it gets deleted.


I tried;


dump=$(ls -a | grep -E "^[0-9]+$")
cp $dump fulldump

But I only got a half of the script, not the full one.


More From » command-line

 Answers
6

You can use chattr to prevent file deletion (while allowing file creation and modification), and other handy permissions


For directory:


sudo chattr -R +a /TEMP/

or if just the (existing) file:


sudo chattr +a /TEMP/youCantDeleteMe.txt

To revert permissions use -a in place of +a


$ mkdir Documents/TEMP && cd $_
$ echo can edit > youCantDeleteMe.txt

$ sudo chattr -R +a ../TEMP/

$ echo after plusA on file>> youCantDeleteMe.txt
$ cat youCantDeleteMe.txt
can edit
after plusA on file

$ sudo rm youCantDeleteMe.txt
rm: cannot remove 'youCantDeleteMe.txt': Operation not permitted

$ ls -lA
-rw-rw-r-- 1 v v 25 Sep 5 05:59 youCantDeleteMe.txt
$ echo . > youCantDeleteMe.txt
bash: youCantDeleteMe.txt: Operation not permitted

$ touch somebody
$ ls -lA
-rw-rw-r-- 1 v v 0 Sep 5 06:04 somebody
-rw-rw-r-- 1 v v 25 Sep 5 05:59 youCantDeleteMe.txt

$ sudo rm somebody
rm: cannot remove 'somebody': Operation not permitted

$ sudo chattr +a ./undel.txt
chattr: No such file or directory while trying to stat ./undel.txt

And to later remove the file


$ sudo chattr -R -a ../TEMP/
$ ls -lA
drwxrwxr-x 2 v v 4096 Sep 5 06:17 .
drwxr-xr-x 7 v v 4096 Sep 5 05:57 ..
-rw-rw-r-- 1 v v 0 Sep 5 06:04 somebody
-rw-rw-r-- 1 v v 25 Sep 5 05:59 youCantDeleteMe.txt

$ rm -rf ./*
$ ls -la
drwxrwxr-x 2 v v 4096 Sep 5 06:17 .
drwxr-xr-x 7 v v 4096 Sep 5 05:57 ..


[#256] Saturday, March 5, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
bearous

Total Points: 226
Total Questions: 116
Total Answers: 136

Location: Guernsey
Member since Sun, Jan 10, 2021
3 Years ago
;