LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General > LinuxAnswers Discussion
User Name
Password
LinuxAnswers Discussion This forum is to discuss articles posted to LinuxAnswers.

Notices

Reply
 
Search this Thread
Old 01-11-2006, 03:27 PM   #1
XavierP
Moderator
 
Registered: Nov 2002
Location: Kent, England
Distribution: Lubuntu
Posts: 19,176
Blog Entries: 4

Rep: Reputation: 430Reputation: 430Reputation: 430Reputation: 430Reputation: 430
DISCUSSION: Find command


This thread is to discuss the article titled: Find command


Quote:
Command your box from command-line-interface. This is the power of unix/linux & this is the power of find. -------------------------------------------------------------------------------- Find command is just like driving a car to the destination. If you can read & follow the instructions provided, you are destined to reach at proper address. Location director boardings at signals & landmarks are the inputs/options provided by the users/yourself & finally the roads are the hierarchy/paths to follow to reach the desired destination. For example : I had to reach rahul's home but i dont remember the exact address, but i had some clues like, it was on 94th street & it was a red colour building, further on there were two trees situated infront of his house. That was enough for me & indeed i reached his home.

Last edited by XavierP; 01-11-2006 at 03:30 PM.
 
Old 01-29-2006, 07:10 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,000
Blog Entries: 11

Rep: Reputation: 893Reputation: 893Reputation: 893Reputation: 893Reputation: 893Reputation: 893Reputation: 893
Quote:
1. Find file with file name 'foo'

find / -name foo
find / -name fo*
-name : Pattern could be a exact file name or wildcard * can be used

--------------------------------------------------------------------------------
2. Find file with file name 'FoO'

find / -iname foo
-iname : Same as -name variable but the match is case insensitive.

Same way -lname & -ilname variable works.
-lname : File is a symbolic link whose content matches the pattern specified.
-ilname : Same as -lname variable but the match is case insensitive.

--------------------------------------------------------------------------------
3. Finding all 'conf' files at / or at mentioned path.

find / -name *.conf


--------------------------------------------------------------------------------
4. Backing-up/copying all the conf files found in last example over to a seperate folder.

find / -name *.conf exec cp { } / \;
-exec : Executes command. The string { } replaced by the output of find command.
" \;" needs to be there as it tells the end of arguements provided to -exec variable.
A quick note on this use of -name ... be SURE that there's no files
that match the globbed pattern in the directory you invoke the command
from if you don't wrap the search-string in quotes.

E.g.. if you had the following directories in the local path
Code:
pg2xbase-2.3.2
pgaccess-0.98.8
pgadmin3-1.2.2
pgmanage-src
pgsql_session_handler.php
php
phpPgAdmin
php_sessions.sql
platform
pme-1.0.4
and you searched for
Code:
find . -name php*
you'd be getting the following (somewhat bewildering) error-message:
Code:
$ find -name php*
find: paths must precede expression
Usage: find [path...] [expression]
which won't happen if you do it like this:
Code:
$ find -name "php*"
In other words: the globbing of the wildcard by the shell will take
precedence :}



Cheers,
Tink
 
Old 08-06-2006, 09:17 AM   #3
XavierP
Moderator
 
Registered: Nov 2002
Location: Kent, England
Distribution: Lubuntu
Posts: 19,176
Blog Entries: 4

Original Poster
Rep: Reputation: 430Reputation: 430Reputation: 430Reputation: 430Reputation: 430
Please note, this Tutorial has now been updated. Points 22 onwards are new.
 
Old 09-11-2006, 03:43 PM   #4
sysconfig
Member
 
Registered: Sep 2006
Location: (.)
Posts: 44

Rep: Reputation: 15
Sometimes we need to find the file in server which we do not know where exactly it is located:

Search and list all files from current directory and down for the string ABC:


Quote:
find ./ -name "*" -exec grep -H ABC {} \;
find ./ -type f -print | xargs grep -H "ABC" /dev/null
egrep -r ABC *
Find all files of a given type from current directory on down:
Quote:
find ./ -name "*.conf" –print
Find all user files larger than 5Mb:

Quote:
find /home -size +5000000c –print
Find all files owned by a user (defined by user id number) on
the system: (could take a long time)


Quote:
find / -user 501 –print
Find all files created or updated in the last five minutes: (Great for finding effects of make install)

Quote:
find / -cmin -5
Find all world writable directories:
Quote:
find / -perm -0002 -type d –print

Find all world writable files:


Quote:
find / -perm -0002 -type f -print
find / -perm -2 ! -type l -ls
Find files with no user:

Quote:
find / -nouser -o -nogroup –print
Find files modified in the last two days:

Quote:
find / -mtime 2 -o -ctime 2
finding files in a directory that are older than 3 days and deleting them:
Quote:
find /directoryname -type f -mtime +3 -exec rm {} \;
 
Old 04-10-2008, 10:03 PM   #5
Asim Ahmed
LQ Newbie
 
Registered: Apr 2008
Posts: 2

Rep: Reputation: 0
Multiple commands in find

Hi Amit,

Could you help me out, if i want to run multiple commands in find command with -exec option, is there any way out to do this ?

Thanks in Advance
 
Old 04-13-2008, 04:55 PM   #6
beadyallen
Member
 
Registered: Mar 2008
Location: UK
Distribution: Fedora, Gentoo
Posts: 209

Rep: Reputation: 36
Quote:
if i want to run multiple commands in find command with -exec option, is there any way out to do this ?
Use -exec to execute the shell program, and just put your multiple commands into that.
See my post here for an example.
 
Old 09-26-2012, 08:48 AM   #7
H_TeXMeX_H
Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269
Well, as long as this thread has been resurrected, I will add:

Use single quotes to prevent globing:
Code:
BAD:
find / -name *.conf
GOOD:
find / -name '*.conf'
Using xargs increases performance and can prevent problems caused by spaces in file names:
Code:
SLOW:
find /directoryname -type f -mtime +3 -exec rm {} \; 
FAST:
find /directoryname -type f -mtime +3 -print0 | xargs -0 rm -f
 
  


Reply


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
Find/grep command to find matching files, print filename, then print matching content stefanlasiewski Programming 8 12-18-2013 06:36 PM
DISCUSSION: A Couple Quick find Tips beg84all LinuxAnswers Discussion 0 02-09-2005 04:47 PM
not find mt command imsajjadali Red Hat 2 02-07-2004 07:27 AM


All times are GMT -5. The time now is 09:59 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration