LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   trouble with find (https://www.linuxquestions.org/questions/linux-newbie-8/trouble-with-find-798092/)

scr-be 03-26-2010 12:33 PM

trouble with find
 
find . -atime +360 -exec ls -lah {} \;

that works fine, but i have a directory .snapshot that is a major waste of time to traverse through. i want find to exclude it. there is an option of -prune but i cannot seem to get it to work without saying
paths must precede expression

did some searching but cannot find example of -prune on the net

any ideas?

i was able to use ! -name .snapshot to avoid the directory, but this still does not address the issue of how to use -prune effectively.

rweaver 03-26-2010 04:41 PM

Prune is a odd function of sometimes annoying utility... try these to get an idea, be ultra careful using it with exec and -print as they DONT ACT THE SAME. Always test and test and test and test...

Code:

rweaver@core:~/test$ cat demo.sh
#!/bin/bash

echo "find . -print"
find . -print
echo

echo "find . -print \( -path ./.snapshot \) -prune"
find . -print \( -path ./.snapshot \) -prune
echo

echo "find . -print \( -path ./.snapshot \) -prune -exec echo \"exec on:{}\" \;"
find . -print \( -path ./.snapshot \) -prune -exec echo "exec on:{}" \;
echo

echo "find . -print \( -path ./.snapshot \) -prune -o -exec echo \"exec on:{}\" \;"
find . -print \( -path ./.snapshot \) -prune -o -exec echo "exec on:{}" \;
echo
rweaver@core:~/test$ ./demo.sh
find . -print
.
./file.png
./.snapshot
./.snapshot/demo2.fil
./.snapshot/demo.fil
./demo.sh
./file.1

find . -print \( -path ./.snapshot \) -prune
.
./file.png
./.snapshot
./demo.sh
./file.1

find . -print \( -path ./.snapshot \) -prune -exec echo "exec on:{}" \;
.
./file.png
./.snapshot
exec on:./.snapshot
./demo.sh
./file.1

find . -print \( -path ./.snapshot \) -prune -o -exec echo "exec on:{}" \;
.
exec on:.
./file.png
exec on:./file.png
./.snapshot
./demo.sh
exec on:./demo.sh
./file.1
exec on:./file.1

Those demos of it should give you a ~fairly~ good idea of what it expects and how it works... often times for simple exclusion its far easier to use ! whatever.

colucix 03-26-2010 05:04 PM

Quote:

Originally Posted by scr-be (Post 3913515)
did some searching but cannot find example of -prune on the net

any ideas?

Tried to search on LQ site? ;) Yep.

Samotnik 03-26-2010 05:08 PM

…-path ! '*.snapshot*'… ?

jschiwal 03-26-2010 05:34 PM

I believe the example in post #4 will still transverse the files in the .snapshot directory, even though the files in it will not be printed. Using prune, the directory will not be traversed, saving time if there are many files in the pruned directory. The find man page even says "To ignore a whole directory tree, use -prune..." in the paragraph describing -path.

hockeyman_102 03-26-2010 05:56 PM

you could make it more of a bash script

Code:

#!/usr/bin/bash

find . -atime +360 | grep -v "snapshot" > find.no_snapshot.tmp

while read file; do
ls -alh $file
done < find.no_snapshot.tmp

rm find.no_snapshot.tmp


scr-be 03-29-2010 11:11 AM

Quote:

Originally Posted by hockeyman_102 (Post 3913800)
you could make it more of a bash script

Code:

#!/usr/bin/bash

find . -atime +360 | grep -v "snapshot" > find.no_snapshot.tmp

while read file; do
ls -alh $file
done < find.no_snapshot.tmp

rm find.no_snapshot.tmp



If .snapshot was not so large, it would not be a big deal and this script would apply. As read this script would only parse out .snapshot/* after find has traversed it. In the environment I work with .snapshot contains at least 4 exact copies of the data set where it exists. They are time delayed local archives. So to traverse .snapshot would cause for at least 4 times the amount of files required for the desired results.

rweaver 03-30-2010 12:10 PM

Did my reply and examples tell you what you needed to know about how to use prune or do you need additional information?

scr-be 03-31-2010 01:15 PM

Quote:

Originally Posted by rweaver (Post 3918132)
Did my reply and examples tell you what you needed to know about how to use prune or do you need additional information?

Yes, it was quite informative. Was able to get the action I was looking for! Thanks!


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