Skip to content

Instantly share code, notes, and snippets.

@Katamori
Created May 1, 2019 12:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Katamori/160d84ba10a31a1e4c513fdc706c7643 to your computer and use it in GitHub Desktop.
Save Katamori/160d84ba10a31a1e4c513fdc706c7643 to your computer and use it in GitHub Desktop.
IPFS utility commands

IPFS utility commands

IPFS is an amazing service, but lacks certain shorthand functionality, so instead of creating these for myself and then forgetting forever, I decided to share it with the community.

Pin folder content selectively

Sometimes, even if you pin content, you may not want to pin a whole directory as a whole. For those purposes, however, there's no way to list a directory content with its hashes in any particular order.

For that, there's a lot of Linux shell tools you can use:

ipfs ls <hash> | sort -n -k 2,2 | sed -e 's/\s.*$//' | head -n <x> | xargs -n1 <command>

Let me explain:

The basic issue is that:

  • ipfs ls outputs a a directory content in the following format: <hash> <size in bytes> <name>, AND
  • above that, it doesn't provide any sorting functionality.

This is where piping comes in handy: with |, you can tell bash to send the output of ipfs ls to sort. Option -n turns off any other sorting methods, and -k 2,2 tells it to use the second "column" of the data input.

The output at this point is identical to ipfs ls, except now the smallest items are coming at first. Note: you can specify -r to sort to reverse order so that the biggest files are the first ones.

If you want filtering, at this pont you can also pipe grep "<regex>" to weed out lines by pattern. For me, for example, grep "^.*\<extension>$" was used to find files with specific formats.

Then sed -e 's/\s.*$//' formats it by returning only the first word: the hash. Meaning that the output of sed is a list of IPFS hashes, ordered by size.

Of course, piping on its own is not enough anymore, because you don't want to add all these hashes. At this point, however, you have multiple choices; I decided to keep it simple with | head -n <x> which limits the hash list to the first x findings. (x is a positive integer, obviously) You can narrow it down by the size, of course, in an earlier point in piping.

IPFS commands, that you may want to use at this point (like ipfs get or ipfs pin add), of course, can process only single-line strings, while the piping output is multiline. For that, I used xargs -n1 <command> which makes <command> run line by line on the output. For example, xargs -n1 ipfs get downloads all the files in the particular order you specified above.

In the end, it's just an example of course; you have countless other options or even substitutions (e.g. you can substitue head with an appropriate sed command), it's just my example I shared to help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment