Linux "find" command

Write tutorials here
There are more tutorials here http://community.linuxmint.com/tutorial/welcome
Forum rules
Please don't add support questions to tutorials,start your own thread in the appropriate sub-forum instead. Before you post please read this
gdshrout
Level 1
Level 1
Posts: 5
Joined: Fri Apr 17, 2009 9:13 am

Linux "find" command

Postby gdshrout » Mon Apr 27, 2009 9:17 am

I'm familiar with Unix, and in the past I've done a find . -name 'xyz' to find files.
Now I'm using Linux Mint, and I need to find a file with 'xyz' in it (text inside a file) ...Any ideas on the best way to give that find command from terminal? I want to look in directories and subdirectories recursively. Thanks!

altair4
Level 18
Level 18
Posts: 8707
Joined: Tue Feb 03, 2009 10:27 am

Re: Linux "find" command

Postby altair4 » Mon Apr 27, 2009 2:45 pm

Same as in unix, grep

grep "angelina" phone_book.txt
Will look for angelina in phone_book.txt in the current directory and print to terminal

grep -R "angelina" /home/me/*
Will look for angelina in every file in any directory within the "me" home directory.

I've never done anything like this:
grep -R "angelina" * which would look through everything on your hard disk. I would think that would take some time.

You can also use it with "find" in compound expressions to limit the search.
Try man grep
Please add a [SOLVED] at the end of your original subject header if your question has been answered and solved.

gdshrout
Level 1
Level 1
Posts: 5
Joined: Fri Apr 17, 2009 9:13 am

Re: Linux "find" command

Postby gdshrout » Tue Apr 28, 2009 8:22 pm

Simple enough - apparently I'm rusty at unix.... ;) Thanks!

slider
Level 5
Level 5
Posts: 579
Joined: Wed May 07, 2008 2:08 pm
Location: Sliding around somewhere!

Re: Linux "find" command

Postby slider » Wed Apr 29, 2009 2:21 pm

You can also do a simple search in terminal:

Code: Select all

locate file_name
or a keyword which will provide output containing all instances where the word or name occurs. Output can be large at times but not always. Works really good when looking for multi-occurrences of files.

Some FILE browsers also support this search as well by typing locate:filename into the URL field. ie: locate:angelina.png or a similar query....

User avatar
Acid_1
Level 5
Level 5
Posts: 795
Joined: Thu Nov 01, 2007 11:12 pm
Location: Saskatchewan, Canada
Contact:

Re: Linux "find" command

Postby Acid_1 » Tue May 12, 2009 1:18 pm

Or what I do...

find |grep insert_name_here
Website: Forkwhilefork

Husse
Level 23
Level 23
Posts: 18703
Joined: Sun Feb 11, 2007 7:22 am
Location: Near Borås Sweden

Re: Linux "find" command

Postby Husse » Thu May 28, 2009 5:48 am

To get locate (which is my preferred search tool) to work well you must first do
sudo updatedb
because locate does not search the hard disk, it searches a database which is updated automatically when the computer is idle (that's why you sometimes notice activity when you don't do something)
Image
Don't fix it if it ain't broken, don't break it if you can't fix it

altair4
Level 18
Level 18
Posts: 8707
Joined: Tue Feb 03, 2009 10:27 am

Re: Linux "find" command

Postby altair4 » Thu May 28, 2009 9:26 am

EDIT: DO NOT UNDER ANY CIRCUMSTANCES DO THIS. It worked fine for me until I rebooted into a blank tesktop - no icons - just the taskbar. Nautilus would not launch from terminal and I could not right click the desktop. I repaired the desktop by deleting the nautilus action. I apologize to anyone who has tried this and found themselves in a similar predicament. I would have deleted this post I thought I would share the fix.

I've just discovered a utility called gnome-search-tool . It doesn't seem to be integrated into anything so I created a nautilus action:

Menu > Preferences > Nautilus Actions Configuration > Add

Label: Find
Icon: gtk-zoom-in
Path: /usr/bin/gnome-search-tool
Parameters: --hidden --allmounts --contains= --path=%M

Now I can go to nautilus, right click on any directory, select "Find" and locate a file based on name and / or content in that directory. I just started to use this and it seems to work OK :wink:

BTW: The parameters I used do the following:
--hidden allows searching in hidden files or directories
--allmounts allows searching in any filesystem that's mounted ( not enabled by default )
--contains= allows searching for content within a file
--path=%M forces the tool to open up where you right click. The default is always your home directory.
Please add a [SOLVED] at the end of your original subject header if your question has been answered and solved.

altair4
Level 18
Level 18
Posts: 8707
Joined: Tue Feb 03, 2009 10:27 am

Re: Linux "find" command

Postby altair4 » Thu May 28, 2009 5:27 pm

OK, In an attempt to redeem myself from this embarrassment I'm trying a different approach - nautilus scripts

Open your editor and enter the following:

Code: Select all

#!/bin/sh
cd $NAUTILUS_SCRIPT_SELECTED_FILE_PATHS
exec gnome-search-tool --hidden --allmounts --contains=


Save this file as Find in /home/your_userid/.gnome2/nautilus-scripts
Make it executable by selecting the file in nautilus, right click to properties , Permissions , allow executing

Now go into nautilus and right click a folder > Scripts > Find
You should see something like this:
Screenshot.png
Screenshot.png (38.55 KiB) Viewed 6161 times


Yes I did reboot just to make sure this wasn't a catastrophe.
The original idea came from here: https://help.ubuntu.com/community/NautilusScriptsHowto
Unfortunately it didn't work as I expected it to so I changed it to this.
Please add a [SOLVED] at the end of your original subject header if your question has been answered and solved.

Husse
Level 23
Level 23
Posts: 18703
Joined: Sun Feb 11, 2007 7:22 am
Location: Near Borås Sweden

Re: Linux "find" command

Postby Husse » Fri May 29, 2009 6:11 am

@ altair4
With the nautilus actions thing - did you create a new profile?
I played a little with some nautilus action and created a new profile with disastrous results
The same action in the existing "Main" profile worked well
Image
Don't fix it if it ain't broken, don't break it if you can't fix it

altair4
Level 18
Level 18
Posts: 8707
Joined: Tue Feb 03, 2009 10:27 am

Re: Linux "find" command

Postby altair4 » Fri May 29, 2009 6:19 am

As a matter of fact I did create a new profile. That's interesting, I will look into that further - on a test box :wink:
I appreciate your insight. For the moment I'm a little shy of using nautilus actions again especially since nautilus scripts works so well.
Please add a [SOLVED] at the end of your original subject header if your question has been answered and solved.

altair4
Level 18
Level 18
Posts: 8707
Joined: Tue Feb 03, 2009 10:27 am

Re: Linux "find" command

Postby altair4 » Sat May 30, 2009 7:06 pm

Just in case anyone wants to know I will continue to tweak this until someone tells me to shut the heck up :)

I received a message that my nautilus script does not work with directories that have spaces in them ( it defaults to a higher level directory ). Here is my fix:

Code: Select all

#!/bin/sh
cd "$(echo $NAUTILUS_SCRIPT_SELECTED_FILE_PATHS)"
exec gnome-search-tool --hidden --allmounts --contains=

Save this file as Find in /home/your_userid/.gnome2/nautilus-scripts
Make it executable by selecting the Find file in nautilus, right click to properties , Permissions , allow executing

Now go into nautilus and right click a folder > Scripts > Find
Now you should be able to search in directories like this:
Screenshot.png
Screenshot.png (34.72 KiB) Viewed 6116 times

For some reason this script does not work in Mint8 only in Mint7. For Mint8 you need to change it to the following:

Code: Select all

#!/bin/sh
exec gnome-search-tool --hidden --path="$(echo NAUTILUS_SCRIPT_SELECTED_FILE_PATHS)" --contains=
Last edited by altair4 on Wed Dec 09, 2009 8:43 am, edited 2 times in total.
Please add a [SOLVED] at the end of your original subject header if your question has been answered and solved.

emorrp1
Level 8
Level 8
Posts: 2158
Joined: Thu Feb 26, 2009 8:58 pm

Re: Linux "find" command

Postby emorrp1 » Sat May 30, 2009 7:13 pm

for simple searches, ctrl-F will work fine, though it doesn't work recursively by default (perhaps there's some preferences somewhere).
If you have a question that has been answered and solved, then please edit your original post and put a [SOLVED] at the end of your subject header
Hint - use a google search including the search term site:forums.linuxmint.com

altair4
Level 18
Level 18
Posts: 8707
Joined: Tue Feb 03, 2009 10:27 am

Re: Linux "find" command

Postby altair4 » Sat May 30, 2009 7:17 pm

This can search for a particular file name and for content within a file or both. Sort of like locate + grep. Plus from a right - click :wink:
EDIT: Actually it's more like what you can do in the Windows Explorer file manager. It will produce a listing of files that have extensions of *.txt , for example, containing the word angelina ( just as another example ).
Please add a [SOLVED] at the end of your original subject header if your question has been answered and solved.


Return to “Tutorials”

Who is online

Users browsing this forum: No registered users and 4 guests