diceline-chartmagnifierquestion-marktwitter-whiteTwitter_Logo_Blue

Today I Learned

How to use the chmod command to change file or folder permissions

For a long time I've been afraid of using the shell command chmod. That because I didn't understand how it works and I feared breaking things. Today though, I learned how easy it is actually. No, not to break things, but to use the chmod command.

A Unix system allows for multiple users with different access rights, which can be changed in the shell by using this command.

The permissions are for reading, writing and executing (rwx) and are expressed for all three types of users a file or folder has. It will come in the form of rwxrwxrwx where the first batch of three characters (rwx), belong to the owner, the second to the group, and the third to all the other users (owner/group/other).

Sure, (rwx rwx rwx) would mean that everybody has all the access rights to that particular file or folder. And we all know that's not a good idea. So, most often you'll see something like rw-r--r-- which means that only the owner has the right to read and write, while everybody else can only read that particular file.

Now onto chmod, which can be used with either a symbolic or a numeric notation for the access settings. You've probably seen somebody else type chmod 644 file_name on your computer, leaving you wondering about what that means.

That's the numeric notation.

If you think about the access rights as a series of bits, the whole thing would look like this:

rwx rwx rwx = 111 111 111
rw- r-- r-- = 110 100 100
rw- r-x --- = 110 101 000

That translates into

rwx rwx rwx = chmod 777
rw- r-- r-- = chmod 644
rw- r-x --- = chmod 650

because in binary notation:

100 = 4
101 = 5
110 = 6
111 = 7

And now you've got a simple rule on how to construct your chmod command depending on what permissions you need to set.

The rwxr--r-- access rights means chmod 744, rw-r----- is chmod 640, and so on.

Simple enough, right?

Cheatsheet

As a developer, you frequently find yourself in the position when you forgot a certain syntax or you just want to see some quick code snippet to implement certain functionality. No need to google it now, you don't even need to leave your favorite terminal, welcome cheat.sh.

Usage:

curl cheat.sh/programming_language/query_string
curl cheat.sh/python/random+list+elements

Git delete all tags

Used to mark specific commits on git and often used to mark product releases on Github, git tags are important. But sometimes, you just need to delete them.

Here's a simple way to do that:

  • Delete all remote tags
git tag -l | xargs -n 1 git push --delete origin
  • Delete local tags
git tag | xargs git tag -d
  • Check if any tags are left
git tag

Use F12 as "Insert" in macOS terminal

On Apple keyboards we don't have an Insert key, and this is most annoying when using the command line or CLI tools like Midnight Commander.

We can fix this by re-assigning the F12 key:

  • Remove F12 as key from System Settings->Mission Control
  • Enable Use F1, F2, etc. keys as standard function keys in System Settings->Keyboard->Keyboard
  • Go to Terminal->Preferences and set the default profile as Pro
  • Go to Terminal->Preferences->Profiles->Keyboard and Replace F12 code with \033[2~

Make macOS remember your SSH keys after restart

We need to tell macOS to add the keys to the agent, in order to make them persistent after reboot.

We have to create new file in ~/.ssh/ called config

vi ~/.ssh/config

with the following content:

Host *
   AddKeysToAgent yes
   UseKeychain yes   

Then, we need to add our keys to the agent and macOS keychain (so your private key password is remembered).

ssh-add -D
ssh-add -K ~/.ssh/*

To verify that our keys are present in the agent:

ssh-add -l