Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
a12a7e1
bump few packages
Jul 17, 2015
c3b98fb
just bump few packages
Jul 28, 2015
9a25980
bump some packages
binarykitchen Sep 6, 2015
9b5f800
optimize files a bit and remove obsolete stuff
binarykitchen Sep 6, 2015
4edeccc
version bump
binarykitchen Sep 6, 2015
f9f3555
another small maintenance bump
Nov 9, 2015
0423de6
version bump
Nov 9, 2015
235825a
bump few packs
Jan 26, 2016
c63ec18
maintenance bump
binarykitchen Apr 3, 2016
e12dcfd
bump version
binarykitchen Apr 3, 2016
f22ecab
mtime instead of ctime
nrullo May 19, 2016
8a325be
mtime instead of ctime. When you try to find a file by timestamp, you…
nrullo May 19, 2016
ac547d2
Merge branch 'master' of https://github.com/nrullo/find-remove
nrullo May 20, 2016
615919f
Merge pull request #6 from nrullo/master
binarykitchen May 21, 2016
bab9f39
Patch version, using mtime now
binarykitchen May 21, 2016
cc4b4ae
bump few packages and remove semi colons
binarykitchen Sep 27, 2016
30a7c47
rewrite unit tests and api for v1.0
binarykitchen Sep 28, 2016
b8dc0e1
improve readme further
binarykitchen Sep 28, 2016
d94e1ce
fix syntax in readme
binarykitchen Sep 28, 2016
c58ed1a
bump rimraf, nodeunit and async for v1.0.1
binarykitchen Mar 18, 2017
ca81be9
Adding an option to limit the number of files to be deleted at a sing…
Dec 4, 2017
615f0d1
Merge pull request #10 from nirshneor/file-deletion-limitation
binarykitchen Dec 4, 2017
a13c849
v1.1.0 with limit number option, yarn.lock and few pack bumps
binarykitchen Dec 4, 2017
4d20807
Update README.md
Dec 5, 2017
a167b22
Merge pull request #11 from nirshneor/patch-1
binarykitchen Dec 6, 2017
7f6daf4
Adding an option to delete by prefix. Tests added too.
Dec 6, 2017
7695e3e
Merge pull request #12 from nirshneor/file-deletion-limitation
binarykitchen Dec 7, 2017
92dd66d
v1.2.0
Dec 7, 2017
07c9e9e
Add promise version
Feb 11, 2018
fdf9807
Replace files instead of adding new ones (lib and test)
Feb 13, 2018
f1d2e14
Update node version to 8,x
Feb 13, 2018
37fc0ad
Update tests for async
Feb 13, 2018
146af38
Remove unused reject callback
Feb 13, 2018
fb7ccc6
Replace deprecated os.tmpDir call
Feb 13, 2018
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
.DS_Store
*.log
node_modules
.idea/
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
language: node_js
node_js:
- "8.9.4"
deploy:
provider: npm
email: [email protected]
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2013 Michael Heuberger
Copyright (c) 2013 - 2017 Michael Heuberger

Permission is hereby granted, free of charge,
to any person obtaining a copy of this software and
Expand Down
87 changes: 47 additions & 40 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
# find-remove
# find-remove v1.0 (breaking!)

[![Build Status](https://travis-ci.org/binarykitchen/find-remove.png?branch=master)](https://travis-ci.org/binarykitchen/find-remove)

recursively finds files by filter options from a start directory onwards and deletes these. useful if you want to clean up a directory in your node.js app.
recursively finds files by filter options from a start directory onwards and deletes those who meet conditions you can define. useful if you want to clean up a directory in your node.js app.

you can filter by extensions, names, level in directory structure and file creation date yeah!
you can filter by extensions, names, level in directory structure, file creation date and ignore by name, yeah!

## installation

to install find-remove, use [npm](http://github.com/isaacs/npm):

$ npm install find-remove
$ npm install -S find-remove

then in your node.js app, get reference to the function like that:

```javascript
var findRemoveSync = require('find-remove');
var findRemoveSync = require('find-remove')
```

## quick examples

### delete all *.bak and *.log files within the /temp/ directory
### 1. delete all *.bak or *.log files within the /temp/ directory

```javascript
var result = findRemoveSync('/temp', {extensions: ['.bak', '.log']});
var result = findRemoveSync('/temp', {extensions: ['.bak', '.log']})
```

the return value `result` is a json object with successfully deleted files. if you output `result` to the console, you will get something like this:
Expand All @@ -35,57 +35,59 @@ the return value `result` is a json object with successfully deleted files. if y
}
```

### delete all files called 'dump.log' within the /temp/ directory and any of its subfolders
### 2. delete all files called 'dump.log' within the /temp/ directory and within its subfolders

```javascript
var result = findRemoveSync(rootDirectory, {files: 'dump.log'});
var result = findRemoveSync('/temp', {files: 'dump.log'})
```

### same as above but do not delete file 'haumiblau.bak'
### 3. same as above, but also deletes any subfolders

```javascript
var result = findRemoveSync(rootDirectory, {files: 'dump.log', ignore: 'haumiblau.bak'});
var result = findRemoveSync('/temp', {files: 'dump.log', dir: '*'})
```

### delete recursively all files called 'dump.log' AND also all files with the extension '.dmp' within /temp/
### 4. delete all *.bak files but not file 'haumiblau.bak'

```javascript
var result = findRemoveSync('/tmp', {files: 'dump.log', extension: '.dmp'});
var result = findRemoveSync('/temp', {extensions: ['.bak'], ignore: 'haumiblau.bak'})
```

### delete recursively all directories called 'CVS' within /dist/
### 5. delete recursively any subdirectory called 'CVS' within /dist/

```javascript
var result = findRemoveSync('/dist', {dir: 'CVS'});
var result = findRemoveSync('/dist', {dir: 'CVS'})
```

### delete everything inside AND including the /temp directory

just call it without options because no options means nothing is filtered.
### 6. delete all jpg files older than one hour with limit of 100 files deletion per operation

```javascript
var result = findRemoveSync('/tmp');
var result = findRemoveSync('/tmp', {age: {seconds: 3600}, extensions: '.jpg', limit: 100})
```

### delete all jpg files older than one hour
### 7. delete all files with prefix 'filenamestartswith'

```javascript
var result = findRemoveSync('/tmp', {age: {seconds: 3600}, extensions: '.jpg'});
var result = findRemoveSync('/tmp', {prefix: 'filenamestartswith'})
```

### apply filter options only for two levels inside the /temp directory
### 8. apply filter options only for two levels inside the /temp directory for all tmp files

```javascript
var result = findRemoveSync('/tmp', {maxLevel: 2, extensions: '.tmp'});
var result = findRemoveSync('/tmp', {maxLevel: 2, extensions: '.tmp'})
```

this deletes any `.tmp` files up to two levels, for example:
/tmp/level1/level2/a.tmp
this deletes any `.tmp` files up to two levels, for example: `/tmp/level1/level2/a.tmp`

but not `/tmp/level1/level2/level3/b.tmp`

why the heck do we have this `maxLevel` option? because of performance. if you care about deep subfolders, apply that option to get a speed boost.

but not:
/tmp/level1/level2/level3/b.tmp
### 9. delete everything recursively (hey, who needs that when you can use nodejs' fs.unlink?)

why the heck do we have this option? because of performance. if you do not care about deep subfolders, apply that option to get a speed boost.
```javascript
var result = findRemoveSync(rootDirectory, {dir: "*", files: "*.*"})
```

## api

Expand All @@ -95,25 +97,30 @@ findRemoveSync takes any start directory and searches files from there for remov

__arguments__

* dir - any directory to search for files for deletion
* options - currently two properties are supported:
* files - can be a string or an array of files you want to delete within `dir`. also `*.*` is allowed here if you want to remove all files (but not directories).
* dir - can be a string or an array of directories you want to delete within `dir`.
* extensions - this too, can be a string or an array of file extenstions you want to delete within `dir`
* ignore - useful to exclude some files. again, can be a string or an array of file names you do NOT want to delete within `dir`
* age.seconds - can be any float number. findRemoveSync then compares it with the file stats and deletes those with creation times older than `age.seconds`
* maxLevel - advanced: limits filtering to a certain level. useful for performance. recommended for crawling huge directory trees.
* test - advanced: set to true for a test run, meaning it does not delete anything but returns an array of files/directories it would have deleted.
* `dir` - any directory to search for files and/or directories for deletion (does not delete that directory itself)
* options - currently those properties are supported:
* `files` - can be a string or an array of files you want to delete within `dir`.
* `dir` - can be a string or an array of directories you want to delete within `dir`.
* `extensions` - this too, can be a string or an array of file extentions you want to delete within `dir`.
* `ignore` - useful to exclude some files. again, can be a string or an array of file names you do NOT want to delete within `dir`
* `age.seconds` - can be any float number. findRemoveSync then compares it with the file stats and deletes those with modification times older than `age.seconds`
* `limit` - can be any integer number. Will limit the number of <b>files</b> to be deleted at single operation to be `limit`
* `prefix` - can be any string. Will delete any files that start with `prefix`.
* `maxLevel` - advanced: limits filtering to a certain level. useful for performance. recommended for crawling huge directory trees.
* `test` - advanced: set to true for a test run, meaning it does not delete anything but returns a JSON of files/directories it would have deleted. useful for testing.

as a precaution, nothing happens when there are no options.

when no options are given, are undefined or null, then everything including directories are removed as if there were no filters. this also applies when only the `maxLevel` parameter is given.
the unit tests are good examples on how to use the above arguments.

__returns__

associative array of files/directories that were deleted.
JSON of files/directories that were deleted. For limit option - will only return number of files deleted.

## todo

* add more filtering options (combinations, regex, etc.)
* needs a rewrite
* add more filtering options (combinations, regex, etc.)
* have an asynchronous solution
* use streams instead

Expand Down
Loading