Share your knowledge at the LQ Wiki.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 09-12-2013, 04:17 PM   #1
LQ Newbie
Registered: Jul 2013
Posts: 1

Rep: Reputation: Disabled
Find command returning success even when no files are found

I've tested this on RHEL/OUL V5.7 and V6.2 with the same results. I need to search a directory for any files older than 25 hours and then do things with those files.

First case: there are no files named *.xmlx and find returns a 1 correctly
# find /mydir/*.xmlx -mmin +1500
find: /mydir/*.xmlx: No such file or directory
# echo $?

Second case: there are files named *.xml but none meet the mmin requirement yet find returns a 0 instead of a 1 as I was expecting
# find /mydir/*.xml -mmin +1500
# echo $?

This is messing up my scripting and I'm not figuring a way out.
Old 09-12-2013, 04:54 PM   #2
Senior Member
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,320

Rep: Reputation: 635Reputation: 635Reputation: 635Reputation: 635Reputation: 635Reputation: 635
find was returning 1 because you gave it a none existing file/dir

it doesn’t return an error simply because your nothing matches your criteria

what is it you want to do with the 'found' files?

you could pass to while read, and operate on them

dumb example..
find /mydir/*.xml -mmin +1500 | while read file;do
   echo "${file}"
   mv "${file}" "${file}.backup"
Old 09-12-2013, 04:58 PM   #3
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1980Reputation: 1980Reputation: 1980Reputation: 1980Reputation: 1980Reputation: 1980Reputation: 1980Reputation: 1980Reputation: 1980Reputation: 1980Reputation: 1980
From man find:
find exits with status 0 if all files are processed successfully, greater than 0 if errors occur. This is deliberately a very broad description, but if the return value is non-zero, you should not rely on the correctness of the results of find.
In the first case an error occurs since the search path is not found. On the other hand the command
find /mydir -name \*.xmlx -mmin +1500
would have returned an exit status of 0. This means that the exit status is not related to the results of the find command, but to the occurrence of errors that find might encounter while performing the search. Said that, you have to check something different from the exit status, for example the number of items found or you can simply do a loop over the find results. In this case, no results means no actions taken:
while read file
  echo $file
done < <(find /mydir -name \*xmlx -mmin +1500)
Hope this helps.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
IOCTL returning positive value on success Prathiba_S Linux - General 1 10-12-2011 10:32 AM
Can't log in despite ldappasswd returning success gxw Linux - Server 7 10-08-2009 04:46 AM
Find/grep/wc command to find matching files, print filename and word count dbasch Linux - Newbie 10 09-14-2009 05:55 PM
Single find command to find multiple files? thok Linux - Newbie 7 01-31-2009 04:45 PM
How to find files and copy the found files to the floppy in one command justmehere Linux - Newbie 11 05-04-2008 11:29 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:18 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration