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 ... |
This is a comprehensive man page, with examples. You'll find the -o, -or options in there.
http://linux.die.net/man/1/find |
Hi,
find . \( -name "abc" -or -name "xyz" \) -exec .... ...... The OR part needs to be grouped with ( and ) (escaped due to bash). Hope this helps. |
Quote:
thx , it works. |
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 |
The dot after find (find .) indicates a search from within the current directory. Maybe that does limit the search even when using full pathnames.
|
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. |
Quote:
|
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. |
Quote:
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 |
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 |
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 |
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. |
Quote:
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 |
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. |