George Thompson
George Thompson

Reputation: 7057

unix script to search subdirectories for a file

This script checks if the exists in the and also if the modification date is less than 1 day. I have been trying to make it traverse the whole directory to look for the file I have tried test and find but am not very familiar with unix scripting.

please advise what the correct syntax or command is, the script works fine if the file I am looking for is in the upload directory.

Problem I would like to search all subdirectories for the file below the upload directory? :o) I am using a mac terminal as unix environment.

filename=help.csv

filedir="localhost/upload/"

cd $filedir

# if [ find . -name "$filename" ]; then
if [ -e "$filename" ]; then               # what should I add here to find the file in   any subdirectory of upload

  echo "File Exists"
  filestr=`find . -name $filename -mtime +1 -print`
  if [ "$filestr" = "" ]; then
    echo "File is not older than 1 day"
  else
    echo "File is older than 1 day"
  fi
else

  echo "File does not exist"
fi

exit 0

Upvotes: 0

Views: 292

Answers (1)

Andy Lester
Andy Lester

Reputation: 93805

You should just be able to start find in the upload directory.

find /path/to/upload/dir -name $filename -mtime +1 -print

The . you have now in your invocation just means "start looking in .", which is the current directory.

Upvotes: 2

Related Questions