1

I am looking to have a script delete files that are older than 90 days in certain directories. As in, there are 8 directories that have different paths to them.

I can run the find command on one directory as such:

find /directory1/dir2/dir3/dir4/ -mtime +90 | wc -l

That command will give a result like 6401.

But I have 8 directories that need to be looked through. How could I build a list of directories as a variable. I have:

variable

DIRLIST=/directory1/dir2/dir3/dir4/ /directory1/dir2/dir3/dir5/ /directory1/dir2/dir3/dir6/ /directory1/dir2/dir3/dir6/ /directory1/dir2/dir3/dir7/

command 

find $DIRLIST | wc -l

The output gives me something along the lines of:

nameofscript.sh[2]: /directory1/dir2/dir3/dir5/: 0403-006 Execute permission denied.

Its strange because when I call that directory alone in the find command, the command works, so I know its not actually a permissions issue.

Any help would be greatly appreciated.

1

You declared $DIRLIST incorrectly

This is how to declare array variable

DIRLIST=(/directory1/dir2/dir3/dir4/ /directory1/dir2/dir3/dir5/ /directory1/dir2/dir3/dir6/ /directory1/dir2/dir3/dir6/ /directory1/dir2/dir3/dir7/)

Then run find command:

find "${DIRLIST[*]}" | wc -l

  • 1
    Array Variable! That is what I was looking for. This helped greatly, thank you! As I have multiple arrays specified, I didn't need to use the wildcard. Also, its an older version of AIX ( yay corporate red tape) so I used single quotes as opposed to the parens. Got it working. – Simply Jan 8 '16 at 6:44
  • 1
    Actually it should be "${DIRLIST[@]}". If you don't quote the variable, then word splitting will be performed on the array values. – gardenhead Jan 8 '16 at 8:26
  • 1
    @Simply With quotes around the value and spaces between values, you aren't defining an array, you're defining a string variable. If you then use an unquoted expansion (find $DIRLIST), which is the “glob+split” operator, that works for you, provided the directory names don't contain spaces or \[*?. – Gilles Jan 8 '16 at 23:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.