Welcome to the most active Linux Forum on the web.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 07-08-2009, 08:22 AM   #1
LQ Newbie
Registered: Feb 2009
Posts: 6

Rep: Reputation: 0
Question bash script: how to check stderr after command "find"

I use command "find" in my bash script: if the filename exist command find work quiet, and if the filename not exist I see the message "find: /tmp/filename: No such file or directory".

My problem is following, i want to have in my script something like this:

find "/tmp/filename" -type f -delete | "if no_any_errors execute command1" , if file_not_found execute command2"

Does it possible?
Old 07-08-2009, 08:35 AM   #2
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
The error message is due to the fact that you use the name of the file you want to look for as the search path of the find command. Usually find is in the form
find /tmp -name filename
this looks recursively inside the /tmp directory for a file called filename. Output is null if any file is found, otherwise the file names are printed out. In both cases the exit code of the command is 0. Anyway, using the file name in place of the search path, as in your example, the exit code is 1 due to the error and 0 otherwise.

However if you do
find "/tmp/filename" -type f
and a directory called /tmp/filename/ exists, the find command looks for any file inside that directory and the exit status is 0.

What I am trying to tell is that the find command is not suitable for the task you want to achieve, because the exit code of find is not strictly related to the existence of the file you're searching. If you're looking for the existence of a particular file, just use test -f and act accordingly to the result. For example:
if [ -f /tmp/filename ]
  echo file found
  echo file not found


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
bash script: using "select" to show multi-word options? (like "option 1"/"o zidane_tribal Programming 7 12-19-2015 02:03 AM
Standard commands give "-bash: open: command not found" even in "su -" and "su root" mibo12 Linux - General 4 11-11-2007 11:18 PM
Shell Script: Find "Word" Run "Command" granatica Linux - Software 5 07-25-2007 08:42 AM
Can't install "glibmm" library. "configure" script can't find "sigc++-2.0&q kornerr Linux - General 4 05-10-2005 03:32 PM
Bash Script: Problem running variable command containing "" Paasan Programming 2 01-21-2004 02:45 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:24 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