Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Exclude node_modules in timemachine
find `pwd` -type d -maxdepth 3 -name 'node_modules' | xargs -n 1 tmutil addexclusion
@gianpaj

This comment has been minimized.

Copy link

commented Nov 13, 2017

$ find `pwd` -maxdepth 3 -type d -name 'node_modules' | xargs -n 1 tmutil addexclusion

reording the -maxdepth option fixes this warning:

find: warning: you have specified the -maxdepth option after a non-option argument -type, but options are not positional (-maxdepth affects tests specified before it as well as those specified after it).  Please specify options before other arguments.
@maxxcrawford

This comment has been minimized.

Copy link

commented Jun 15, 2018

Can this command be run from the root user directory, or is there a specific directory?

@tomwidmer

This comment has been minimized.

Copy link

commented Jun 21, 2018

To run it from a higher level if you have node projects in various subfolders, you can do:

find `pwd` -type d -maxdepth 8 -prune -name 'node_modules' | xargs -n 1 tmutil addexclusion

See also https://github.com/stevegrunwell/asimov

@gimdongwoo

This comment has been minimized.

Copy link

commented Jul 9, 2018

I think, this is work.

find `pwd` -type d -maxdepth 8 -name 'node_modules' | xargs -n 1 tmutil addexclusion

What is -prune?

@fuerst

This comment has been minimized.

Copy link

commented Aug 1, 2018

No need to pipe to xargs, just use find -exec like this:

find $(pwd) -type d -name node_modules -maxdepth 3 -exec tmutil addexclusion {} \;

Using $() for command substitution because back ticks are deprecated.

@osopolar

This comment has been minimized.

Copy link

commented Aug 15, 2018

And how to exclude only the node_modules directory but not it's sub-directories for example only /Users/Example/Projects/test/node_modules but not /Users/Example/Projects/test/node_modules/write/node_modules? I guess it does not harm but it helps to keep the list cleaner.

@osopolar

This comment has been minimized.

Copy link

commented Aug 15, 2018

You may use launchd to scan your project folder on a regular basis to exclude new node_modules directories, see https://gist.github.com/osopolar/9d8e8188091f7cbbfcee924ccfbb32dd

@qgy18

This comment has been minimized.

Copy link

commented Aug 23, 2018

Thank you for your code, it helps a lot to me. At last, I use node.js to 'exclude only the node_modules directory but not it's sub-directories for example only'

/**
* exclude some directory from timemachine
* usage: 
* node find_node_modules.js | sudo xargs -n 1 tmutil addexclusion -p
* defail for -p flag: https://www.macworld.com/article/2033804/control-time-machine-from-the-command-line.html
*/

const dir = '/Users/your_name'; // base directory
const exclusionNames = ['node_modules', '.git']; 

const { readdirSync, statSync } = require('fs');
const { sep } = require('path');

function findNodeModules(dir, list) {
    list = list || [];

    readdirSync(dir).forEach(subDir => {
        let newDir = `${dir}${sep}${subDir}`;
        
        if(statSync(newDir).isDirectory()) {
            if(exclusionNames.includes(subDir.toLowerCase())) {
                list.push(newDir);
                return;
            }

            findNodeModules(newDir, list);
        }
    });
}

let list = [];
findNodeModules(dir, list);

console.log(list.join('\n'));
@giano

This comment has been minimized.

Copy link

commented Aug 29, 2018

This tool is great for this: https://github.com/stevegrunwell/asimov

@dev01d

This comment has been minimized.

Copy link

commented Feb 3, 2019

@gimdongwoo The -prune argument will prevent find from descending into the node_modules directory once found, which otherwise litters the exclude list. So if youre like me and have a ton of projects in your working directory, rather than trying to guess the depth in the file structure with -maxdepth you can use -prune to search the whole directory to add clean entries.

This version of @fuerst's command will get you only the top level node_modules directory of each occurrence and functions like adding them in the Time Machine GUI.

find $(pwd) -type d -name node_modules -prune -exec tmutil addexclusion {} \;
@techouse

This comment has been minimized.

Copy link

commented May 9, 2019

I wrote a simple bash script which you can simply put in your development/work directory and run or add to your crontab.

#!/usr/bin/env bash

EXCLUDED_DIRECTORIES=( "env" "node_modules" "vendor" "venv" )

WORK_DIR=$(dirname "${BASH_SOURCE[0]}")
WORK_DIR=$(realpath "${WORK_DIR}")

for EXCLUDED_DIRECTORY in "${EXCLUDED_DIRECTORIES[@]}"; do
    find ${WORK_DIR} -maxdepth 2 -type d -name ${EXCLUDED_DIRECTORY} -prune -exec tmutil addexclusion {} \;
done
@chen86860

This comment has been minimized.

Copy link

commented May 28, 2019

No need to pipe to xargs, just use find -exec like this:

find $(pwd) -type d -name node_modules -maxdepth 3 -exec tmutil addexclusion {} \;

Using $() for command substitution because back ticks are deprecated.

I cannot add exclude folders using your script but after some of arguments:

find $(pwd) -type d -name node_modules -maxdepth 3 -exec tmutil addexclusion -p {} \;

Thanks anyway.

@moekhalil

This comment has been minimized.

Copy link

commented Jun 21, 2019

For some reason, prepending -prune would return an empty list.

Got this one for you guys/gals, working with prune and confirmation.

find `pwd` -name 'node_modules' -prune -type d -exec tmutil addexclusion {} \; -exec tmutil isexcluded {} \;

edit: The directories are showing as excluded when you run tmutil isexcluded <dir> (as the included command does) on each directory.

They do not however populate the list inside the table located at:

"System Preferences..." >> "Time Machine" Preferences >> "Options..." >> "Exclude these items from backup:".

I am almost certain that's expected and it should all work fine, but...SHOULD IT NOT WORK I will report back

@codexp

This comment has been minimized.

Copy link

commented Jul 25, 2019

@giano thank you for the tool recommendation, it is great!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.