eolas/Programming Languages/Shell Scripting/File permissions and execution.md
2022-04-23 13:26:53 +01:00

1.9 KiB

tags
Programming_Languages
shell

Viewing file permissions

In order to see file permissions within the terminal, use the -l or -rfl with the ls command. Remember this command can be applied at both the directory and single-file level. For example:

drwxr-xr-x  7 thomas thomas 4096 Oct  2 19:22 angular-learning-lab
drwxr-xr-x  5 thomas thomas 4096 Oct 17 18:05 code-exercises
drwxr-xr-x  5 thomas thomas 4096 Sep  4 16:15 js-kata
drwxr-xr-x  9 thomas thomas 4096 Sep 26 18:10 sinequanon
drwxr-xr-x 12 thomas thomas 4096 Sep 19 17:41 thomas-bishop
drwxr-xr-x  5 thomas thomas 4096 Sep  4 19:24 ts-kata

chmod

We use chmod for transferring ownership and file permissions quickly from the command-line.

Octal notation

chmod uses octal notation. Each numeral refers to a permission set. There are three numerals. The placement denotes the user group. From left to right this is:

  • user
  • group
  • everyone else.

If you are working solo and not with group access to files, you can disregard assigning the other numerals, by putting zeros in as placeholders.

Permission codes

Example

$ chmod -v 700 dummy.txt
$ ls -l dummy.txt
$ -rwx------  1 thomasbishop  staff  27 13 May 15:42 dummy.txtExample

Useful options

-v → verbose: tell the user what chmod is doing

-r → work recursively, i.e apply the action to directories as well as files

-f →silent: suppress most error messages

Running bash files

In most cases, especially when you are working alone, the most frequent codes you are going to need are 700 and 600. When shell scripting, you need to make your scripts executable for them to work, therefore you should always chmod 700 when creating a .sh file.

Then to invoke the script from the shell you simply enter:

./your-bash-script.sh