Skip to content

Commit 638d8f3

Browse files
committed
Update Bash_shell-SomeUsefulTricks.md
1 parent f51274a commit 638d8f3

File tree

1 file changed

+42
-7
lines changed

1 file changed

+42
-7
lines changed

Bash_shell-SomeUsefulTricks.md

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
* ### File operations
2727

28-
* [What do file and directory permissions mean?](#what-do-file-and-directory-permissions-mean)
28+
* [What do file and directory permissions mean?](#file-and-directory-permissions)
2929
* [Clear the contents of a file without deleting the file](#clear-the-contents-of-a-file-without-deleting-the-file)
3030
* [List all directories - not files, just directories](#list-all-directories---not-files-just-directories)
3131
* [Pitfalls of parsing `ls`](#pitfalls-of-parsing-ls)
@@ -615,19 +615,54 @@ For now, I will remain *skeptical* that ***"snake case"*** or any other case-rel
615615
616616
[****](#table-of-contents)
617617
618-
## What do file and directory permissions mean?
618+
## File and directory permissions
619619
620620
>**File permissions:**
621621
>
622-
>r = read the contents of the file
623-
>w = modify the file
624-
>x = run the file as an executable
622+
>r = read the contents of the file; value: `4`; binary: `100`
623+
>w = modify the file; value `6`; binary: `110`
624+
>x = run the file as an executable; value `7`; binary: `111`
625625
626626
> **Directory permissions:**
627627
>
628-
> r = list the contents of the directory, but not 'ls' into it
628+
> r = list the contents of the directory, but not `cd` into it
629629
> w = delete or add a file in the directory
630-
> x = move into the directory
630+
> x = search/list the directory contents
631+
632+
To modify file or directory permissions, the `chmod` command is used. Perhaps the *simplest* usage of `chmod` is to express the permissions as numeric values; e.g. `chmod 644 /path/to/file`. This perhaps makes more sense if we take the `644` permissions one digit at a time (6, 4, 4); for a **file** :
633+
634+
- `6` : file "owner" permission; expressed as binary: `110` => permission to write (modify) the file
635+
- `4` : file "group member" permission; expressed as binary: `100`
636+
- `4` : "everyone else" permission; expressed as binary: `100`
637+
638+
IOW, each user entity (owner, group member, everyone else) has his permissions set by an octal (0-7) code. The *"everyone else"* designation refers to users who are **not** the owner, and **not** a group member.
639+
640+
Perhaps the most frequent need for `chmod` is to declare a *script* file as *executable*. For example, you have written a script - `helloworld.sh`, and now you want to ***run*** that script. Before you can run it, the script file must be marked as executable:
641+
642+
```bash
643+
$ chmod 755 helloworld.sh
644+
$ ./helloworld.sh
645+
Hello World!
646+
$
647+
```
648+
649+
But there are many other situations where `chmod` could (and should) be used. An example I encountered recently was *uniformly* setting all file and folder permissions in a "music library" I created. The library was sourced from various locations: an old Windows machine, and even older NAS drive, YouTube downloads, etc, etc. It was a *mess*! Complicating this was of course the fact that file and folder permissions mean different things, and they should not be set the same. Here's a useful technique to remember:
650+
651+
```bash
652+
# first - set all folder permissions so any user can access all music in the library
653+
# this means setting folder permissions to include "execute" privileges
654+
# we use 'find' with the '-exec' option to identify & set permissions for all folders
655+
656+
$ find /path/to/dir -type d -exec chmod 755 {} +
657+
658+
# second - set all file permissions so any user can read all music, only the owner can change the files
659+
660+
$ find /path/to/dir -type f -exec chmod 644 {} +
661+
```
662+
663+
Finally - you might be wondering why the command to manipulate permissions is called `chmod`. Consulting [wikipedia](https://en.wikipedia.org/wiki/Chmod), provides the answer to that question:
664+
665+
> `chmod` is a shell command for changing access permissions (and *flags*) of files and directories/folders. The name is short for ***ch**ange **mod**e* where ***mode*** refers to the permissions and flags collectively.
631666
632667
[**⋀**](#table-of-contents)
633668

0 commit comments

Comments
 (0)