0

I am a scripting novice. I am trying to write a simple bash script for my Ubunutu 10.04 server to delete archives in a folder older than 30 days & older than a year in a different folder. They can be two different command lines, as I will be putting them in different scripts.

I was just starting out by trying the following, which does not work at all:

# find ~/addon_backups/202 -name 202adata* -maxdepth 0 -ctime +30

and

# find ~/addon_backups/202/ME -name *.tar.gz -maxdepth 0 -ctime +365

I am getting the following respectively:

find: paths must precede expression: 202adata_010213.tar.gz
Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]

and

find: paths must precede expression: 0213ME-202.tar.gz
Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]

I have tried multiple variations and I am clearly missing something. Any help is appreciated.

AvatharV
  • 15
  • 2
  • [This][1] seems to answer your question: [1]: http://stackoverflow.com/questions/6495501/find-paths-must-precede-expression-how-do-i-specify-a-recursive-search-that – zzk Feb 26 '13 at 23:50

1 Answers1

2

You have to quote the parameter to -name so the shell won't interpret it (shellcheck automatically points that out):

find ~/addon_backups/202/ME -name "*.tar.gz" -maxdepth 0 -ctime +365

Also note that you'll want to use -mtime instead of -ctime, -maxdepth 1 instead of 0, and you can use -exec rm () {} + to delete:

find ~/addon_backups/202/ME -name "*.tar.gz" -maxdepth 1 -mtime +365 -exec rm {} +

Or more simply:

find ~/addon_backups/202/ME/*.tar.gz -mtime +365 -exec rm {} +
that other guy
  • 116,971
  • 11
  • 170
  • 194