How to use the mv function from shelljs

Find comprehensive JavaScript shelljs.mv code examples handpicked from public code repositorys.

shelljs.mv is a function in the ShellJS library that moves a file or directory from one location to another.

491
492
493
494
495
496
497
498
499
500
  path.join('ui', 'src/pages', indexFileName),
  customNextIndex,
  'utf8'
);

sh.mv(
  path.join('ui', 'src/pages', '_app.tsx'),
  path.join('ui', 'src/pages', '_app.page.tsx')
);
sh.mv(
fork icon29
star icon93
watch icon20

+ 29 other calls in file

180
181
182
183
184
185
186
187
188
189
shell.mv(path.join(locations.xcodeCordovaProj, originalName + '-Info.plist'), path.join(locations.xcodeCordovaProj, name + '-Info.plist'));
shell.mv(path.join(locations.xcodeCordovaProj, originalName + '-Prefix.pch'), path.join(locations.xcodeCordovaProj, name + '-Prefix.pch'));
// CB-8914 remove userdata otherwise project is un-usable in xcode 
shell.rm('-rf',path.join(locations.xcodeProjDir,'xcuserdata/'));
shell.mv(locations.xcodeProjDir, path.join(locations.root, name + '.xcodeproj'));
shell.mv(locations.xcodeCordovaProj, path.join(locations.root, name));

// Update locations with new paths
locations.xcodeCordovaProj = path.join(locations.root, name);
locations.xcodeProjDir = path.join(locations.root, name + '.xcodeproj');
fork icon9
star icon20
watch icon4

+ 75 other calls in file

How does shelljs.mv work?

shelljs.mv is a function in the ShellJS library that allows you to move a file or directory from one location to another. To move a file, you pass the current path of the file as the first argument and the new path as the second argument: javascript Copy code {{{{{{{ const shell = require('shelljs'); shell.mv('/path/to/myfile.txt', '/new/path/myfile.txt'); This will move the file at /path/to/myfile.txt to /new/path/myfile.txt. To move a directory, you pass the current directory path as the first argument and the new directory path as the second argument: javascript Copy code {{{{{{{ class="!whitespace-pre hljs language-javascript">const shell = require('shelljs'); shell.mv('/path/to/mydir', '/new/path/mydir'); This will move the directory at /path/to/mydir to /new/path/mydir. You can also use the -f option to force the move, overwriting the destination file if it already exists: javascript Copy code {{{{{{{ class="!whitespace-pre hljs language-javascript">const shell = require('shelljs'); shell.mv('-f', '/path/to/myfile.txt', '/new/path/myfile.txt'); By default, shelljs.mv does not print any output to the console. If you want to see the output, you can use the -v option: javascript Copy code {{{{{{{ class="!whitespace-pre hljs language-javascript">const shell = require('shelljs'); shell.mv('-v', '/path/to/myfile.txt', '/new/path/myfile.txt'); This will print a message to the console indicating the file was moved: bash Copy code {{{{{{{ class="!whitespace-pre hljs language-bash">mv /path/to/myfile.txt -> /new/path/myfile.txt In addition to these basic options, shelljs.mv supports a number of other options, such as the ability to move multiple files at once using glob patterns, or the ability to create a backup of the destination file before overwriting it.

395
396
397
398
399
400
401
402
403
404
405
// Rename file
extras.rename = function (from, to) {
  from = extras.resolve(from)
  to = extras.resolve(to)
  if (extras.exist(from)) {
    return sh.mv(from, to)
  }
}


// Resolve path
fork icon0
star icon3
watch icon1

+ 56 other calls in file

144
145
146
147
148
149
150
151
152
153
154
 * @param {String} source
 * @param {String} dest
 * @returns {Void}
 */
function move(source, dest) {
    Shell.mv("-f", source, dest);
}


/**
 * Copies the Files
fork icon0
star icon3
watch icon0

Ai Example

1
2
3
4
const shell = require("shelljs");

// Move myfile.txt from /path/to/ to /new/path/
shell.mv("/path/to/myfile.txt", "/new/path/myfile.txt");

And here's an example of using shelljs.mv to move a directory: javascript Copy code

115
116
117
118
119
120
121
122
123
124
125
126
127


async function npmPublish() {
  shell.echo("PUBLISHING ON NPM...");


  shell.cp("-ur", ["lib/bindings/**/*.{js,d.ts}"], `${distPath}/bindings/`);
  shell.mv([`${distPath}/bindings/native.prod.js`], [`${distPath}/bindings/native.js`]);
  // shell.rm("-r", [`${distPath}/**/*.test.ts`]); // No more remaining *.test.ts files for now at this step


  shell.cp("-r", ["package.json", "README.md", "../../LICENSE"], distPath);

fork icon0
star icon1
watch icon0

72
73
74
75
76
77
78
79
80
81
shell.ls(`${pluginSrc}/*lugin.tsx`).forEach(function (file) {
 if(pluginName.endsWith("Plugin") || pluginName.endsWith("plugin")){
   shell.mv(file, `${pluginSrc}/${pluginName}.tsx`);
   shell.sed ('-i', /import .*lugin from '.\/.*lugin';/, `import ${pluginName} from './${pluginName}';`, `${pluginSrc}/index.ts`);
 } else {
   shell.mv(file, `${pluginSrc}/${pluginName}Plugin.tsx`);
   shell.sed ('-i', /import .*Plugin from '.\/.*Plugin';/, `import ${pluginName} from './${pluginName}Plugin';`, `${pluginSrc}/index.ts`);
 }
});

fork icon0
star icon0
watch icon1

+ 9 other calls in file

69
70
71
72
73
74
75
76
77
78
79
shell.sed ('-i', /export default class .*Plugin extends/, `export default class ${pluginName} extends`, `${pluginSrc}/*lugin.tsx`);


// ensuring file name always ends with plugin
shell.ls(`${pluginSrc}/*lugin.tsx`).forEach(function (file) {
 if(pluginName.endsWith("Plugin") || pluginName.endsWith("plugin")){
   shell.mv(file, `${pluginSrc}/${pluginName}.tsx`);
   shell.sed ('-i', /import .*lugin from '.\/.*lugin';/, `import ${pluginName} from './${pluginName}';`, `${pluginSrc}/index.ts`);
 } else {
   shell.mv(file, `${pluginSrc}/${pluginName}Plugin.tsx`);
   shell.sed ('-i', /import .*Plugin from '.\/.*Plugin';/, `import ${pluginName} from './${pluginName}Plugin';`, `${pluginSrc}/index.ts`);
fork icon0
star icon0
watch icon1

119
120
121
122
123
124
125
126
127
128
const name = path.basename(from, extension);
const to = path.join(directory, `${tokens[name]}${extension}`);

if (tokens[name] !== undefined && from !== to) {
  if (!shell.test('-e', to)) {
    shell.mv(from, to);
  } else {
    console.error(chalk.red(`${file} could not be renamed: ${to} already exists.`));
  }
}
fork icon0
star icon0
watch icon2

+ 6 other calls in file

101
102
103
104
105
106
107
108
109
110
// rename and move metadata.json to correct folder
let aotMetadataJsonPath = path.normalize(`${filesInfo.ngcFolder}/${filesInfo.typingsFolder}/${packageName}.metadata.json`);
let aotMetadataJsonPathNew = path.normalize(`${filesInfo.typingsFolder}/index.metadata.json`);
if (sh.test('-f', aotMetadataJsonPath)) {
    sh.echo(chalk.yellowBright('  [metadata.json]: ') + chalk.greenBright(`${aotMetadataJsonPath} → ${aotMetadataJsonPathNew}`));
    sh.mv(aotMetadataJsonPath, aotMetadataJsonPathNew);
}
else {
    sh.echo(chalk.redBright(`Couldn't find metadata.json: "${aotMetadataJsonPath}"`));
    return;
fork icon0
star icon0
watch icon1

+ 15 other calls in file

44
45
46
47
48
49
50
51
52
53
shell.exec(`git pull origin ${branch}`);

// 判断是否成功
if (fs.existsSync(targetDir)) {
  if (outputDir) {
    shell.mv(targetDir, outputDir);
    shell.mv(outputDir, basePath);
  } else {
    shell.mv(targetDir, basePath);
  }
fork icon0
star icon0
watch icon0

+ 5 other calls in file