3

I've written a script to iterate though a directory in Solaris. The script looks for files which are older than 30 minutes and echo. However, my if condition is always returning true regardless how old the file is. Someone please help to fix this issue.

for f in `ls -1`;
# Take action on each file. $f store current file name
do
  if [ -f "$f" ]; then
  #Checks if the file is a file not a directory
  if test 'find "$f" -mmin +30'
  # Check if the file is older than 30 minutes after modifications
  then
     echo $f is older than 30 mins
  fi
 fi
 done
user3484214
  • 41
  • 1
  • 1
  • 3

3 Answers3

4
  1. You should not parse the output of ls
  2. You invoke find for every file which is unnecessarily slow

You can replace your whole script with

find . -maxdepth 1 -type f -mmin +30 | while IFS= read -r file; do
    [ -e "${file}" ] && echo "${file} is older than 30 mins"
done

or, if your default shell on Solaris supports process substitution

while IFS= read -r file; do
    [ -e "${file}" ] && echo "${file} is older than 30 mins"
done < <(find . -maxdepth 1 -type f -mmin +30)

If you have GNU find available on your system the whole thing can be done in one line:

find . -maxdepth 1 -type f -mmin +30 -printf "%s is older than 30 mins\n"
Adrian Frühwirth
  • 42,970
  • 10
  • 60
  • 71
  • HI Adrian, when I do the first option I get the following error. `find: bad option -maxdepth find: [-H | -L] path-list predicate-list` Ideally I want to move the files into another directory. I am just echoing here to test the if condition. – user3484214 Apr 01 '14 at 09:06
  • Ah, so Solaris' `find` doesn't have `-maxdepth` either. Well, you could use `find . ! -name . -prune -type f -mmin +30` to emulate it. – Adrian Frühwirth Apr 01 '14 at 09:12
  • You could also get rid of results from subdirectories by eliminating paths with more than two slashes `/` via `find . -type f -mmin +30 | sed '/[/].*[/]/d'` but this might be even slower if you have lots of files/directories in deeper levels. – Adrian Frühwirth Apr 01 '14 at 09:21
  • That didn't work either. I found the issue though. 1. Solaris doesn't have the `-mmin` option. It only has `mtime`. 2. For some reason my `test` command is not working and it always returns `true`. However, when I type `find -name $FILENAME -type f -mtime +1` to the shell it returns the name of the file if it's older than a day. – user3484214 Apr 02 '14 at 01:45
1

Another option would be to use to check the time. Something like below should work.

for f in *
# Take action on each file. $f store current file name
do
  if [ -f "$f" ]; then
    #Checks if the file is a file not a directory
    fileTime=$(stat --printf "%Y" "$f")
    curTime=$(date +%s)
    if (( ( ($curTime - $fileTime) / 60 ) < 30 ))
      echo "$f is less than 30 mins old"
    then
      echo "$f is older than 30 mins"
    fi
  fi
done
Reinstate Monica Please
  • 11,123
  • 3
  • 27
  • 48
  • I would agree, unfortunately `stat` is not portable and not readily available on all Solaris versions (Solaris has its own tool called `truss`) so this probably won't work. – Adrian Frühwirth Apr 01 '14 at 08:59
  • @AdrianFrühwirth That's disappointing. Could parse `ls` for time only in this case, though I think your solution is better. – Reinstate Monica Please Apr 01 '14 at 09:30
1

Since you are iterating through a directory you could try the below command which will find all files ending with log type edited in the past 30 min. Using:

  • -mmin +30 would give all files edited before 30 minutes ago

  • -mmin -30 would give all files that have changed within the last 30 minutes

 

find ./ -type f -name "*.log" -mmin -30 -exec ls -l {} \;
mwfearnley
  • 3,303
  • 2
  • 34
  • 35
Melanie
  • 11
  • 1