Something went wrong in one of the above commands. This is what it should be;
cd ~/RetroPie/emulators/RetroArch/tools
./retroarch-joyconfig -o p1.cfg -p 1 -j 1
So
cd ~/RetroPie/emulators/RetroArch/tools
./retroarch-joyconfig -o p1.cfg -p 1 -j 1