LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   find statement (https://www.linuxquestions.org/questions/linux-newbie-8/find-statement-766693/)

elainelaw 11-04-2009 12:55 AM

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 ...

chrism01 11-04-2009 01:08 AM

This is a comprehensive man page, with examples. You'll find the -o, -or options in there.
http://linux.die.net/man/1/find

druuna 11-04-2009 01:29 AM

Hi,

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

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

Hope this helps.

elainelaw 11-04-2009 01:42 AM

Quote:

Originally Posted by druuna (Post 3743738)
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.

elainelaw 11-04-2009 02:11 AM

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

AngTheo789 11-04-2009 02:37 AM

The dot after find (find .) indicates a search from within the current directory. Maybe that does limit the search even when using full pathnames.

druuna 11-04-2009 02:51 AM

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.

elainelaw 11-04-2009 03:17 AM

Quote:

Originally Posted by druuna (Post 3743830)
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

druuna 11-04-2009 03:28 AM

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.

elainelaw 11-04-2009 08:48 AM

Quote:

Originally Posted by druuna (Post 3743879)
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

druuna 11-04-2009 08:56 AM

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.

elainelaw 11-05-2009 02:17 AM

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

druuna 11-05-2009 02:52 AM

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.

elainelaw 11-05-2009 03:08 AM

Quote:

Originally Posted by druuna (Post 3745393)
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

jschiwal 11-05-2009 03:56 AM

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.


All times are GMT -5. The time now is 09:47 AM.