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

 

 

 

Script for Wrapping "rm -rf " in chosen Folders

Programming languages, Coding, Executables, Package Creation, and Scripting.
Post Reply
Message
Author
User avatar
bester69
Posts: 2072
Joined: 2015-04-02 13:15
Has thanked: 24 times
Been thanked: 14 times

Script for Wrapping "rm -rf " in chosen Folders

#1 Post by bester69 »

Hi,

I'd like you to help me with an easy script to wrap "rm -rf" when i exec this command to prevent to erase some critical folders.

im sure you can get me, or point me to the code for a similar wrap :arrow:

Thanks,.

Ejemp.

Code: Select all

/home/user/CarlaCV.doc
/home/user/CarlaPic.png
/home/user/CarlaMAIN/

rm -rf ~/Car*
Accidendaly, forgot "CarlaMAIN" folder, and we dont want this to happend :oops:

Flow Script:

Code: Select all

rm -rf  {list folders} -> 
WRAP script (myrm.sh) -->  rm -rf {list folders | Excluding | list folders FORBIDDEN} -->
echo "WARNING --> Wrapping Operation has been commit!!
bester69 wrote:STOP 2030 globalists demons, keep the fight for humanity freedom against NWO...

User avatar
edbarx
Posts: 5401
Joined: 2007-07-18 06:19
Location: 35° 50 N, 14 º 35 E
Been thanked: 2 times

Re: Script for Wrapping "rm -rf " in chosen Folders

#2 Post by edbarx »

Code: Select all

rm -rf
If you don't want to accidentally shoot yourself in the head, don't play Russian Roulette.
Debian == { > 30, 000 packages }; Debian != systemd
The worst infection of all, is a false sense of security!
It is hard to get away from CLI tools.

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

Re: Script for Wrapping "rm -rf " in chosen Folders

#3 Post by GarryRicketson »

That is not a good idea to be doing it this way,
see :

Code: Select all

~$ man rm 
The -r option ,... recursively
The -f option -f, --force (force-remove all files without prompting you)
followed with / ,....logic should tell you what could happen.
but in case it does not :
/ – Tells rm to start at the root directory, which contains all the files on your computer and all mounted media devices, including remote file shares and removable drives.
Some times it is hard to tell if this "bester69" is serious or joking, I think maybe he is joking in this case,...
by bester69 » I'd like you to help me with an easy script to wrap "rm -rf" when i exec this command to prevent to erase some critical folders.
"to prevent to erase some critical folders",.... this command can remove everything,and
will, and not even give a warning or ask you Yes or no,...
Look at the manual on 'rm'
-f, --force
ignore nonexistent files, never prompt

-r, -R, --recursive
remove directories and their contents recursively

-v, --verbose
explain what is being done
To prevent removing critical files, you would want to use:

Code: Select all

rm -r -v /this/orthat/directory/andfiles.*
Or some thing a long that line, would be safe to use, and
it would prompt you, verbally, just to make sure you really
want to remove the directory and the files, and subdirectories in it.
That way, if there are any "critical files", or directories that happen to
be in the path, you still have a chance to cancel the operation.
Yes I do realize /home/joe/thisfile, in theory should keep it from
starting at / ,.... in theory, it would only remove the files and sub directories that the "path" points to,
"thisfile",..... however a tiny typo, or space, where you shouldn't have had
one, and combined with the fact that it is forced, with no warnings,...How do you plan to test the "script" ? Hopefully on a VM,... because if there is a mistake, it could just remove everything.
edbarx wrote:

Code: Select all

rm -rf
If you don't want to accidentally shoot yourself in the head, don't play Russian Roulette.
Is the perfect analogy. ( I think "analogy" is the right word" )
Any way that is about what it boils down to.

User avatar
bester69
Posts: 2072
Joined: 2015-04-02 13:15
Has thanked: 24 times
Been thanked: 14 times

Re: Script for Wrapping "rm -rf " in chosen Folders

#4 Post by bester69 »

GarryRicketson wrote:That is not a good idea to be doing it this way,
see :

Code: Select all

~$ man rm 
The -r option ,... recursively
The -f option -f, --force (force-remove all files without prompting you)
followed with / ,....logic should tell you what could happen.
but in case it does not :
/ – Tells rm to start at the root directory, which contains all the files on your computer and all mounted media devices, including remote file shares and removable drives.
Some times it is hard to tell if this "bester69" is serious or joking, I think maybe he is joking in this case,...
by bester69 » I'd like you to help me with an easy script to wrap "rm -rf" when i exec this command to prevent to erase some critical folders.
"to prevent to erase some critical folders",.... this command can remove everything,and
will, and not even give a warning or ask you Yes or no,...
Look at the manual on 'rm'
-f, --force
ignore nonexistent files, never prompt

-r, -R, --recursive
remove directories and their contents recursively

-v, --verbose
explain what is being done
To prevent removing critical files, you would want to use:

Code: Select all

rm -r -v /this/orthat/directory/andfiles.*
Or some thing a long that line, would be safe to use, and
it would prompt you, verbally, just to make sure you really
want to remove the directory and the files, and subdirectories in it.
That way, if there are any "critical files", or directories that happen to
be in the path, you still have a chance to cancel the operation.
Yes I do realize /home/joe/thisfile, in theory should keep it from
starting at / ,.... in theory, it would only remove the files and sub directories that the "path" points to,
"thisfile",..... however a tiny typo, or space, where you shouldn't have had
one, and combined with the fact that it is forced, with no warnings,...How do you plan to test the "script" ? Hopefully on a VM,... because if there is a mistake, it could just remove everything.
edbarx wrote:

Code: Select all

rm -rf
If you don't want to accidentally shoot yourself in the head, don't play Russian Roulette.
Is the perfect analogy. ( I think "analogy" is the right word" )
Any way that is about what it boils down to.
I think, in console most of users use "rm -rf" to erase folders, unless ive allways done this way, and up to date there has been not any problem.

An easy way it happens to me right now, would be :

Code: Select all

rm -rf Car*--> wrap myrm.sh:
rsync -av --dry-run --delete emptyfolder/  Car* |tee > filestodelete.txt

if myProtectedFolder in filetodelete.txt 
then echo "Warning !!, stop  commited rm, a protected folder has been tried to erased"
else  "rm -rf Car*"

I really would like to have this warpping script in my computer installed.
bester69 wrote:STOP 2030 globalists demons, keep the fight for humanity freedom against NWO...

User avatar
edbarx
Posts: 5401
Joined: 2007-07-18 06:19
Location: 35° 50 N, 14 º 35 E
Been thanked: 2 times

Re: Script for Wrapping "rm -rf " in chosen Folders

#5 Post by edbarx »

Our warning, mine and Garry's, are intended to help you avoid deleting your installation accidentally. If you are responsible and know exactly what you are doing, creating a script with your posted instructions is easy.

Copy your instructions to a text editor adding this line at the top:

Code: Select all

#!/bin/sh
This tells the OS you want sh to run the your script.

Before you can run your script you will need to make the file containing it executable. This is done as follows:

Code: Select all

chmod 555 your-script-file.sh
To install it, use move as root to a directory like /bin or /usr/bin. I suggest you use /usr/bin but be aware apt will not know anything about your new script.

After following these steps you will be able to run your script by typing its name in a shell like all other programs.

Alternatively, but harder, you can create a simple package with your script and use dpkg -i to install it just like official packages. That way, apt will know about your script and your custom package.

To create a simple empty source package containing only the debian subdirectory use dh_make. First create a directory for the sources, like:

Code: Select all

nuke-them-0.1.0
This will create the debian subdirectory and fill it will a minimal working skeleton of template files. Now, copy your script to the nuke-them-0.1.0 directory with the proper file permissions.

You will need to do some file renaming in debian/ and edit at least one file to have your script installed by the package.

The final step is to call dpkg-buildpackage to build your own custom package.

bester69, shall we go for it? 8)
Debian == { > 30, 000 packages }; Debian != systemd
The worst infection of all, is a false sense of security!
It is hard to get away from CLI tools.

sgosnell
Posts: 975
Joined: 2011-03-14 01:49

Re: Script for Wrapping "rm -rf " in chosen Folders

#6 Post by sgosnell »

I don't think many people are using rm -rf very often, if at all. I do use rm -r to delete directories, but I'm careful about which ones I use, and where I start.
Take my advice, I'm not using it.

User avatar
bester69
Posts: 2072
Joined: 2015-04-02 13:15
Has thanked: 24 times
Been thanked: 14 times

Re: Script for Wrapping "rm -rf " in chosen Folders

#7 Post by bester69 »

This is a first aproach, im sure you can improvet it :D

Code: Select all

#!/bin/bash
#
ruta=$(readlink -f "$2")
ruta2=$(dirname "$ruta")
borrado="$2"

rm -rf /tmp/xxzzyy_empty
mkdir /tmp/xxzzyy_empty
cd /tmp/xxzzyy_empty
mkdir empty

clear
echo "ruta es: $ruta"
echo "borrado es: $borrado"
echo "ruta2 es: $ruta2"
echo "rsync -av --dry-run --delete empty/ $ruta2/$borrado"
sleep 5
rsync -av --dry-run --delete empty/ $ruta|awk  ' {print  "'$ruta'/"$2}'| tee listfiles
sleep 5

for sel in $(cat listfiles)
do    
dir1="/home/$USER/temp/libva/va/"
dir2="xYxU"
dir3="xYxU"

if [[ $sel == *"$dir1"* ]]; then
	echo "Carpeta $dir1 está protegida!!"
        exit
elif [[ $sel == *"$dir2"* ]]; then
	echo "Carpeta $dir2 está protegida!!"
        exit
elif [[ $sel == *"$dir3"* ]]; then
	echo "Carpeta $dir3 está protegida!!"
        exit

        else
    echo "esto es Correcto: $sel"
fi
done

echo "rm $1 $ruta"
bester69 wrote:STOP 2030 globalists demons, keep the fight for humanity freedom against NWO...

Post Reply