This project offers a set of functions to perform disk cleanup operations in Node.js, including backup creation, restoration, and removal of files and directories.
npm install disk-cleanup
Removing Temporary Files
The removeTemporary function is used to delete temporary files from a specified directory.
const diskCleanup = require('disk-cleanup');
diskCleanup.removeTemporary('/path/to/directory', (err, result) => {
if (err) {
console.error('Error:', err);
} else {
console.log('Result:', result);
}
});
Backup and Restore
This package also offers backup and restore functionalities.
*const diskCleanup = require('disk-cleanup');
*const backupPath = diskCleanup.createBackupDirectory('/path/to/directory');
*diskCleanup.backupFile("/path/of/file","/path/of/backup directory");
*diskCleanup.restoreBackup( "filename","backupPath", "dirPath");
*diskCleanup.removeBackupFile( "fileName", "backupPath");
*diskCleanup.removeBackupdir("backupPath");
Filter by File Size
const diskCleanup = require('disk-cleanup');
//size will given in the bytes in all functions
// Deletes files of a specified size from a given directory.
diskCleanup.filterEqual('/path/to/directory', size, (err, result) => {
if (err) {
console.error('Error:', err);
} else {
console.log('Result:', result);
}
});
//Deletes files greater than a specified size from a given directory.
diskCleanup.filterGreater('/path/to/directory', size, (err, result) => {
if (err) {
console.error('Error:', err);
} else {
console.log('Result:', result);
}
});
//Deletes files smaller than a specified size from a given directory.
diskCleanup.filterSmaller('/path/to/directory', size, (err, result) => {
if (err) {
console.error('Error:', err);
} else {
console.log('Result:', result);
}
});
//if u know size not in bytes than you can simply convert by functions:
diskCleanup.filterSmaller('/path/to/directory', kb_to_bytes(size in bytes), (err, result) => {
if (err) {
console.error('Error:', err);
} else {
console.log('Result:', result);
}
});
Size conversion
kb_to_bytes(20) // convert 20 kb into bytes
mb_to_bytes(20) //20 mb to bytes
gb_to_bytes(10)
tb_to_bytes(20)
pb_to_bytes(20)
eb_to_bytes(20)
zb_to_bytes(20)
yb_to_bytes(20)