The Rambam writes that crushing pepper corns on Shabbat is a Torah prohibition of the Melacha of Tohen-grinding. However, Maran, in Siman 321:7, rules that if one grinds with two Shinuim-deviations from the normal way of grinding, it is permitted. For example, he uses the back of a knife or the bottom of the glass to pulverize pepper corns or garlic and also does it on a table as opposed to a bowl.
The Ben Ish Hai writes that the custom of Baghdad was to be lenient to grind with even one Shinui. Nevertheless, Hacham Ovadia, Hacham Bension, the Menuhat Ahaba and all the other Poskim rule that one must be strict and grind only with two Shinuim.
When grinding with two Shinuim, there is no need to do so immediately before consumption, as opposed to chopping vegetables in a regular fashion which is only permitted within half an hour of the meal. Of course, his intent must be to grind the spice for use on Shabbat, and not prepare for after Shabbat.
Some people crush spices, such as pepper corns, in a cloth so that the fragments don’t scatter. The Poskim discuss whether this constitutes a problem of “Molid Reha”-imbuing an item with a fragrance- which is an Issur D’rabanan (Rabbinic prohibition). The Mishna Berura rules that there is no issue, since that is not his intent-Davar She’eno Mitkaven. Hacham Ovadia writes that although he does not intend to imbue the cloth with a smell, it is a Pesik Resheh-an inevitable outcome, which is usually prohibited. Nevertheless, it is permitted, since Molid Reha is only a D’rabanan, and it is “Lo Ichpat Leh “-he does not care about the result. This is especially true, since the Rambam did not even bring this prohibition of Molid Reha.
It is permitted to grind pepper corns or garlic with two Shinuim-using the back of a knife or a glass directly on the table-even for consumption at a late time on Shabbat. There is no problem to grind on a cloth, even though the cloth receives the smell of the spices.