I wrote a script that deletes all except the last two files in a folder:
#!/bin/bash ls -1 --quoting-style=shell-always /path/to/some/folder \ | head -n -2 \ | xargs printf -- "'/path/to/some/folder/%s'\n" \ | xargs sudo rm -rf
This script will be executed as a cron job every day.
The reasoning is as follows:
Obtain a list of all files using
ls -1(so that I get one file per line);
Remove the last two from the list using
head -n -2;
lsprints relative paths, use the
xargs printfthing to prepend the folder path and make it an absolute path;
Send them to
sudo rm -rfusing
Everyone has access to this folder, so anyone can create and delete any files in this folder.
The problem is:
sudo rm -rf is scary.
xargs sudo rm -rf is incredibly scary.
I want to be sure that no one can damage other folders/systems by creating clever files to be deleted (either accidentally or on purpose). I don't know, something clever like:
file with / spaces.txt
which could result in a super scary
sudo rm -rf /.
EDIT: My mistake, file names cannot contain
/, so this specific problem wouldn't happen, but the question about whether or not there are other risks still stands.
This is why I am using
--quoting-style=shell-always, this should prevent any tricks with files with spaces. But now I am wondering if someone could be extra clever with spaces and quotes in the filename, perhaps.
Is my script safe?
Note: I need
sudo because I am acessing the folder remotely (from a mapped network drive using
mount), and I couldn't get it to work without sudo.