Comparing two folders based on last modified date - date

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

Related

How do I diff only certain files?

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

MATLAB is using wrong dependency (files with the same name in different directories)

I'm trying to adapt a MATLAB script. I have the original code in a folder and the new version in another:
dev\original
dev\new
The new folder has all the original files and a few more. But one of the functions dependencies in one of the files in the new folder is calling the file in the original folder by mistake.
File:
dev\new\example.m
Dependency Report:
current dir : file_1
current dir : file_2
other : file_3
Even though there are "file_1", "file_2" and "file_3" in both folders.
dev\original\file_1.m
dev\original\file_2.m
dev\original\file_3.m
dev\new\file_1.m
dev\new\file_2.m
dev\new\file_3.m
Is there a way to force MATLAB to call the correct file? Or maybe only allow it to call files from the current and sub directories?
Edit:
To ilustrate more information, I ran the "pwd" and "which -all" commands to show that MATLAB knows the existence of both files and is running in the "new" folder.
>> pwd
ans =
'C:\dev\new'
>> which -all file_3
Not on MATLAB path % model constructor
C:\dev\new\file_3.m % Shadowed
You should look into using private folders where you control the scope of what your doing,
basically you put file_1, file_2 and file_3 in a private folder:
dev/new/example.m
dev/new/private/file_1.m
dev/new/private/file_2.m
dev/new/private/file_3.m
Then your example will call the file_*.m in the appropriate private folder.

rsync: How to NOT apply wildcard excludes recursively

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

copy multiples files from multiples directories into one directory using MATLAB

How can I manage to copy the files from multiple directories into one target directory IN MATLAB for example if the directories are organized as follow:
directory1
sub-directory1
sub-sub-directory1-1
file1
file2
sub-sub-directory1-2
file4
thisis-my-file
sub-directory2
sub-sub-directory2-1
file
myfile
sub-sub-directory2-2
file-case1
the result should be something like this:
target-directory
file1
file2
file4
thisis-my-file
file
myfile
file-case1
here is one of many possible solutions:
First, get all subfolders of your directory1:
% define the destination folder
destinationFolder = 'c:\temp';
% genpath delivers all subfolders of the given directory
directories = genpath('C:\directory1');
% the following regular expression gets all these subfolder, seperated by ';'
directories = regexp([directories ';'],'(.*?);','tokens');
Now you can use the dir-function inside a for-loop for getting all the files in the subfolders:
for i=1:length(directories)
% you could use a wildcard, if you only want some
% specific files to be moved in to the target directory.
% (filesep is a built-in function!)
files = dir([directories{i}{1} filesep '*.*']);
% use a second loop for copying your files
for j=1:length(files)
% build the path and copy the file to the desired destination
copyfile([directories{i}{1} filesep files(j).name)], destinationFolder);
end;
end;
you can use a matlab command for copying files which is copyfile('source','destination') like
copyfile('directory1/sub-directory1/sub-sub-directory1-1/file1.txt','target-directory')
copyfile('directory1/sub-directory1/sub-sub-directory1-1/file2.txt','target-directory')
copyfile('directory1/sub-directory1/sub-sub-directory1-2/file4.txt','target-directory')
copyfile('directory1/sub-directory1/sub-sub-directory1-2/thisis-my-file.txt','target-directory')
copyfile('directory1/sub-directory2/sub-sub-directory2-1/file.txt','target-directory')
like this way for all of your file and you get those file in your destination folder. you can also give a complete path of the each source and destination also.

7zip / winrar command to extract a folder with path intact to specific folder but excluding parent source path

example
There is a file "sample.rar".
Folder structure is: "rising\dawn\ and here there are many (folders1, folders2 and file1, file2)" in this archive.
i have used following command
7z.exe x "sample.rar" "rising\dawn\*" -oi:\delete
The result is:
all files and folders in "rising\dawn\" are extracted to "i:\delete" folder but the empty parent folders "rising\dawn\" are also created in destination folder.
e.g. destination looks:
i:\delete\rising\dawn\folder1\file1.bmp
i:\delete\rising\dawn\folder2\subfolder
i:\delete\rising\dawn\file1.txt
i:\delete\rising\dawn\file2.txt
i don't want "rising\dawn\" empty folders to be created but the folder structure there onwards must be as is in the archive.
i want the result:
i:\delete\folder1\file1.bmp
i:\delete\folder2\subfolder
i:\delete\file1.txt
i:\delete\file2.txt
at last i found a way out solution. thanks to the winrar support. i have accepted it as an answer below.
if you find the question useful don't forget to click the up-vote button.
Finally this gave me the result.
Thanks to winrar support.
rar x -ep1 sample.rar rising\dawn\* d:\e\delete\
i have tried other answers given here, this is the only correct answer.
don't forget to upvote.
You can extract the archive normally and
1) move the lower level folder/files to where you would like it, then
2) remove the extra top level archive folders.
Code to do so will depend on the exact task.
Using e command instead of x and add -r option works well.
Like this:
7z.exe e -r "sample.rar" "rising\dawn\*" -oi:\delete
My executable version is "7-Zip [64] 9.20 2010-11-18",
And the platform is Windows 8.1.
This command line eliminates unnecessary parent folders and preserves the hierarchy of folders.
You need to use the e command rather than the x command:
7z.exe e "sample.rar" "scholar\update\*" -oi:\delete
Using e instead of x means 7zip will extract all matching files into the same folder (as specified via the -so switch, or the current directory if this isn't specified) rather than preserving the folder structure from inside the archive.