Shaw
Shaw

Reputation: 1139

Bash-Performing the same command on several directories

I want to create a script that will delete any files older than 7 days on a specified list of directories, but wondering what would be the best way to go about it...

I want to perform the following command on all directories specified:

find DIRECTORY_PATH -type f -mtime +7 -exec rm {} \;

Maybe an array holding a list of directories, and loop through each element in the array performing the find command on that?

Any help/advice would be appreciated.

Upvotes: 1

Views: 96

Answers (1)

fedorqui
fedorqui

Reputation: 290335

You can directly store all the directories in a file, say dirs.txt and loop through it:

while read dir
do
  find "$dir" -type f -mtime +7 -exec rm {} \;
done < dirs.txt

Upvotes: 2

Related Questions