npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

find-remove

v5.0.0

Published

recursively finds files and/or directories by filter options from a start directory onwards and deletes these according to plenty of options you can configure. useful if you want to clean up stuff within a directory in your node.js app.

Downloads

48,353

Readme

find-remove

finally in typescript (since v5)

recursively finds files by filter options from a start directory onwards and deletes only those which 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, file creation date and ignore by name, yeah!

installation

to install find-remove, use npm:

$ npm install -S find-remove

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

import findRemoveSync from "find-remove";

quick examples

1. delete all _.bak or _.log files within the /temp/ directory

const 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:

{
    '/tmp/haumiblau.bak': true,
    '/tmp/dump.log': true
}

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

const result = findRemoveSync("/temp", { files: "dump.log" });

3. same as above, but also deletes any subfolders

const result = findRemoveSync("/temp", { files: "dump.log", dir: "*" });

4. delete all *.bak files but not file 'haumiblau.bak'

const result = findRemoveSync("/temp", { extensions: [".bak"], ignore: "haumiblau.bak" });

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

const result = findRemoveSync("/dist", { dir: "CVS" });

6. delete all jpg files older than one hour with limit of 100 files deletion per operation

const result = findRemoveSync("/tmp", {
  age: { seconds: 3600 },
  extensions: ".jpg",
  limit: 100,
});

7. delete all files with prefix 'filenamestartswith'

const result = findRemoveSync("/tmp", { prefix: "filenamestartswith" });

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

const result = findRemoveSync("/tmp", { maxLevel: 2, extensions: ".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.

9. delete everything recursively (hey, who needs that when you can use nodejs' fs.unlink?)

const result = findRemoveSync(rootDirectory, { dir: "*", files: "*.*" });

10. delete all files that match a regular expression

const result = findRemoveSync(rootDirectory, { files: "example[1-3]", regex: true });

this deletes files example1.txt, example2.txt, and example3.txt, but not example8.txt.

11. delete all directories that match a regular expression

const result = findRemoveSync(rootDirectory, { dir: "^assets_", regex: true });

this deletes all directories that start with assets_.

api

findRemoveSync(dir, options)

findRemoveSync takes any start directory and searches files from there for removal. the selection of files for removal depends on the given options. and at last, it deletes the selected files/directories.

arguments

  • 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 extensions 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 files 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.
    • regex - set to true to treat files or dir option strings as regular expression patterns.

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

the unit tests are good examples on how to use the above arguments.

returns

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

todo

  • add more filtering options (e.g. combinations)
  • have an asynchronous solution
  • use streams instead

license

MIT