LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Debian
User Name
Password
Debian This forum is for the discussion of Debian Linux.

Notices


Reply
  Search this Thread
Old 05-12-2010, 06:04 AM   #1
tkmsr
Member
 
Registered: Oct 2006
Distribution: Ubuntu,Open Suse,Debian,Mac OS X
Posts: 798

Rep: Reputation: 39
strange behavior of find


Today while working with some file I had to search all files or directories that were beginning with red5
I was at that time in /usr/lib directory and issued
Code:
server:/usr/lib# find / -name red5*
and got following output.
Code:
/etc/init.d/red5-server
/etc/default/red5-server
/etc/red5
/etc/red5/red5.xml
/etc/red5/red5.policy
/etc/red5/red5-core.xml
/etc/red5/red5-common.xml
/etc/red5/red5.properties
/etc/red5/red5.globals
/usr/share/red5
/usr/share/red5/red5.jar
/usr/share/red5/red5-remoting.jar
/root/red5-server_0.9~svn3968-2_all.deb
/var/log/red5
/var/log/red5/red5.log
/var/run/red5-server.pid
/var/cache/apt/archives/red5-server_0.9~svn3968-2_all.deb
/var/lib/dpkg/info/red5-server.postrm
/var/lib/dpkg/info/red5-server.list
After some time
from /usr/share I executed
Code:
find / -name red5*
and the output had changed
Code:
/etc/red5
/usr/share/red5
/var/log/red5
I had not deleted any file in this duration.

I again excuted from my home directory.
Code:
find / -name red5*
The out put this time was even strange.
Code:
/root/red5-server_0.9~svn3968-2_all.deb
/var/cache/apt/archives/red5-server_0.9~svn3968-2_all.deb
Can some one let me know why is this happening?


I am using
2.6.26-2-xen-amd64

Last edited by tkmsr; 05-12-2010 at 06:07 AM.
 
Old 05-12-2010, 06:51 AM   #2
AlucardZero
Senior Member
 
Registered: May 2006
Location: USA
Distribution: Debian
Posts: 4,824

Rep: Reputation: 615Reputation: 615Reputation: 615Reputation: 615Reputation: 615Reputation: 615
Easy, your shell is expanding your wildcard. Quote it so that find gets it instead. "red5*"
 
Old 05-12-2010, 06:55 AM   #3
tkmsr
Member
 
Registered: Oct 2006
Distribution: Ubuntu,Open Suse,Debian,Mac OS X
Posts: 798

Original Poster
Rep: Reputation: 39
Yes even I wanted the shell to expand the wild card.But why are the results different when they are issued from different directories.In each case I am gave it to search from /
 
Old 05-12-2010, 07:08 AM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
It happened because when you issued the command in a directory with no files-or-directories matching red5* the shell was unable to expand it so left it as red5* and find looked under / for files matching red5*.

When you issued the command in a directory with files-or-directories matching red5* (let's assume you were in the /etc directory) the shell expanded it to red5 and find looked under / for files called exactly red5.

You can see what is happening by running echo red5* when /etc is your current diretory and when another directory without any red* files-or-directories like this
Code:
c@CW8:~$ cd /tmp
c@CW8:/tmp$ echo red*
red*
c@CW8:/tmp$ touch red5
c@CW8:/tmp$ echo red*
red5

Last edited by catkin; 05-12-2010 at 07:08 AM. Reason: Made example more pertinent
 
Old 05-12-2010, 07:21 AM   #5
tkmsr
Member
 
Registered: Oct 2006
Distribution: Ubuntu,Open Suse,Debian,Mac OS X
Posts: 798

Original Poster
Rep: Reputation: 39
Quote:
Originally Posted by catkin View Post
When you issued the command in a directory with files-or-directories matching red5* (let's assume you were in the /etc directory) the shell expanded it to red5 and find looked under / for files called exactly red5.

You can see what is happening by running echo red5* when /etc is your current diretory and when another directory without any red* files-or-directories like this
Code:
c@CW8:~$ cd /tmp
c@CW8:/tmp$ echo red*
red*
c@CW8:/tmp$ touch red5
c@CW8:/tmp$ echo red*
red5
If some one reads man page of find in examples it is given
find /tmp -name core
meaning if you have to find a file core
in directory /tmp you will execute it the above way.
it no where says find will stop searching if it finds the files in current directory.find need to search sub directories in /tmp as well.
Had it been the case it would not have given me results from other directories.

Last edited by tkmsr; 05-12-2010 at 07:23 AM.
 
Old 05-12-2010, 07:44 AM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by tkmsr View Post
it no where says find will stop searching if it finds the files in current directory.find need to search sub directories in /tmp as well.
Correct but not relevant to what is happening here.

The key concept here is what is passed to the find executable. Depending on which directory you are in, entering the command find / -name red5* at the command prompt results in a actual find command of find / -name red5* or find / -name red5
 
Old 05-12-2010, 08:24 AM   #7
tkmsr
Member
 
Registered: Oct 2006
Distribution: Ubuntu,Open Suse,Debian,Mac OS X
Posts: 798

Original Poster
Rep: Reputation: 39
Quote:
Originally Posted by catkin View Post
find / -name red5* at the command prompt results in a actual find command of find / -name red5* or find / -name red5
Exactly this is what I am also saying.But output changed when I issued from different directories.What had directories to do
when I had already passed /
to start search from /

The output should be same which ever directory I issue.
Unless I say some thing like
Code:
find . -name red5*
 
Old 05-12-2010, 08:30 AM   #8
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
The important fact the OP seems to be ignoring is that the shell interprets red5* before passing it to whatever command (find in this case) was specified.

If there are any files in the shell's current directory matching red5*, then the shell will replace the red5* in the command line with the list of such files.

Quote:
Originally Posted by tkmsr View Post
If some one reads man page of find
The man page for each individual command is not supposed to remind you of behaviors of the shell. It tells you what find does with the command that the shell passes to find.

Quote:
Originally Posted by tkmsr View Post
What had directories to do
when I had already passed /
to start search from /
That / tells find where to search. It does not mean anything to the shell, especially not to the shell's processing of red5*.

As you were already told earlier in this thread, if you want find to get the red5* without the shell processing it first, use "red5*"

Last edited by johnsfine; 05-12-2010 at 08:35 AM.
 
Old 05-12-2010, 08:31 AM   #9
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by tkmsr View Post
Exactly this is what I am also saying.But output changed when I issued from different directories.What had directories to do
when I had already passed /
to start search from /

The output should be same which ever directory I issue.
Unless I say some thing like
Code:
find . -name red5*
No because if there is a file-or-directory matching red5* in the directory you are in, the shell itself will expand red5* to its name. In your case there was a file called red5 so find never saw red5*, it only saw red5 and did what it was told to do -- it looked everywhere under / for red5 and reported what it found. Try these commands which will illustrate the how the shell does filename expansion
Code:
mkdir /tmp/my_tmp
cd /tmp/my_tmp
echo red*
touch red5
echo red*
 
  


Reply



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
strange behavior of find or grep alenD Programming 4 09-21-2007 06:35 AM
Very Strange Behavior raysr Mandriva 4 08-31-2004 02:06 PM
qmail's strange behavior Madrinator Linux - Networking 2 10-03-2003 05:30 PM
Strange Behavior andrewb758 Linux - Hardware 5 08-31-2003 02:42 PM
strange behavior abhijit Linux - General 3 07-09-2003 11:25 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Debian

All times are GMT -5. The time now is 05:00 PM.

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