简体   繁体   中英

bash script for loop

I'm trying to basically look at a directory that has sub-directory for each user. Say user1 and user2 and so on. I want to basically go through each directory and look for files older than a certain date and log it. I'm able to get a list of directories with ls -d /directory/*/. I know I can use find ./ -type f -mtime +30 to look for files that haven't been modified more than 30 days. I'm having trouble figuring out how to use the find so each time through it will look at like find ./user1 -type f -mtime30 and then ./user2 and so on.

#!/bin/bash
LIST="$(ls -d /Volumes/db_backups/*/)"
for i in "$LIST";do
        fold=`basename $i`
        echo $fold
        ModList=$(find $i -type f -mtime +30 >$fold.out )
        $ModList
done

Update: I was able to get a list of files. However, I want to not loop through a specific sub-directory and to email each user. For instance, I have a file being logged like /dir1/dir2/user1/file.txt. How would I grab the user1 list and put it in a user1.out and then user2.out and so on?

You can supply multiple paths to find :

find directory/* -type f ...

Here directory/* would expand to directory/user1 directory/user2 , in effect giving multiple paths to find .

edit On second thoughts, you should be able to just use find directory -type f (hat tip @anubhava).

If you really want to run a separate process for each subdirectory, then this is what you want:

cd /Volumes/db_backups
for entry in *
do
  if [[ -d "${entry}" ]]                       # skip things that aren't directories
  then
    find "${entry}" -type f -mtime +30 -print > /some/log/directory/"${entry}".output
  fi
done

The if ... fi bit could be shortened to [[ -d "${entry}" ]] && find ... , which is more concise but not quite as explicit about your intention.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM