Tuesday, 15 April 2014

linux - Correct syntax for executing commands with arguments in find -exec -



linux - Correct syntax for executing commands with arguments in find -exec -

i have line in script supposed pruning old backups, except throws error target directory:

find $backup_dir* -mtime $retention -exec rm {} \; >> $msg 2>&1

so trying add together -rf rm command, it's still not working [no errors!]

find $backup_dir* -mtime $retention -exec "rm -rf" {} \; >> $msg 2>&1

what right syntax command? i've tried double quotes, single quotes , backticks... have escape -rf somehow?

i typically prefer -0 xargs method:

find ... -print0 | xargs -0r rm -rf

this does, work commands take 1 or multiple arguments till end of line. quite safe takes binary junk in file name , nowadays nicely separated arguments. avoids having remeber {} syntax. reduces number of kid executions. -r alternative needed in gnu variant create sure not phone call command if no match found. freebsd ignored (as default).

in case, there -delete action gnu find:

find ... -delete

if insist on using escape method, be:

find ... -exec /bin/rm -rf "{}" \;

or if using gnu find, can bit more efficient with:

find ... -exec /bin/rm -rf "{}" +

and sure, utilize -ok :)

touch test1 test2 test3 find . -type f -ok rm -rf "{}" + "rm -rf ./test1 ./test2 ./test3"? y

and reply question: no don't have escape commands options. if not work, problem somewhere else, must in startdir & search pattern. seek without output redirection , using -print or -ok debug.

linux bash

No comments:

Post a Comment