Sunday, April 4, 2010

What to do when you don't have locate tool

What to do when you want to find certain file(s) and you don't have the locate tool? Probably you are in Solaris, AIX, or other variants of UNIX that doesn't have the locate tool. Probably your Linux sysadmin just don't like the locate tool to be there?
May be you are in the support team and now that there is no fancy locate tool you still need to find where the file is located.

Someone at the mailing list asked about the question, so I will write it here so that everybody can benefit from it.

What we can do depends on the frequency of locating such files. If we are to mimic the slocate / mlocate (ubuntu) background process, this what I would suggest (assuming you have /root folder and have root equivalent access):

[sourcecode lang="shell"]<br /># find / -print &gt; /root/locations.txt &amp;<br />[1] 30851<br />[/sourcecode]



The number "30851" is the process number for the background process. You need to wait for a while until the background process completed. When it completes, it should display something like:

[sourcecode lang="shell"]<br />[1] Done        find / - print &gt; /root/locations.txt<br />[/sourcecode]


Whenever you want to search file name containing "tomcat-6.0", just run this command:

[sourcecode lang="shell"]<br /># grep "tomcat-6.0" /root/locations.txt<br />/home/dbaktiar/Downloads/apache-tomcat-6.0.24.zip<br />/home/dbaktiar/Downloads/apache-tomcat-6.0.26.tar.gz<br />[/sourcecode]


If you are using Ubuntu or sudo based tools, we can modify:

[sourcecode lang="shell"]<br /># sudo find / -print &gt; /root/locations.txt &amp;<br />[1] 30851<br />[/sourcecode]



Wait until the background process complete (you can do something else if the task take quite long).
Then to find files with "tomcat-6.0" string in its filename:

 

[sourcecode lang="shell"]<br /># sudo grep "tomcat" /root/locations.txt<br />[/sourcecode]


If you want to locate certain file(s) once you can run this (as suggested by Mr Budi Rahardjo):

[sourcecode lang="shell"]<br /># find . -name &lt;pattern&gt; -print<br />[/sourcecode]


Replace <pattern> with the full name or partial name of the file. When wespecify the name of the file, we can use wildcards, but don't forget to quote the name when needed.

No comments:

Post a Comment