Erased /usr/local/bin directory by mistake I am a somewhat newbie to Unix.  I recently started a bash tutorial where I created a hello world bash script like this:

#!/bin/bash
echo 'Hello World!'


then, I moved it into /usr/local/bin using this command:

sudo mv hello.sh /usr/local/bin


now /bin under /usr/local is no longer a directory, but a file, and when using ls -l on it, I get this output:

MacBook-Pro-de-Omar:local omarnavarro$ ls
bin
MacBook-Pro-de-Omar:local omarnavarro$ ls -l
total 8
-rwxr-xr-x  1 omarnavarro  staff  32 Apr 11 19:27 bin
MacBook-Pro-de-Omar:local omarnavarro$ cat bin
#!/bin/bash
echo 'Hello world!'


I understand that /usr/local/bin is part of the $PATH, and so I believe there must have been lots of commands in there.  Will this affect me much?  how can I restore the commands that previously lived in /usr/local/bin when it was a directory.  I can't believe I screwed up so badly!
The canonical answer, of course, is to restore /usr/local/bin from your latest backup...

The good news is that if that mv command succeeded and created a file called bin, then /usr/local/bin didn't already exist as a directory on your system, and you've lost nothing. Otherwise, hello.sh would have been moved into the /usr/local/bin directory (which was presumably the intention of the tutorial), it wouldn't have replaced it. 

This is not unexpected, as /usr/local/bin doesn't generally exist on Macs until you or a 3rd party app creates it. But now you have learned the awesome power of the command line, you know why it's important to keep good backups ;)

Had you mangled /usr/bin you'd need to reinstall the system:


Deleted /usr/local/bin and /usr/bin on OS X


The good news is system integrity protection (SIP) is making it harder to mangle system files with sudo

PS: An easy way to prevent the issue from happening is to always add a trailing / if moving files to another directory (or rely on tab completion wich does the same thing): sudo mv hello.sh /usr/local/bin/. This way you'll get an error message if the target directory doesn't exist.