First linux coding job.
Told to make a script for a chron job that dives through all folders on server and deletes any files over X days (probably 20).
I have tested the code below on my home Redhat box.
Of course I have many trepidations before I promote this code to actual usage. Code below works fine, BUT I also need it to delete ONLY files (and NOT directories) OVER "X" number of days.
Cannot download, make any modifications to the system of hundreds of directories and thousands of files. The first time I run this, I expect to delete over 10,000 files, as our clients leave their files on our servers and forget about them. (This will wake them up!)
I tried changing line five, "elif [ -f "$i" -a -mtime +21 ]; then" -remove file (rm -i "$1")
Doesn't work, saying "[: mtime: binary operator expected"
Do I need another elif statement, or can I make the selection criteria (files only and just those over "X" days) work on one line?
CODE:
#!/bin/bash
print_folder_recurse() {
for i in "$1"/*; do
if [ -d "$i" ]; then
print_folder_recurse "$i"
elif [ -f "$i" ]; then
rm -i $i #(put the -i in there for #testing, it will be removed)
fi
done
}
path "/go/to/your/happy/place"
echo "Delete path: $path"
print_folder_recurse $path
Thank you.
$1
and$i
in the same script. What is the$i
about? Does it work (better) if you switch to using$1
consistently?