Find file only from a Directory

Hi,

I am using the below query to find files from a Directory:

SOURCE_DIR--Directory

Files in the Directory:

ABC.log
XYZ.out

The SOURCE_DIR has subdirectory....

when i use the below command...

find /apps/informatica/node1/scripts/test '(' -name '.log' -o -name '.out' ')'

Its displaying the files from SOURCE_DIR along with files from SubDirectory.

My requirement is i want only files from SOURCE_DIR not the Sub direcotry files..

Thanks in advance

Since you're on Linux, just add -maxdepth 1 before the '('

1 Like

find takes an argument -maxdepth. Here is what the manpage says about maxdepth:

So, you can use -maxdepth 1 as an additional arg to you find command.

  • GP

That option's specific to GNU find, usually only available in Linux

Thanks Corona688....It worked....

@Carona688, thanks for pointing that out. Never having used it before, I assumed that -depth works similarly on Unix. Just found out, otherwise. So how would one go about doing something similar in, say, HP-UX?

Touchy fiddling with the -prune option, perhaps. I have yet to manage a 1:1 equivalent.

You find this sort of thing all the time in UNIX vs Linux utilities. Small little GNU extensions to utilities for pretty obvious features that the UNIX standard doesn't cover, often bafflingly difficult to replace. The -d option to GNU date, which prints whatever date you tell it to, in particular is badly missed...

Hi,
The above solution has worked prefectly as expected....
Incase if i want to proceed with moving the files from Source to Target , only if the log file exits,if not do not do the archive but proceed with the next step in the script.
how can this be achived
Below is the orignal query that i am using:
I have used an "if condtion" to check the status of previous, however it always return 0.

find $LOGDIR -maxdepth 1 \( -name "*.log" -o -name "*.out" \) |
if [ $? != 0 ]; then
  echo "No Log Files to Achrive"
else
  while IFS="." read FILE
  do
    BASE=`basename "$FILE"`
    echo "$BASE" >/tmp/$$
    IFS="." read BASE EXT < /tmp/$$
    mv "$FILE" "$LOGARCHIVE/$BASE.$datetimestamp.$EXT"
    if [ $? != 0 ]
    then
      echo "! Error - Error archiving log file !"
    fi
  done
fi

Sorry, what? I don't think that translated very well.

Hi Corona688,

Sorry i could convey my message correctly......

In my query the "Find" is checking for files with ".log" and ".out" in the LOG_DIR,

I wanted to proceed with next steps only if the files exists.
If not exit from the while loop.

I tried checking with status of the previous command using if..but its not working.....

Please suggest.

So it should only move in/file.ext to out/file-date.ext when out/file-date.ext already exists? :confused:

If not that, then, when what already exists? The file you haven't moved yet? It already exists by definition...

if file.ext exits in Dir "in" then move it to out/file-date.ext.

else simply quit from the while loop....

find never prints nonexistent files. If find finds no files at all, it prints exactly nothing.

If the while read loop reads nothing the very first time, the code inside it won't run even once.

That's why your error-checking code doesn't report any errors -- there weren't any.

It's not useless, though. You might be unable to move the files for other reasons, like directory permissions.

1 Like