V
V
Vladimir2016-11-23 03:16:53
bash
Vladimir, 2016-11-23 03:16:53

What are the pitfalls in such a bash instruction?

Runs on Ubuntu:

PROJECT_PATH="/var/www/..."
....
find $PROJECT_PATH -type d -mtime -3 -mtime +1 | head -n -1 | xargs rm -r

What can happen if find doesn't find anything - it will pass an empty argument to rm?
Can such a script delete folders above itself (if, for example, $PROJECT_PATH falls under the find conditions?)?
How to protect such a script from deleting unnecessary things as much as possible?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Saboteur, 2016-11-23
@saboteur_kiev

replace "xargs rm -r" with "xargs echo rm -r" and check.
What is the task?
So far, the task is strange:
Delete all directories in /var/www that are older than 1 day but younger than 3 days, while leaving one random directory (since the output is not sorted, and which directory will be skipped will be implicitly determined by what will find find first)?
If you just delete everything that is older than 1 day, then maybe like this:

PROJECT_PATH="/var/www/project1"
find $PROJECT_PATH -mtime +1 -delete

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question