I'm using rsync to do a local backup of a Matlab model (end goal is a shell script to autobackup my key PhD files).
The model has layers of folders, and I want to exclude some wildcard matches on top level but not lower levels. Is this possible?
Example file structure:
/Model % DO NOT WANT to sync *.mat files in this directory
/Model/Data/Greens % WANT to sync *.mat files in this subdirectory
Example (simplified) code:
#!/bin/bash
rsync -a --exclude="*.mat" /Users/Me/MATLAB/Model /Volumes/KINGSTON/Backup
The example code excludes ALL *.mat files in ALL folder levels. How do I fix it to only apply the '*.mat' wildcard to the top folder?
TL;DR: Is there a way to apply rsync recursively to all subfolders, but apply a wildcard exclude to only the top-level folder?
By default, *.mat is going to match all .mat files in your folder and all sub-directories. If you'd only like to ignore .mat files in the parent directory, you can add the Model/ prefix to the exclude directive to only ignore .mat files in that folder
rsync -a --exclude="Model/*.mat" /Users/Me/MATLAB/Model /Volumes/KINGSTON/Backup
Alternately, if you want to include .mat files only in the Model/Data/Greens folder you can use the --include option in conjunction with the --exclude option
rsync -a --include="Model/Data/Greens/*.mat" --exclude="*.mat" /Users/Me/MATLAB/Model /Volumes/KINGSTON/Backup
Related
I have a list of files (a subset of the files in a directory) and I want to generate a patch that includes only the differences in those files.
From the diff manual, it looks like I can exclude (-x), but would need to specify that for every file that I don't want to include, which seems cumbersome and difficult to script cleanly.
Is there a way to just give diff a list of files? I've already isolated the files with the changes into a separate directory, and I also have a file with the list of filenames, so I can present it to diff in whichever way works best.
What I've tried:
cd filestodiff/
for i in `*`; do diff /fileswithchanges/$i /fileswithoutchanges/$i >> mypatch.diff; done
However patch doesn't see this as valid input because there's no filename header included.
patchutils provides filterdiff that can do this:
diff -ur old/ new/ | filterdiff -I filelist > patchfile
It is packaged for several linux distributions
Okay so I want to know how I would go about doing this, using grep to locate .txt files named "cocacola1", "cocacola2", "cocacola3" & then copying them to another directory. So searching for files named "cocacola" &/even if it contains other characters within the file name to then copy them to another directory/location.
You can just use unix find. Assuming the files you're searching for are in 'source' and you want to copy to 'destination':
find source -name '*cocacola*' -exec cp {} destination \;
I put the wildcard '*' before and after cocacola since you said other characters might exist in the file name.
Imagine the following structure:
/a/1.txt
/a/2.txt
/a/.keep
/a/b/1.txt
/a/b/2.txt
/a/b/3.txt
/a/b/.keep
/a/b/c/1.txt
/a/b/c/2.txt
/a/b/c/3.txt
/a/b/c/4.txt
/a/b/c/.keep
/d/test.txt
/d/work.txt
I want to ignore all files in a directory except .keep files to obtain the following results:
/a/.keep
/a/b/.keep
/a/b/c/.keep
/d/test.txt
/d/work.txt
My .gitignore file that doesn't work:
/a/*
!.keep
Unfortunatelly, you cannot reinclude files at directories ignored by previous rules, according to the gitignore Documentation:
It is not possible to re-include a file if a parent directory of that file is excluded. Git doesn’t list excluded directories for performance reasons, so any patterns on contained files have no effect, no matter where they are defined.
So this
/a/*
!/a/**/.keep
will only reinclude /a/.keep but not the others.
You'll have to exclude each file pattern under /a explictly.
/a/**/*.txt
/a/**/.ignore
/a/**/.alsoignore
UPDATE: Or a better solution is to create the following .gitgnore at your /a subdirectory:
*.*
!.keep
(the only drawback is that this solution will also keep files with no extension)
In your case, you should use:
/a/*
!**/.keep
From the gitignore documentation:
A leading "**" followed by a slash means match in all directories. For
example, "**/foo" matches file or directory "foo" anywhere, the same
as pattern "foo". "**/foo/bar" matches file or directory "bar"
anywhere that is directly under directory "foo".
Suppose I have a directory structure like
C:\Users\Desktop\abc\d
I want to rar archive the abc folder so that the structure of rar is:
abc\d
When I try using powershell to archive, winrar replicates the full path inside the archive, like:
\Users\Desktop\abc\d
I dont want the full path to be created inside the archive
Here's the script:
https://gist.github.com/saurabhwahile/50f1091fb29c2bb327b7
What am I doing wrong?
Use the command line:
Rar.exe a -r -ep1 Test.rar "C:\Users\Desktop\abc"
Rar.exe is the console version of WinRAR stored in same directory as WinRAR.exe. You can use this command line also with WinRAR.exe if you want to see the compression process in a graphic window.
a is the command and means add files to archive.
-r is a switch to recursively add all files and subdirectories including empty subdirectories to the archive.
-ep1 is another switch which results in execluding base directory.
For this command line the base directory is "C:\Users\Desktop\" and therefore the created archive Test.rar contains only abc and all files and subdirectories in directory abc which is what you want.
One more hint: Using the command line
Rar.exe a -r -ep1 Test.rar "C:\Users\Desktop\abc\"
results in adding all files and subdirectories of directory abc to the archive, but without directory name abc being also stored in the archive. The backslash at end makes this difference.
In other words: On using switch -ep1 everything up to last backslash in file/directory specification is not added to the archive.
For more information about available switches see the text file Rar.txt in the program files directory of WinRAR.
I have two folders A and B, which contain identical folder structure and files, but Folder B contains modified files. Both folders contain subfolders too.
I want to check which files are modified in folder B and copy it to a different folder C.
How can I achieve this using a cmd/shell script?
AFAIK rsync and unison can't handle your needs, since you want the changes to go to a third folder C.
This code is untested:
#python
import os
import shutil
a_dir=...
b_dir=...
c_dir=...
len_a_dir=len(a_dir)
for root, dirs, files in os.walk(a_dir):
dirs.sort()
for file in sorted(files):
a_file=os.path.join(root, file)
b_file='%s%s' % (b_dir, file[len_a_dir:])
if os.path.getmtime(a_file)!=os.path.getmtime(b_file):
c_file='%s%s' % (c_dir, file[len_a_dir:])
shutil.copyfile(b_file, c_file)
Try this:
rsync -r --compare-dest=/path/to/A /path/to/B/ /path/to/C