LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-04-2009, 12:55 AM   #1
elainelaw
Member
 
Registered: Jan 2008
Posts: 258

Rep: Reputation: 30
find statement


I would like to use find to search a file , the condition is to find the file which the name is "abc" OR "def" , then do ..

I tried the below , but not work , can advise how to do it ? thx

find . -name "abc" -name "def" -exec ...
 
Old 11-04-2009, 01:08 AM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,358

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
This is a comprehensive man page, with examples. You'll find the -o, -or options in there.
http://linux.die.net/man/1/find
 
Old 11-04-2009, 01:29 AM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

find . \( -name "abc" -or -name "xyz" \) -exec .... ......

The OR part needs to be grouped with ( and ) (escaped due to bash).

Hope this helps.
 
Old 11-04-2009, 01:42 AM   #4
elainelaw
Member
 
Registered: Jan 2008
Posts: 258

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by druuna View Post
Hi,

find . \( -name "abc" -or -name "xyz" \) -exec .... ......

The OR part needs to be grouped with ( and ) (escaped due to bash).

Hope this helps.

thx , it works.
 
Old 11-04-2009, 02:11 AM   #5
elainelaw
Member
 
Registered: Jan 2008
Posts: 258

Original Poster
Rep: Reputation: 30
I have one question ,

if the file is NOT in same path , i have the below but it is only find the current path

find . -name \( -name "abc" -or -name "xyz" \)

if this one , it is also not work .

find . -name \( -name "/tmp/abc" -or -name "/home/xyz" \)


can advise what can i do ? thx
 
Old 11-04-2009, 02:37 AM   #6
AngTheo789
Member
 
Registered: Sep 2009
Posts: 110

Rep: Reputation: 24
The dot after find (find .) indicates a search from within the current directory. Maybe that does limit the search even when using full pathnames.
 
Old 11-04-2009, 02:51 AM   #7
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

You can search from the root dir:

find / \( -name ...........

Like AngTheo789 said: The dot is to search from the dir you started the command, but you can also put a "hard" directory in its place (/home or / or /tmp). You cannot, to my knowledge use 2 different starting points in one find statement.

Hope this helps.
 
Old 11-04-2009, 03:17 AM   #8
elainelaw
Member
 
Registered: Jan 2008
Posts: 258

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by druuna View Post
Hi,

You can search from the root dir:

find / \( -name ...........

Like AngTheo789 said: The dot is to search from the dir you started the command, but you can also put a "hard" directory in its place (/home or / or /tmp). You cannot, to my knowledge use 2 different starting points in one find statement.

Hope this helps.
search from root directory , it is work , but it also consume many resource and need time to process the search , is there any method to find the file in full path ? thx
 
Old 11-04-2009, 03:28 AM   #9
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

That depends on where you suspect the files are that you are looking for.

For example if you know they are in /var and/or in /opt, you need to start from / (or 2 find statements, one from /var and one from /opt).

If you know that the files you are looking for are in one of the home directories you can start in /home (find /home -name ....). Zoom in as far as possible to reduce the time and resources needed by the find statement.

There is a possible alternative: locate. But you do need an up-to-date locate database for that (updatedb, as root to update that database).

Hope this helps.
 
Old 11-04-2009, 08:48 AM   #10
elainelaw
Member
 
Registered: Jan 2008
Posts: 258

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by druuna View Post
Hi,

That depends on where you suspect the files are that you are looking for.

For example if you know they are in /var and/or in /opt, you need to start from / (or 2 find statements, one from /var and one from /opt).

If you know that the files you are looking for are in one of the home directories you can start in /home (find /home -name ....). Zoom in as far as possible to reduce the time and resources needed by the find statement.

There is a possible alternative: locate. But you do need an up-to-date locate database for that (updatedb, as root to update that database).

Hope this helps.
thx reply,

Yes , I know the directory that the files are located , you suggest to use 2 find statement , is this the only method that I can use ? can I use single statement to do it ? thx
 
Old 11-04-2009, 08:56 AM   #11
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

No, to my knowledge you cannot.

But if you do know the directories where the files reside then find isn't really needed. You can ls the file and check if it is there:

Code:
ls /dir1/to/your/file1 1>/dev/null 2>&1
if [[ $? == "0"]]
then
  <<<< file is present >>>>
else
  <<<< file is not present >>>>
fi
You also need to do this twice, but the resources taken aren't even close to what find would.
 
Old 11-05-2009, 02:17 AM   #12
elainelaw
Member
 
Registered: Jan 2008
Posts: 258

Original Poster
Rep: Reputation: 30
thx reply ,

I still have one question


my script is as below

for FILE in `find /tmp -mtime +3 -name "abc" -exec ls {} \; `

do
echo $FILE
cp $FILE /tmp/backup/$FILE

done



it can not moved to /tmp/backup as the variable $FILE is /tmp/abc , so it can not copy to /tmp/backup//tmp/abc , can advise if I just want to copy the file to /tmp/backup , what can i do ? thx
 
Old 11-05-2009, 02:52 AM   #13
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

It is bad practice to use a backup-directory inside a directory you are backing up. Make sure your target and destination are not part of each other.

It looks like you are trying out things and use /tmp "just in case", you could use /var/tmp and /tmp, one as destination for the backup and one the target.

find /tmp -mtime +3 -name "abc" -exec cp {} /var/tmp \; (The {} in the find command is replaced by what is found.)

As you can see you do not need the for loop, you can use cp in the find command. If you want to see what find has found as well (the echo statement in your loop):

find /tmp -mtime +3 -name "abc" -print -exec cp {} /var/tmp \;

Hope this helps.
 
Old 11-05-2009, 03:08 AM   #14
elainelaw
Member
 
Registered: Jan 2008
Posts: 258

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by druuna View Post
Hi,

It is bad practice to use a backup-directory inside a directory you are backing up. Make sure your target and destination are not part of each other.

It looks like you are trying out things and use /tmp "just in case", you could use /var/tmp and /tmp, one as destination for the backup and one the target.

find /tmp -mtime +3 -name "abc" -exec cp {} /var/tmp \; (The {} in the find command is replaced by what is found.)

As you can see you do not need the for loop, you can use cp in the find command. If you want to see what find has found as well (the echo statement in your loop):

find /tmp -mtime +3 -name "abc" -print -exec cp {} /var/tmp \;

Hope this helps.
thx reply ,

your method works , but as I need to use the variable $FILE to do something , how can I keep the do , done statement in my case ? is it possible to do that ? thx
 
Old 11-05-2009, 03:56 AM   #15
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
You don't need a filename in the destination. You could use a directory target. "-exec cp '{}' /path/to/targetdir/ \;"

To remove the directory part of a pathname you could use ${file#*/}
You can also use the `basename' command to remove the directory part.

Also, find will recurse subdirectories by default unless you limit the depth of the search with the `-maxdepth' argument. Maxdepth needs to be after the directory and before other arguments like `-name'. You can combine -maxdepth and -mindepth to limit the depth within a range.

I would recommend scanning the info manual for the find command. It is a very flexible command. I only recently discovered the -delete and -ls find commands.
 
  


Reply



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
How to find the path statement. swamprat Linux - Newbie 3 11-09-2008 11:43 PM
Problem with if statement in a find -exec statement romsieze Programming 2 10-02-2008 12:38 AM
find statement tostay2003 Programming 4 12-11-2007 11:44 AM
Case statement with If statement cbo0485 Linux - Newbie 4 11-07-2007 08:05 PM
the FOR statement (C) sk84blood813 Programming 3 12-09-2004 08:25 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:10 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration