4 Replies Latest reply: Jan 30, 2014 10:41 AM by rukbat RSS

Unix find - need nonrecursive solution

Greg.Spall Expert
Currently Being Moderated

Hey got another UNIX question.

I'm on Unix, in SunOS 5.10


I'm trying to use the find command to pick off some target files, however, it traverses all subfolders

recursively. I realize I could just use an "ls" solution, however, I also want to utilize the -mtime option of

find (to pick off only files older than - for example - 4 weeks.)


So I suppose my bigger question is: "What's the easiest way to search a single directory for files older than a

given time (ie # of days), and not recursively search child folders?"


So first off, a test case:


mkdir test

cd test


touch abc.txt

touch abc.sh

mkdir dir1

touch dir1/abc.sh


cd ..


find ./test -type f -name "*.sh"





So here, I just want to return:




Now doing some Googling, I found a few threads that mention some other options:


1) -maxdepth

   Tried this, doesn't seem to be there in my version of Unix/ksh

   find ./test -type f -name "*.sh" -maxdepth 1

   find: bad option -maxdepth


2) -xdev

   Only works on mount points, not a good/generic solution. (doesn't work for me, these aren't mount points)


3) -prune

   find dir \( ! -name dir -prune \) ...


   so :

   find ./test \( ! -name ./test -prune \) -type f -name "*.sh"

      (no results)

   find ./test \( ! -name "./test" -prune \) -type f -name "*.sh"

      (no results)


Any ideas?


  • Correct Answers - 10 points
  • Helpful Answers - 5 points