Hey @tuan3w, the idea of the files API (you might also see it referred to as “MFS” or Mutable FileSystem) is generally to make it easier to edit and organize files in IPFS. Since nothing can be changed after it’s added to IPFS (or: changing a file/directory change’s its hash, which basically means you’re making a new file), doing something that might simple on your hard drive, like renaming a file, can be pretty complicated in IPFS.
For example, if you had a directory structure like:
And you renamed
introduction.txt, you’d have to:
- Make a copy of the
ipfs-introduction directory and change the name of the file in it.
- Add the new directory to IPFS.
- Make a copy of the
documents directory and change the hash for the
ipfs-introduction directory in it to point to the new one.
- Add that new directory to IPFS.
- Get the new hash for
documents so you can share it.
To make that easier, the files API gives you some basic Unix-style commands (
rm) to manipulate files. When you use them, IPFS takes care of managing all the different hashes and links that have to change. So, to do the above operation, you can just:
Move the file:
> ipfs files mv /documents/ipfs-introduction/overview.txt /documents/ipfs-introduction/introduction.txt
Get the hash:
> ipfs files stat /documents
Does that make sense? Hopefully that helps clarify some of your original questions:
ipfs files cp doesn’t make a copy; it just adds a pointer to the hash of the file you are copying. So in your example, you added a pointer named
greetings in the IPFS object that represents the root directory whose value is
When we call some function like
ipfs files mkdir and
ipfs files write
On the other hand,
ipfs files write adds the data to IPFS. It’s kind of like
ipfs add [filename], except it doesn’t just add it to the IPFS network with a hash — it also puts it in the MFS root directory with a nice name. After that, you’ll have:
- The original file on your computer’s filesystem
- A copy of the file in IPFS
- Two pointers to the file in IPFS: one as just a regular hash and one from MFS
does anyone can access to it or they have my private key?
Right now, the only way to access MFS content is through the files API. That means someone has to have access to your IPFS node’s API in order to read. The API is normally private (only your computer can access it). You can change that, but I don’t recommend it.