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
;Since
ls
prints relative paths, use thexargs printf
thing to prepend the folder path and make it an absolute path;Send them to
sudo rm -rf
usingxargs
.
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.
printf -- '%s\0' /path/to/some/folder/* | head -zn -2 | xargs -0 rm
?/
in the name be created I am trying to achieve this back herels
output this is already a poorly written command, even with quoting.ls
also uses locale for sorting order, I think, so I don't see what's the purpose ofhead
in removing last 2 ( unless you're trying to get rid of.
and..
which iirc aren't allowed as arguments torm
anyway. Just usefind /path/to/folder -type f delete
. And nosudo
if you run from cron - cron is already at root level