rsync include only directory pattern - rsync

I want to include only directories named *cache*, and all files and subdirectories under them.
How to write rync --include --exclude?
source dest
├── a │
├── b ├── b
│   └── d │   └── d
│   └── e │   └── e
│   └── cache │   └── cache
├── c ├── c
│   └── f │   └── f
│   └── npm_cache │   └── npm_cache
├── g ├── g
│   └── cache_stores │   └── cache_stores
├── h ├── h
│   └── cache │   └── cache
│   └── i │   └── i
│   └── j │   └── j
└── k │
└── l │

This should work:
--include='*/'
--include='*cache*/**'
--exclude='*'
--prune-empty-dirs
That says:
Include all folders (this is necessary to search inside them).
Include all files with "cache" in the name of a parent directory.
Exclude everything else.
Prune away any folders that were copied but turned out to contain no caches. Unfortunately, this also removes any empty folders within cache directories, but hopefully that's not important to you.

I have accepted ams's answer, but if you don't know rsync --include --exclude syntax (I don't), get an explicit file list with find first.
cd source
find . | grep /.*cache.*/ | rsync --files-from=- source dest

Related

How to copy a file to every directory with Grunt?

So I'm building a WP plugin and it's customary to put empty index.html files into every folder to prevent directory listing where the host allows it. I'm building the deployment-ready package with grunt, but the only thing I'm missing are these files. I have many folders and would rather not create these files by hand. I'm happy to create one, and make Grunt copy that file to every path. But how?
No additional grunt plug-ins are necessary. Your requirement can be achieved using Grunt's built-in features.
Consider adding a custom Task to your Gruntfile.js as per the one named createEmptyHtmlFiles shown below.
Gruntfile.js
module.exports = function(grunt) {
grunt.initConfig({
// ...
});
/**
* Custom task to create empty `index.html` file in all folders.
*/
grunt.registerTask('createEmptyHtmlFiles', function() {
var fileName = 'index.html',
contents = '';
grunt.file.expand({ filter: 'isDirectory' }, 'dist/**/*')
.forEach(function(dirPath) {
var htmlFilePath = dirPath + '/' + fileName;
grunt.file.write(htmlFilePath, contents, { encoding: 'utf8'})
});
});
grunt.registerTask('default', ['createEmptyHtmlFiles']);
};
Explanation:
Typically your Gruntfile.js will include grunt.initConfig({ ... }); section that defines the configuration of various Tasks that you want to perform. This part should remain as per your current configuration.
A custom Task named createEmptyHtmlFiles is registered that does the following:
Assigns the desired filename, i.e. index.html, to the fileName variable and also assigns an empty string to the contents variable.
Next we utilize grunt.file.expand to which we pass a globbing pattern. In the example above the glob provided is 'dist/**/*'. The globbing pattern combined with the filter: 'isDirectory' option essentially obtains the pathnames to all folders inside the dist directory.
Important: This glob pattern you will need to change as per your directory structure.
Next we iterate each directory pathname using the Array's forEach method.
In each turn of the forEach loop we assign to the htmlFilePath variable a new pathname for where the resultant index.html file is to be created.
Each index.html file is created using grunt.file.write.
Demo:
Lets say the project directory is structured as follows:
.
├── Gruntfile.js
├── dist
│   ├── a
│   │   ├── b
│   │   │   └── 1.txt
│   │   └── c
│   │   └── 2.txt
│   ├── d
│   │   ├── 3.txt
│   │   └── e
│   │   └── 4.txt
│   └── f
│   └── g
│   └── 5.txt
├── node_modules
│ └── ...
└── package.json
Given the Gruntfile.js above after running $ grunt it will change to the following:
.
├── Gruntfile.js
├── dist
│   ├── a
│   │   ├── b
│   │   │   ├── 1.txt
│   │   │   └── index.html <-----
│   │   ├── c
│   │   │   ├── 2.txt
│   │   │   └── index.html <-----
│   │   └── index.html <-----
│   ├── d
│   │   ├── 3.txt
│   │   ├── e
│   │   │   ├── 4.txt
│   │   │   └── index.html <-----
│   │   └── index.html <-----
│   └── f
│   ├── g
│   │   ├── 5.txt
│   │   └── index.html <-----
│   └── index.html <-----
├── node_modules
│ └── ...
└── package.json
Note Every folder inside the dist directory now includes an empty index.html file.
You may need to exclude the index.html from being created in specific directories. In which case we can you can negate specific directories via the glob pattern(s) passed to the grunt.file.expand method.
For instance, lets say we configure it as follows in the createEmptyHtmlFiles task:
...
grunt.file.expand({ filter: 'isDirectory' }, ['dist/**/*', '!dist/a/{b,c}'])
...
Note: This time we pass an Array that contains two glob patterns. The first one is the same as per the previous example, however the second one begins with ! which will negate a match.
Running $ grunt using the the aforementioned glob patterns will result in the following directory structure:
.
├── Gruntfile.js
├── dist
│ ├── a
│ │ ├── b
│ │ │ └── 1.txt
│ │ ├── c
│ │ │ └── 2.txt
│ │ └── index.html <-----
│ ├── d
│ │ ├── 3.txt
│ │ ├── e
│ │ │ ├── 4.txt
│ │ │ └── index.html <-----
│ │ └── index.html <-----
│ └── f
│ ├── g
│ │ ├── 5.txt
│ │ └── index.html <-----
│ └── index.html <-----
├── node_modules
│ └── ...
└── package.json
Note Every folder inside the dist directory, excluding folders b and c, now include an empty index.html file.
btw. When you say "empty index.html files", I've taken that literally. However if you did need some html markup in each file you can assign that to the contents variable. For example:
contents = '<!DOCTYPE html>\n<html>\n<head></head>\n<body></body>\n</html>';
But I said "copy a file ..."
In which case you can change the custom Task to the following:
/**
* Custom task to copy a source `index.html` file in all folders.
*/
grunt.registerTask('copyFileToFolders', function() {
var srcFilePath = './path/to/file/to/copy/index.html';
grunt.file.expand({ filter: 'isDirectory' }, 'dist/**/*')
.forEach(function(dirPath) {
grunt.file.copy(srcFilePath, dirPath + '/index.html')
});
});
Notes:
This utilizes grunt.file.copy to copy the source file to all folders.
The pathname assigned to the srcFilePath variable should be substituted with a real pathname to the actual master index.html file that you want to copy to all folders.
As per the first example, the glob pattern passed to grunt.file.expand must be change as necessary.

How to move files found with pattern and move to another subdirectory in unix

I have this:
.
├── dirA
│   └── ProdA
│   ├── Brief
│   │   └── Form.xlsx
│   ├── Results
│   └── Studies
└── dirB
└── BrandB
└── ProdB
├── Brief
│   └── Form.xlsx
└── Results
and i want this:
.
├── dirA
│   └── ProdA
│   ├── Brief
│   ├── Results
│   └── Studies
│      └── Form.xlsx
└── dirB
└── BrandB
└── ProdB
├── Brief
└── Results
└── Studies
└── Form.xslx
So basically i have to find files Form.xlsx and move it from subdirectory Brief to subdirectory Studies (create it if it does not exists), both at the same level.
when i do:
find . -name '*.xlsx' -exec mv '{}' ../Studies ';'
I got:
.
├── dirA
│   └── ProdA
│   ├── Brief
│   ├── Results
│   └── Studies
└── dirB
└── BrandB
└── ProdB
├── Brief
└── Results
You shouldn't use .. to get the matched file's parent directory, use dirname instead.
find . -name "*.xlsx" -exec sh -c 'mv {} "$(dirname $(dirname {}))/Studies/"' \;
Have a try! :)

tree terminal command: Avoid printing all sub folders/files and putting limit

I would like to print all the subdirectories and files from a certain directory. But some of the subfolders have humungous number of files and I would like to cap the number of subdirectories/files they print for each subfolder where it goes over that cap. How do I do it?
Currently I have this situation:
/data$ tree
.
├── filenames.json
├── tripletlists
│   ├── class_tripletlist_test.txt
│   ├── class_tripletlist_train.txt
│   ├── class_tripletlist_val.txt
│   ├── closure_tripletlist_test.txt
│   ├── closure_tripletlist_train.txt
│   ├── closure_tripletlist_val.txt
│   ├── gender_tripletlist_test.txt
│   ├── gender_tripletlist_train.txt
│   ├── gender_tripletlist_val.txt
│   ├── heel_tripletlist_test.txt
│   ├── heel_tripletlist_train.txt
│   └── heel_tripletlist_val.txt
└── ut-zap50k-images
├── Boots
│   ├── Ankle
│   │   ├── adidas
│   │   │   ├── 8030969.3.jpg
│   │   │   └── 8030970.107722.jpg
│   │   ├── adidas Kids
│   │   │   ├── 8070145.388249.jpg
│   │   │   └── 8070146.388250.jpg
│   │   ├── adidas Originals
│   │   │   ├── 8027274.372160.jpg
│   │   │   ├── 8027274.372161.jpg
│   │   │   ├── 8027310.115329.jpg
│   │   │   ├── 8027310.183092.jpg
│   │   │   ├── 8027320.372147.jpg
│   │   │   └── 8027320.372178.jpg
│   │   ├── adidas Originals Kids
│   │   │   ├── 8025627.371498.jpg
│   │   │   ├── 8025627.74095.jpg
│   │   │   ├── 8025719.11196.jpg
You can use the flag --filename N in tree --filenames N where N is the number of caps. For example, if I just want to print maximum of four subdirectories or files per subdirectory, you can youse tree --filename 4.
>> ls
filenames.json tripletlists ut-zap50k-images
>> tree --filelimit 4
.
├── filenames.json
├── tripletlists [12 entries exceeds filelimit, not opening dir]
└── ut-zap50k-images
├── Boots [5 entries exceeds filelimit, not opening dir]
├── Sandals
│   ├── Athletic [6 entries exceeds filelimit, not opening dir]
│   ├── Flat [314 entries exceeds filelimit, not opening dir]
│   └── Heel [25 entries exceeds filelimit, not opening dir]
├── Shoes [10 entries exceeds filelimit, not opening dir]
└── Slippers
├── Boot [6 entries exceeds filelimit, not opening dir]
├── Slipper Flats [77 entries exceeds filelimit, not opening dir]
└── Slipper Heels
├── Daniel Green [8 entries exceeds filelimit, not opening dir]
└── L.B. Evans
├── 7590239.255.jpg
└── 7590239.72.jpg

Generate a list or map of css files

I'm starting to work on a new app at my company. I'm hoping to run a quick process that will generate an outline, tree, or other map-type thing of all of the CSS and SASS files in the app directory.
I know I can grep it, but I wanted to see if someone had something more targeted I could use.
If you're simply looking to generate a tree, the common tree command can filter by file type if provided a pattern. Maybe this will help:
tree -P "*.css" --prune
The -P option allows you to match a pattern, and the --prune option hides empty folders (or ones which don't contain match files).
It's a pretty nifty tool; here's some sample output from tree -P "*.js" --prune on a node project directory:
.
├── Authorize.js
├── collections.js
├── functions
│   ├── downloadImage.js
│   ├── generateThumbnails.js
│   ├── hashImage.js
│   ├── loadMedia.js
│   └── uploadFile.js
├── node_modules
│   ├── body-parser
│   │   ├── index.js
│   │   ├── lib
│   │   │   ├── read.js
│   │   │   └── types
│   │   │   ├── json.js
│   │   │   ├── raw.js
│   │   │   ├── text.js
│   │   │   └── urlencoded.js
│   │   └── node_modules
│   │   ├── bytes
│   │   │   └── index.js
│   │   ├── content-type
│   │   │   └── index.js
More documentation here: http://www.computerhope.com/unix/tree.htm

Custom grunt configuration

I'm porting an application from php to node(sailsjs) at the same time trying to replace ant with grunt. I like the current project build structure and I would like to preserve some of it.
It looks like below...
project root
├── build (git ignored)
│   ├── coverage
│   ├── dist(to be deployed to target env)
│   └── local(to be deployed to local env)
├── lib
│   └── some library files like selenium..etc.
├── src
│   ├── conf
│   │   └── target/local properties
│   ├── scripts(may not be needed with grunt??)
│   │   ├── db
│   │   │   └── create_scripts...
│   │   ├── se
│   │   │   └── run_selenium_scripts...
│   │   └── tests
│   │   └── run_unit_test_scripts...
│   ├── tests
│   │   └── test_code....
│   └── webapp(this is where I'd like to place node[sailsjs] code)
│      └── code....
└── wiki .etc...
It doesn't exactly have to be the same way as above but more or less I prefer to build something similar. Now, pretty much all the sailsjs examples I have seen look like below.
project root
├── .tmp
│   └── stuff...
├── package.json
├── tasks
│   ├── config
│   │   └── grunt task configs...
│   └── register
│      └── grunt task registrations...
├── tests
│   ├── unit
│   └── selenium
└── Gruntfile.js
Where should I place Gruntfile.js, app.js, package.json to achieve what I want? What other detail should I have to make grunt function and create artifacts as I want them?
Note: Obviously I'm not expecting to get all the details of grunt configuration. But I guess it helps to see where most important things go and how basic tasks could be configured.
Thanks for your answer.
It's hard to give a precise answer without a detail of your build steps, but I would suggest:
Gruntfile.js and package.json go to your root folder
you setup your individual build tasks (whatever they are) to output to build: see the doc of each task on how to do that, it's usually the dest option
Hope this helps a bit.

Resources