如何修复 windows 终端中的 'find' 命令
How to fix 'find' command in windows terminal
我写了一个 Nodejs 脚本来查找最后 changed/modified 个文件名。
出于这个原因,我正在使用 find
CLI 命令。我有一个隐藏文件 .change 用于将其他文件与该文件进行比较(修改时间)。
下面是一段代码:
const es6dir = 'es6';
const path2dir = './htdocs/';
const exec = require("child_process").exec;
exec(`find ${path2dir + es6dir}/ -type f -newer .change`, (error, stdout) => {
if(error){
console.log(`Error: ${error}`);
return;
}
console.log(stdout);
//update .change modified timestamp
exec('touch -c .change');
}
如果我在 Git Bash 中 运行 这个命令,一切正常,但如果我使用 windows 终端,它说一个不正确的命令。
有没有同时适用于 Linux 和 Windows 终端的简单方法?
我想在两个平台上 运行 这个命令,因为一些团队成员正在 Linux 上工作,而其他人正在使用 windows 机器。
考虑使用 Node 的内置 fs.Stats
over platform specific commands or utilities. The fs
module exposing fs.stat
method returns the property mtime
包含上次修改时间(以毫秒为单位)。
可以通过子进程或使用 fs.stat
和 fs.writeFile
来实现交叉兼容性。
Stats returns 像这样的对象:
Stats {
dev: 16777220,
mode: 33188,
nlink: 1,
uid: 501,
gid: 20,
rdev: 0,
blksize: 4096,
ino: 5077219,
size: 11,
blocks: 8,
atimeMs: 1556271390822.264,
mtimeMs: 1556271389892.5886,
ctimeMs: 1556271389892.5886,
birthtimeMs: 1556270439285.706,
atime: 2019-04-26T09:36:30.822Z,
mtime: 2019-04-26T09:36:29.893Z,
ctime: 2019-04-26T09:36:29.893Z,
birthtime: 2019-04-26T09:20:39.286Z }
正如评论和答案中所建议的那样,我同意这是一种更好的方法。以下是创建新文件和检查创建日期的方法。
const fs = require('fs');
// Directory
const PATH = './';
// Get file's stats
fs.stat(`./.change`, function(error, stats) {
if (error) { throw error; } // Throw if an error, file not found
let time = Date.now(); // Current Time
console.log(`Current .change: Created: `, stats['mtime']); // Created Time
// If current time > file creation time
if (time > stats['mtime']) {
// writeFile function with filename, content and callback function
fs.writeFile(`${PATH}/.change`, 'Inside File', function (error) {
if (error) { throw error; }
console.log('File is updated successfully.');
});
}
});
我写了一个 Nodejs 脚本来查找最后 changed/modified 个文件名。
出于这个原因,我正在使用 find
CLI 命令。我有一个隐藏文件 .change 用于将其他文件与该文件进行比较(修改时间)。
下面是一段代码:
const es6dir = 'es6';
const path2dir = './htdocs/';
const exec = require("child_process").exec;
exec(`find ${path2dir + es6dir}/ -type f -newer .change`, (error, stdout) => {
if(error){
console.log(`Error: ${error}`);
return;
}
console.log(stdout);
//update .change modified timestamp
exec('touch -c .change');
}
如果我在 Git Bash 中 运行 这个命令,一切正常,但如果我使用 windows 终端,它说一个不正确的命令。
有没有同时适用于 Linux 和 Windows 终端的简单方法?
我想在两个平台上 运行 这个命令,因为一些团队成员正在 Linux 上工作,而其他人正在使用 windows 机器。
考虑使用 Node 的内置 fs.Stats
over platform specific commands or utilities. The fs
module exposing fs.stat
method returns the property mtime
包含上次修改时间(以毫秒为单位)。
可以通过子进程或使用 fs.stat
和 fs.writeFile
来实现交叉兼容性。
Stats returns 像这样的对象:
Stats {
dev: 16777220,
mode: 33188,
nlink: 1,
uid: 501,
gid: 20,
rdev: 0,
blksize: 4096,
ino: 5077219,
size: 11,
blocks: 8,
atimeMs: 1556271390822.264,
mtimeMs: 1556271389892.5886,
ctimeMs: 1556271389892.5886,
birthtimeMs: 1556270439285.706,
atime: 2019-04-26T09:36:30.822Z,
mtime: 2019-04-26T09:36:29.893Z,
ctime: 2019-04-26T09:36:29.893Z,
birthtime: 2019-04-26T09:20:39.286Z }
正如评论和答案中所建议的那样,我同意这是一种更好的方法。以下是创建新文件和检查创建日期的方法。
const fs = require('fs');
// Directory
const PATH = './';
// Get file's stats
fs.stat(`./.change`, function(error, stats) {
if (error) { throw error; } // Throw if an error, file not found
let time = Date.now(); // Current Time
console.log(`Current .change: Created: `, stats['mtime']); // Created Time
// If current time > file creation time
if (time > stats['mtime']) {
// writeFile function with filename, content and callback function
fs.writeFile(`${PATH}/.change`, 'Inside File', function (error) {
if (error) { throw error; }
console.log('File is updated successfully.');
});
}
});