LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-21-2020, 07:01 PM   #1
JavaMeister
LQ Newbie
 
Registered: Jul 2017
Location: NC
Distribution: Ubuntu 18.04
Posts: 16

Rep: Reputation: Disabled
rsync - exclude a directory but include specific file(s) within that directory


I want to exclude a directory but/and include a specific file within that directory. Said another way, I want to exclude all the files in a specific directory but/and include (a) specific file(s) in that same directory. I would prefer to do this using exclude-from=exclude.txt and include-from=include.txt. Is this possible? What is the format of the command?
 
Old 09-21-2020, 08:11 PM   #2
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 4,504

Rep: Reputation: 2062Reputation: 2062Reputation: 2062Reputation: 2062Reputation: 2062Reputation: 2062Reputation: 2062Reputation: 2062Reputation: 2062Reputation: 2062Reputation: 2062
Yes. You can use the "--filter merge /path/to/your_filter_file" option and put your list of exclude and include patterns in that file. Each line in the pattern file must begin with a prefix that indicates its function. See the section for "--filter=" and the "FILTER RULES" section in the rsync manpage.

Here's a trivial example:
Code:
rsync -av --filter=$HOME/filter_rules Pictures/ /media/backup/Pictures
where there is a subdirectory "Pictures/private_pics", and the filter_rules file contains
Code:
include private_pics/Janes-face.jpg
exclude private_pics/*
Note that "-" is a synonym for "exclude" and "+" is a synonym for "include" in that filter file.

As with "--exclude" and "--include" options, the rules are processed in order, and an earlier rule overrides a later one. That is useful, for example, for including specific files that would also match a later exclude pattern.

Last edited by rknichols; 09-21-2020 at 08:14 PM.
 
2 members found this post helpful.
Old 09-23-2020, 11:31 AM   #3
JavaMeister
LQ Newbie
 
Registered: Jul 2017
Location: NC
Distribution: Ubuntu 18.04
Posts: 16

Original Poster
Rep: Reputation: Disabled
rsync - exclude a directory but include specific file(s) within that directory

Thank you RKNichols. That looks like exactly what I want. I have studied the referenced man pages thoroughly.

It is not working for me, though. I have no idea what I am missing. Here is the story.

My filter rules file, filter.txt:
Code:
include src/below/inBelow.txt
exclude src/below/
My rsync command shell, do:
Code:
rsync -avxu --filter=filter.txt src bkup
The directory structure(I added the colors):
Code:
ron@ron-OptiPlex-990:~/Tools/play$ ll -R
.:
total 16
drwxr-xr-x 2 ron ron 4096 Sep 23 10:49 bkup/
-rwxr-xr-x 1 ron ron   41 Sep 23 10:25 do*
-rw-r--r-- 1 ron ron   49 Sep 23 10:26 filter.txt
drwxr-xr-x 3 ron ron 4096 Sep 23 09:23 src/

./bkup:
total 0

./src:
total 8
drwxr-xr-x 2 ron ron 4096 Sep 23 09:23 below/
-rw-r--r-- 1 ron ron   15 Sep 23 09:23 inTools.txt

./src/below:
total 8
-rw-r--r-- 1 ron ron 15 Sep 23 09:22 inBelow.txt
-rw-r--r-- 1 ron ron 19 Sep 23 09:23 notThis.txt

ron@ron-OptiPlex-990:~/Tools/play$
Here are the results I get for my trouble:
Code:
ron@ron-OptiPlex-990:~/Tools/play$ ./do
Unknown filter rule: `filter.txt'
rsync error: syntax or usage error (code 1) at exclude.c(902) [client=3.1.2]
ron@ron-OptiPlex-990:~/Tools/play$
The goal is to backup src/inTools.txt, and src/below/inBelow.txt, but exclude everything else in ./src/below, specifically src/below/notThis.txt.
I am at a complete loss. Please tell me I have made some simple error.
 
Old 09-23-2020, 11:36 AM   #4
SoftSprocket
Member
 
Registered: Nov 2014
Posts: 399

Rep: Reputation: Disabled
Have you tried providing the path to filter.txt? i.e. ./filter.txt or ~/Tools/play/filter.txt
 
Old 09-23-2020, 11:49 AM   #5
JavaMeister
LQ Newbie
 
Registered: Jul 2017
Location: NC
Distribution: Ubuntu 18.04
Posts: 16

Original Poster
Rep: Reputation: Disabled
Hot damn! Got it!
Two errors:
change
Code:
--filter=filter.txt
to
Code:
--filter 'merge filter.txt'
and add an * to
Code:
exclude src/below/*
All works! I'm excited.
 
Old 09-23-2020, 01:30 PM   #6
JavaMeister
LQ Newbie
 
Registered: Jul 2017
Location: NC
Distribution: Ubuntu 18.04
Posts: 16

Original Poster
Rep: Reputation: Disabled
Good suggestion, SoftSprocket. Yes, I did try it, with no success. But you will see from the post below yours, that was not the problem. Thanks.
 
Old 09-23-2020, 01:49 PM   #7
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 4,504

Rep: Reputation: 2062Reputation: 2062Reputation: 2062Reputation: 2062Reputation: 2062Reputation: 2062Reputation: 2062Reputation: 2062Reputation: 2062Reputation: 2062Reputation: 2062
Glad to hear that you got there! The complexity of rsync can be quite a challenge if you need something beyond the basics.
 
  


Reply

Tags
exclude, include files, rsync


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] rsync issues with --include-from=FILE and --exclude-from=FILE jgould Linux - Server 8 04-04-2018 01:44 PM
rsync Include/exclude based on pattern & file size? Geremia Linux - Software 1 01-20-2012 09:49 PM
Rsync: exclude and include filters? JosephS Linux - Software 1 02-03-2011 11:43 PM
Rsync exclude (or include) issue eyeprotocol Linux - General 1 02-10-2010 12:08 AM
rsync include/exclude problems toaster.waffle Linux - Software 4 11-19-2009 10:57 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:12 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