1 | # move(src, dest, [options, callback])
|
2 |
|
3 | Moves a file or directory, even across devices.
|
4 |
|
5 | - `src` `<String>`
|
6 | - `dest` `<String>`
|
7 | - `options` `<Object>`
|
8 | - `overwrite` `<boolean>`: overwrite existing file or directory, default is `false`.
|
9 | - `callback` `<Function>`
|
10 |
|
11 | ## Example:
|
12 |
|
13 | ```js
|
14 | const fs = require('fs-extra')
|
15 |
|
16 | const srcpath = '/tmp/file.txt'
|
17 | const dstpath = '/tmp/this/path/does/not/exist/file.txt'
|
18 |
|
19 | // With a callback:
|
20 | fs.move(srcpath, dstpath, err => {
|
21 | if (err) return console.error(err)
|
22 |
|
23 | console.log('success!')
|
24 | })
|
25 |
|
26 | // With Promises:
|
27 | fs.move(srcpath, dstpath)
|
28 | .then(() => {
|
29 | console.log('success!')
|
30 | })
|
31 | .catch(err => {
|
32 | console.error(err)
|
33 | })
|
34 |
|
35 | // With async/await:
|
36 | async function example (src, dest) {
|
37 | try {
|
38 | await fs.move(srcpath, dstpath)
|
39 | console.log('success!')
|
40 | } catch (err) {
|
41 | console.error(err)
|
42 | }
|
43 | }
|
44 |
|
45 | example(srcpath, dstpath)
|
46 | ```
|
47 |
|
48 | **Using `overwrite` option**
|
49 |
|
50 | ```js
|
51 | const fs = require('fs-extra')
|
52 |
|
53 | fs.move('/tmp/somedir', '/tmp/may/already/existed/somedir', { overwrite: true }, err => {
|
54 | if (err) return console.error(err)
|
55 |
|
56 | console.log('success!')
|
57 | })
|
58 | ```
|