Source: Standard literature
Kōgai (笄) which means "sword-needle". The kōgai was a hairdressing tool that was carried in the scabbard with a Japanese sword.
An early kōgai that is believed to date from the 15th century.
Metropolitan Museum accession number 14.100.523.
Gift of Bashford Dean, 1914.
"Old records say that the tip of the kōgai was used for scratching on hot days underneath the eboshi (烏帽子) and to stick loose hair back under the headgear, and that the mimikaki (耳掻) was used to clean the ears. There is also the thought that the entire kōgai was occasionally used for hair arranging.
But the kōgai, worn on the outside of the saya, had besides its practical use an important ornamental purpose: it conveyed the status and wealth of the wearer." 1
-Markus Sesko, 2014
The main parts of the kōgai are the grip section called dō (胴), the needle itself called sao (棹 or 竿) and the little scoop at the back of the grip called mimikaki (耳掻).
They were typically worn on the side of the scabbard facing outwards when the sword was worn, and part of their hilts would stick through a hole in the tsuba (鐔) or "guard" called kōgai-hitsu-ana (笄櫃孔).
1. Markus Sesko; "Encyclopedia of Japanese Swords". Lulu Enterprises, Inc. 2014. Page 240.