LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   LinuxAnswers Discussion (https://www.linuxquestions.org/questions/linuxanswers-discussion-27/)
-   -   DISCUSSION: Find command (https://www.linuxquestions.org/questions/linuxanswers-discussion-27/discussion-find-command-402154/)

XavierP 01-11-2006 02:27 PM

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.

Tinkster 01-29-2006 06:10 PM

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

XavierP 08-06-2006 08:17 AM

Please note, this Tutorial has now been updated. Points 22 onwards are new.

sysconfig 09-11-2006 02:43 PM

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 {} \;

Asim Ahmed 04-10-2008 09:03 PM

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

beadyallen 04-13-2008 03:55 PM

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.

H_TeXMeX_H 09-26-2012 07:48 AM

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



All times are GMT -5. The time now is 11:53 AM.