How to get the CID of a folder

Question

The closest and only answer I saw is: ipfs add -r {folder-name}

But it adds the whole folder to IPFS (but you don’t want to really add, but simply to get its CID) and displays all CIDs from all files.

A better command I came with is ipfs add -qr --only-hash {folder-name}, because it only gets the CID instead of adding all the files; but, still, it outputs all CIDs instead of only the folder’s CID you want.

Working answer

How to display the CID of a folder:
ipfs add -qr --only-hash {folder-name} | tail -n 1

Display base32/CID v2 of a folder:
ipfs add -qr --only-hash {folder-name} | tail -n 1 | ipfs cid base32

Updated answer by @Jorropo

How to display the CID of a folder:
ipfs add -Qr --only-hash {folder-name}

Display base32/CID v2 of a folder:
ipfs add -Qr --only-hash {folder-name} | ipfs cid base32

Bonus

Display the CID of a single file:
ipfs add -q --only-hash {file-name}

Display the base32/CID v2 of a single file:
ipfs add -q --only-hash {file-name} | ipfs cid base32

ipfs add -Qr --only-hash {folder-name}

There is -q for --quiet and -Q for --quieter

1 Like

Many thanks.

I’ve updated it.

Months ago I was searching how to get the CID of a folder and didn’t found any answer before, so I had to figure myself a hack until I discovered this case sensitive Q flag by you. Many thanks, again; because now I can use a standard command instead of a glue, tricky workaround.

It will be important for Web3Updater so it can get the CID of the bare git repo it adds to ENS as update.