LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Using Find with an exclude/exclude file (https://www.linuxquestions.org/questions/linux-general-1/using-find-with-an-exclude-exclude-file-909348/)

metallica1973 10-21-2011 09:16 AM

Using Find with an exclude/exclude file
 
I am familiar with using tar and exclude/include files:

Code:

tar zcf backup.dirs.tgz --files-from=include.mydirs --exclude-from=exclude.mydirs --no-recursion
but was wondering if I could use find in the same way. I know that you can just specify the directories to exclude but my list is large. Tar is specific which I like:

Code:


find . -depth -ipath './.*' -prune -o -ipath './Downloads*' -prune -o -print

I have to backup specific files from several location and exclude the residual garbage. In my script I have it structure to use find and cpio together and wanted to minimally modify it. So what I want is:

Code:

find (read from include file) (read from exclude file) | cpio -oavc > somedir
I am trying to avoid using several find statements for querying specific directories. Thanks

Code:

find /home/testuser/ \( ! -iname "testdir1*" ! -iname "testdir2*"\)
find /usr/var/ \( ! -iname "testdir3*" ! -iname "testdir4*"\)
find /usr/lib/ \( ! -iname "libssl*" ! -iname "libperl*"\)

or
find /home/testuser/ -depth -ipath './testdir1*' -o -prune -ipath './testdir2*' -o -prune print


ShadowCat8 10-21-2011 03:59 PM

As a thought, have you looked at using rsync?

HTH. Let us know.

metallica1973 10-22-2011 07:28 AM

Many thanks for the reply

It looks promising, do you know if the compression on the archive is as good or better that using tar or cpio? I am concerned about spacing issues.

Regards

metallica1973 10-23-2011 09:10 AM

it can be done using grep:

Code:

find ... | grep -v -f excludefile | grep -f includefile | cpio ...
regards

metallica1973 10-24-2011 09:11 AM

Just an update:

Code:

find ... | grep -f includefile | cpio -oavc > /some/destination
appears to be all that is needed. Using grep in this way, there wasn't a need for an exclude file. It will only use what is in the include-file.

metallica1973 10-27-2011 04:12 PM

This worked better

Code:

find / -depth -ipath '/home/testuser/.*' -prune -o -print| grep -f include.mydirs | grep -v -f exclude.mydirs
but it doesnt work if I want to see the differences within 24 hours

Code:


find / -depth -mtime 0 -ipath '/home/testuser/.*' -prune -o -print| grep -f include.mydirs | grep -v -f exclude.mydirs

??

metallica1973 10-27-2011 10:36 PM

it did work. I made an adjustment in my exclude.mydir. i was blocking my test directory.

jschiwal 10-28-2011 12:38 AM

By the way, you can use several directories in find. Including only the directories you want to backup, you may not need a long exclude list

find /home/mydir /var/ ...

metallica1973 11-06-2011 09:39 PM

Just wanted to add a note so that other users may have a better understanding of my headaches:

Code:

sudo find / -depth ! -path '*/\.*' -iname '*' -type f | grep -f include_home | grep -v -f exclude_home
excludes "all" hidden directories

Code:

! -path '*/\.*'
my include_home file(files and directories to include) which is all that was needed (no grep -v -f exclude_home) if you are not worried about "find" decending into other directories searching for the same file and directories

ex. just a snippett of the results

Code:

sudo find / -depth ! -path '*/\.*' -iname '*' -type f | grep -f include_home
/etc/network/interfaces
/etc/network/if-up.d/avahi-daemon
/xspace/shit/music/transfer/0a92e46f-a8c1-4e0e-bcfe-c3cd7797a682/etc/network/if-down.d/avahi-autoipd.dpkg-new
/xspace/shit/music/transfer/0a92e46f-a8c1-4e0e-bcfe-c3cd7797a682/etc/network/interfaces

my include_home file(a snippett):

Code:

home/testuser/transfer/
home/testuser/Videos/
home/testuser/VirtualBox VMs/
/etc/lsb-release
/etc/network/

and my exclude_home file

Code:

#home/
usr/
lib/
bin/
etc/
xspace/
boot/
docs/
sbin/
tmp/
sys/
var/
opt/
media/
dev/
proc/

As you can see from above find will ascend down into other directories but if grep -v -f exclude_home files is used, it will not allow that to happen thus filtering out replica files in other-directories except if there are duplicates withing the same directories. Grep are simply the filters:

Code:

sudo find / -depth ! -path '*/\.*' -iname '*' -type f | grep -f include_home | grep -v -f exclude_home
/etc/network/interfaces
/etc/network/if-up.d/avahi-daemon



All times are GMT -5. The time now is 03:37 PM.