I'm writing a shell script that syncs files and I want to give users the ability to exclude certain files from syncing by creating a .syncignore file similar to Git's .gitignore file. According to the gitignore documentation, and my own experiments, these exclusion rules are more complicated than a simple glob match. Some examples:
If you have foo in your .gitignore file, it will exclude foo appearing anywhere in the path (e.g. ./foo, ./bar/foo, and ./bar/foo/baz would be excluded) but not partial matches of foo (e.g. ./foobar, ./bar/foobar/baz would NOT be excluded).
If you include a slash, then the rule is applied relative to the current directory. For example, if you have /foo in your .gitignore file, it will exclude ./foo but not ./bar/foo.
You can include wildcards. For example, foo* will exclude ./foo, ./foobar, and ./bar/foobar/baz.
Is there any easy way to replicate the exclusion rules for .gitignore in a shell script on OS X?
Use rsync to synchronize the files. Use its existing include/exclude pattern support. Put the rules in .rsync-filter and pass the -F flag to make it read the patterns from that file.
rsync man page
Just use git. Make sure you have git 2.3.0 or later on both sides, and use push-to-deploy.
Related
I have a lot of markdown files in various directories each with the same format (# title, then ## sub-title).
can I make the --toc respect the folder layout, in that the folder itself is the name of chapter, and each markdown file is content of this chapter.
so far pandoc totally ignores my folder names, it works the same as putting all the markdown files within the same folder.
My approach to this is to create index files in each folder with first level heading and downgrade headings in other files by one level.
I use Git and by default I'm using default structure, having first level headings in files, but when I want to generate ebook using pandoc I'm modifying files via automated Linux shell script. After that, I revert changed files via Git.
Here's the script:
find ./docs/*/ -name "*.md" ! -name "*index.md" -exec perl -pi -e "s/^(#)+\s/#$&/g" {} \;
./docs/*/ means I'm looking only for files inside subfolders of docs directory like docs/foo/file1.md, docs/bar/file2.md.
I'm also interested only in *.md files, excluding *index.md files.
In index.md files (that I name usually 00-index.md to make them appear as first), I put a first level heading # and because those files are excluded from find portion of the script, their headings aren't downgraded.
Next, there's a perl's search and replace command with regular expression s/^(#)+\s/#$&/g that looks for all lines starting from one or more # and adds another # to them.
In the end, I'm running pandoc with --toc-depth=2 so the table of content contains only first and second level headings.
pandoc ./docs/**/*.md --verbose --fail-if-warnings --toc-depth=2 --table-of-contents -o ./ebook.epub
To revert all changes made to files, I restore changes in the Git repo.
git restore .
Is there a way to exclude a file? I would like to exclude all *.config files. Everything else should be included.
Or if I could say include: *.aspx, *.ascx, *.xml, *.png, *.gif, *.html that would be fine.
To quote what Tod said in this forum post:
I'm not sure the component we use to FTP supports negated wildcards,
but you can simply add a Delete Files action before this that operates
on *.config
Alternatively (if you don't want to delete because you may re-use the
files), you can use the Synchronize/Transfer Files action to a
temporary directory (e.g. ~\Ftp) and use a !*.config mask on that to
not transfer the configs, then use the FTP action from ~\Ftp as the
source directory.
In shell, what is a good way to duplicating files in an existing directory so that the result gives the same file but with a different extension? So taking something like:
path/view/blah.html.erb
And adding:
path/view/blah.mobile.erb
So that in the path/view directory, there would be:
path/view/blah.html.erb
path/view/blah.mobile.erb
I'd ideally like to perform this at a directory level and not create the file if it already has both extensions but that isn't necessary.
You can do:
cd /path/view/
for f in *.html.erb; do
cp "$f" "${f/.html./.mobile.}"
done
PS: This replaces first instance of .html. with .mobile., syntax is bash specific (let me know if you're not using BASH).
I am reading rsync docs, INCLUDE/EXCLUDE PATTERN RULES section. Following the rules explained there I would like to exclude the following folders and files:
all .metadata folders
all *.DS_Store* files
So, I am creating rules like:
- .DS_Store
.metadata/
But files and folders are not excluded. What am I doing incorrectly?
The following will skip everything in .DS_Store directories plus the .DS_Store directories themselves and works with rsync distributed with Mavericks: rsync --exclude='.DS_Store' --exclude='.metadata' <your_source_dir> <your_destination_dir>.
The --exclude=<pattern> is actually just a shorthard for --filter='- <pattern>'. This means --exclude='.DS_Store' and --filter='- .DS_Store' are equivalent. The same goes with --include=<pattern> which is actually just a shorthand for --filter='+ <pattern>'.
Using Ruby (1.9.3) I need to replace a single file in a zip archive.
The situation is as follows. I have ~1000 zip archives that need to be updated, specifically one file in each of them needs to be replaced. The archives are all of the same structure. Is there a quick and dirty way for Ruby, or a library/gem for Ruby, to simply say "replace the file in this zip archive with this file on the filesystem"?
I'll work on a solution of my own in the meantime.
You can use the zip command, called from the ruby, which probably will be the best solution. From the zip manpage zip manpage
-d
--delete
Remove (delete) entries from a zip archive. For example:
zip -d foo foo/tom/junk foo/harry/\* \*.o
will remove the entry foo/tom/junk, all of the files that start with foo/harry/, and all of the files that end with .o (in any path). Note that shell path‐
name expansion has been inhibited with backslashes, so that zip can see the asterisks, enabling zip to match on the contents of the zip archive instead of the
contents of the current directory. (The backslashes are not used on MSDOS-based platforms.) Can also use quotes to escape the asterisks as in
zip -d foo foo/tom/junk "foo/harry/*" "*.o"
Not escaping the asterisks on a system where the shell expands wildcards could result in the asterisks being converted to a list of files in the current
directory and that list used to delete entries from the archive.
Under MSDOS, -d is case sensitive when it matches names in the zip archive. This requires that file names be entered in upper case if they were zipped by
PKZIP on an MSDOS system. (We considered making this case insensitive on systems where paths were case insensitive, but it is possible the archive came from
a system where case does matter and the archive could include both Bar and bar as separate files in the archive.) But see the new option -ic to ignore case
in the archive.
If you want a pure ruby solution take a look at ZipFileSystem
Zip::ZipFile looks promising. It appears to have a way to delete and add files to a zip archive.