shelljs

Rename file in directory (Shelljs)


How can i rename a file into the same directory?

When i run this i've got the following error:

shell.cp('-R', './../../config/test.txt', './../../config/test1.txt');

I read the docs, but none of that answers my question.

Thanks for any help.


Solution

  • Utilize the ShellJS mv() command to rename a file instead of the cp() command.

    Assuming that the path definitions in your given example actually do exist, utilize the following instead:

    var shell = require('shelljs');
    
    shell.mv('./../../config/test.txt', './../../config/test1.txt');
    

    Note: The ./ part at the beginning of each of the pathnames that you've provided seems to be redundant so you can omit that part too. For instance:

    var shell = require('shelljs');
    
    shell.mv('../../config/test.txt', '../../config/test1.txt');