Re: find in root (286 Views)
Reply
Advisor
Reen
Posts: 18
Registered: ‎03-12-2007
Message 1 of 13 (286 Views)

find in root

hi,
I have a file which contains a list of filenames which i have to search in the whole filesystem (root /). I have used a script like
cat file|while read FILENAME
do
find / -name $FILENAME
done
But this is running damn slowly and i fear it may take years to complete this command.
Any one help me out plzzz
Esteemed Contributor
Hemmetter
Posts: 284
Registered: ‎10-17-2000
Message 2 of 13 (286 Views)

Re: find in root

Hi Reen@


What about:

# find / -xdev | grep -f file


rgds
HGH
Honored Contributor
Luk Vandenbussche
Posts: 2,734
Registered: ‎01-04-2000
Message 3 of 13 (286 Views)

Re: find in root

Hi,

find / -xdev -name $FILENAME
Honored Contributor
Rasheed Tamton
Posts: 965
Registered: ‎05-18-2002
Message 4 of 13 (286 Views)

Re: find in root

hi,

find / -xdev -type f -name $FILENAME

(type f will only look for normal files)



Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 5 of 13 (286 Views)

Re: find in root

Hi:

If you truly mean the whole file system, visting every mountpoint, then:

# find / -type f -name ...

...will do that AND potentially take a long time.

If you don't want to visit mountpoints then add the '-xdev' switch as noted:

# find / -xdev -type f -name ...

Notice that in either case I specified only *files* with the '-type f' argument. Drop that if you truly want both files and directories.

Lastly you can reduce the number of searches by doing something like:

# find / -xdev -type f -name myfile -o -name yourfile -o -name "her*" -o -name "his*"

That is, look for files by individual name and perhaps using wildcards. Be sure to quote the metacharacters so that the shell doesn't interpret them.

Regards!

...JRF...
Honored Contributor
Yogeeraj_1
Posts: 4,613
Registered: ‎11-25-2001
Message 6 of 13 (286 Views)

Re: find in root

hi Reena,

You may also consider breaking you code into several parts so that you can launch them in parallel.

kind regards
yogeeraj
No person was ever honoured for what he received. Honour has been the reward for what he gave (clavin coolidge)
Honored Contributor
Bill Hassell
Posts: 14,227
Registered: ‎05-29-2000
Message 7 of 13 (286 Views)

Re: find in root

NEVER, EVER run find on the root directory for a production machine. Of course it will be slow -- you are examining every directory including CDROMs, network filesystems, database files, etc, etc. Why would you look for a file like that? If a user can't find their file, there are VERY few places where the user can create a file, namely: $HOME /var/tmp and /tmp, so your command changes to:

find /home/some_user /var/tmp /tmp -name $FILENAME

If you are looking for a particular command, start with the man pages, then look in the system directories and application directories:

find /usr/bin /usr/sbin /use/lbin /usr/local /usr/contrib /opt/*/bin -name $FILENAME

A typical HP-UX system installation has several thousand files in several hundred directories, most of which you never need to search.

Is there a reason you need to look in every directory in the entire machine?
Acclaimed Contributor
A. Clay Stephenson
Posts: 17,825
Registered: ‎07-16-1998
Message 8 of 13 (286 Views)

Re: find in root

If you nevertheless must run find over a large portion of your system'm filetree then I suggest a Plan B:

1) Run find once and write the output to a temporary file - preferably with filters to restrict it to specific filesystems and to restrict it to regular files.
2) Scan this list of files using grep to find the matches.

If it ain't broke, I can fix that.
Advisor
Reen
Posts: 18
Registered: ‎03-12-2007
Message 9 of 13 (286 Views)

Re: find in root

Its not tat i want to search in root. I just gave an example. Cos the directory am searching in has many subdirectiories and it is taking lot of time in searching for all the files in all these directories.. any faster way of searching?
Honored Contributor
Sandman!
Posts: 2,220
Registered: ‎01-13-2005
Message 10 of 13 (286 Views)

Re: find in root

Look for ways to narrow down your search for ex. search only 3 filesystems out of however many are present on the server or the filenames have a common extension "*.c" or run the command when the system is under minimal load.

~hope it helps
Acclaimed Contributor
Dennis Handly
Posts: 25,303
Registered: ‎03-06-2006
Message 11 of 13 (286 Views)

Re: find in root

[ Edited ]

>any faster way of searching?

As HGH, JRF and Clay (Plan B) have suggested, you want to do one find, then grep with your list of files. Your original while/find would do N finds.

One advantage of Plan B is that you can look for a filename that wasn't in your "file".

 

> find / -xdev -type f -name myfile -o -name yourfile -o -name "her*" -o -name "his*"

 

If you are going to mix AND with OR, you need to use ():

# find / -xdev -type f \( -name myfile -o -name yourfile -o -name "her*" -o -name "his*" \)

Honored Contributor
Rasheed Tamton
Posts: 965
Registered: ‎05-18-2002
Message 12 of 13 (286 Views)

Re: find in root

Hi,

You can add -type f (type is file, -d dir, etc.)
(find / -type f -name $FILENAME)
or even add -mtime -12 (within the last 12 days) or +12 (12 + days)
(find . -type f -mtime -1300 -name $FILENAME)

to make it faster the search operation.
Also see the -atime, -mtime and -user (owner of the file, etc to make your search faster.

See man find

Rasheed Tamton.
Honored Contributor
Steven Schweda
Posts: 9,096
Registered: ‎02-23-2005
Message 13 of 13 (286 Views)

Re: find in root

> [...] i have to search in the whole
> filesystem (root /).

> Its not tat i want to search in root. I
> just gave an example.

Sometimes it's helpful to ask the question
for which you actually want the answer.

> cat file|while read FILENAME

Knowing, even approximately, what is in
"file" might also be helpful in deciding
which of the techniques already suggested
would be good or bad.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.