0

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?

3
  • Are you trying to split a file by a substring of the paths in it? Commented Jan 8, 2014 at 15:52
  • I think i'm getting close. I added this to my for loop. for i in "$LIST";do fold=basename $i So basically I want to create output files for each subdirectory that has a list of files older than x days. Then I think I can basically email each output file to each user. Commented Jan 8, 2014 at 15:59
  • Please add your actual code to the question. Commented Jan 8, 2014 at 16:00

2 Answers 2

1

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).

Sign up to request clarification or add additional context in comments.

1 Comment

Is this different from find directory -type f?
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.