Mutable File System | Lesson 8 of 11

Move a file or directory

MFS allows you to move files between directories just as you would on your local machine, using the files.mv method.

The method looks like this:

await ipfs.files.mv(from, to, [options])

from is the source path (or paths) of the content you'd like to move. to is the destination path.

If your destination path references parent directories that don't already exist, you'll need to use the { parents: true } option, just as you did with files.mkdir.

You can use files.mv to perform a number of different operations:

// move a single file into a directory
await ipfs.files.mv('/source-file.txt', '/destination-directory')

// move a directory into another directory
await ipfs.files.mv('/source-directory', '/destination-directory')

// overwrite the contents of a destination file with the contents of a source file
await ipfs.files.mv('/source-file.txt', '/destination-file.txt')

To move multiple files into a directory, you can add multiple source paths before the to argument:

// move multiple files into a directory
await ipfs.files.mv('/source-file-1.txt', '/source-file-2.txt', '/source-file-3.txt', '/destination-directory')

If you're starting with an array of source paths, you can use JavaScript's spread syntax to expand its elements into the required format:

await ipfs.files.mv(...fromArray, '/destination-directory')

not yet startedTry it!

Move only the files (no directories) from your root directory into the some/stuff directory.

Remember that you can pass an array into files.mv as the from value. This is useful because it allows you to run the async function only once. Be sure to use the await keyword so that the mv call completes before the contents of your /some/stuff directory are evaluated.

When creating your array to pass in, ensure it only contains files, not directories. Remember that each object accessed through files.ls in IPFS has a type property that you can use to determine whether it's a file or a directory; its value is 0 for files and 1 for directories. (Hint: Try the filter or forEach array methods.)

Remember that files.mv needs paths, not filenames, so you'll need to prepend each filename in your array with /. (Hint: Try the map or forEach array methods and incorporate the name property available through the files.ls method.)

If you experience lag time after hitting "Submit", please try again, uploading smaller files to work with.

Step 1: Upload files
Step 2: Update code
View SolutionReplace with SolutionClear Default Code
Upload file(s) and update the code to complete the challenge.
You must upload a file before submitting.