2

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!

5

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:

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.

  • you're awesome. Thanks for the prompt answer. I was expecting something to fail somewhere down the line but since all the 'whereis' commands directed me to 'usr/bin', I thought I'd be fine. – Omar N Apr 12 '17 at 3:11
  • @fd0 Thanks for the clarification. I didn't recall /usr/local being there when I did a clean Sierra install a few months ago, and it didn't exist on earlier versions of OS X. But it appears you're right wrt Sierra... will update answer to clarify. – calum_b Apr 12 '17 at 15:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .