name: inverse layout: true class: center, middle, inverse --- # File and directory attributes On who has access to what and a nice hack for dealing with duplicate content .footnote[Marek Šuppa
Ondrej Jariabka
Adrián Matejov] --- layout: false # Why UNIX for Data Science? - You will almost inevitably need to set up your own server environment - Understanding how permissions work will be crucial to you being able to effectively use it - Setting up links here and there will give you a bit of a superpower unheard of on other systems --- # UNIX directory structure - On the system level, the directory structure is implemented as a tree [![:scale 100%](https://mermaid.ink/img/eyJjb2RlIjoiZ3JhcGggVEJcbiAgICByb290KFwiL1wiKVxuICAgIHJvb3QgLS0-IGJvb3QoXCJib290XCIpXG4gICAgcm9vdCAtLT4gZGV2KFwiZGV2XCIpXG4gICAgcm9vdCAtLT4gbWVkaWEoXCJtZWRpYVwiKVxuICAgIG1lZGlhIC0tPiBjZHJvbVtjZHJvbV1cbiAgICBtZWRpYSAtLT4gdXNiW1wiVVNCXCJdXG4gICAgcm9vdCAtLT4gcHJvYyhcInByb2NcIilcbiAgICByb290IC0tPiBldGMoXCJldGNcIilcbiAgICByb290IC0tPiB1c3IoXCJ1c3JcIilcbiAgICB1c3IgLS0-IHVzcmJpbihcImJpblwiKVxuICAgIHJvb3QgLS0-IGJpbihcImJpblwiKVxuICAgIHJvb3QgLS0-IGhvbWUoXCJob21lXCIpXG4gICAgaG9tZSAtLT4gamFuZShcImphbmVcIilcbiAgICBqYW5lIC0tPiBEb2N1bWVudHNcbiAgICBqYW5lIC0tPiBEb3dubG9hZHNcbiAgICBqYW5lIC0tPiBQaG90b3NcbiAgICBqYW5lIC0tPiBNdXNpY1xuICAgIERvY3VtZW50cyAtLT4gaG9tZXdvcmsoW2hvbWV3b3JrLnR4dF0pXG4gICAgcm9vdCAtLT4gdG1wKFwidG1wXCIpXG4gICAgc3ViZ3JhcGggbW91bnRwb2ludHNcbiAgICBjZHJvbVxuICAgIHVzYlxuICAgIGVuZCIsIm1lcm1haWQiOnsidGhlbWUiOiJiYXNlIiwidGhlbWVWYXJpYWJsZXMiOnsiYmFja2dyb3VuZCI6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wMCUsIDkzLjMzMzMzMzMzMzMlKSJ9fSwidXBkYXRlRWRpdG9yIjpmYWxzZX0) - There is one more level of structure underneath. --- # `inode`s .center[ ![:scale 50%](./images/inodes.png) ] - The basic data structure is called an `inode`, uniquely identified by an ID -- - Each `inode` contains information about a specific file -- - A directory is actually a _special file_: it contains a list of filenames and pointers to thier `inode`s .center[ .font-small[Image from http://faculty.salina.k-state.edu/tim/unix_sg/advanced/links.html] ] --- # File in UNIX systems - name (and an optional extension, e.g. `.txt`) - attributes (metadata) - owner (UID) and group (GID) - access rights for _owner_, _group_ and _others_ - time of last attribute change (`ctime`), modification (`mtime`) and access to content (`atime`) - content -- .center[ ![scale: 40%](images/13-ufs.png) .font-small[Image from https://www.cs.rutgers.edu/~pxk/416/notes/13-fs-studies.html] ] --- # File metadata - Not shown by default in `ls` listing - Part of the "long listing format" (i.e. via `ls -l`) ```bash $ ls `-l` /labs/lab06_dist.sh -rwxr-xr-x 1 mrshu mrshu 3402021 Oct 26 15:36 lab06_dist.sh ``` -- - To do the same on a directory, add `-d` flag (`ls` lists directory contents by default) ```bash $ ls `-l` /labs total 20264 -rwxr-xr-x 1 root root 3389123 Sep 21 13:21 lab01_dist.sh -rwxr-xr-x 1 root root 3428779 Sep 28 14:09 lab02_dist.sh -rwxr-xr-x 1 root root 3644753 Oct 5 14:23 lab03_dist.sh $ ls `-l -d` /labs drwxr-xr-x 2 root root 4096 Oct 26 17:01 /labs/ ``` --- # File metadata: File Types .center[![:scale 60%](./images/perms1.png)] **File types**: - `-` - "normal" file - `d` - directory - `c` / `b` - character / block device - `p` - named pipe - `l` - symbolic link - `s` - socket .center[.font-small[Image from https://test-www.ics.uci.edu/computing/linux/file-security.php]] --- # File metadata: Permissions .center[![:scale 60%](./images/perms1.png)] .left-eq-column[ - Permissions are set and evaluated on three levels: 1. owner 2. group 3. others - Each level can have permission to - read (`r`) - write (`w`) - execute (`x`) - Can also be represented as octal (base 8) numbers ] .right-eq-column[ Conversion from `rwxr-xr--` to octal: .center[![:scale 80%](images/permissions.png)] .font-xsmall[Image from https://danielmiessler.com/study/unixlinux_permissions/] ] ??? - Permissions are evaluated in the same order as they are represented in the listing 1. Am I the _owner_? 2. If not, am I part of the _group_? 3. If not, then I am part of _others_. --- # File metadata: Permissions II .center[![:scale 60%](./images/perms1.png)] .left-eq-column[ Permissions can be changed with the `chmod` command: `chmod {u,g,o,a}{+,-,=}{r,w,x} file` - `u`ser (owner), `g`roup, `o`ther and `a`ll - `+` adds, `-` removes and `=` sets - `r`ead, `w`rite and e`x`ecute permissions ```bash $ chmod g+rx file.txt $ chmod a-r file.txt $ chmod o=rw file.txt ``` ] .right-eq-column[ It also works with numerical (octal) representation: - `chmod 750 file` - set the file permissions of `file` to `rwxr-x---` ```bash # (111 111 111) in binary $ chmod 777 file.txt # (111 100 101) in binary $ chmod 745 file.txt ``` ] ??? Maybe also `setUID`, `setGID` and `umask` (in the future)? --- # File metadata: Directory Permissions .center[![:scale 60%](./images/perms1.png)] Permissions work a bit differently on directories: - read (`r`) - allow listing of contents - write (`w`) - create, modify and remove files in the directory - execute (`x`) - ability to **go through** the directory - in other words, to include it in the path --- # File metadata: Owner and Group .center[![:scale 60%](./images/perms1.png)] .left-eq-column[ - New files/folders are created with the owner's UID and their active/primary group's GID - Only `root` can change the owner (via the `chown` command) ```bash # Change the owner of file.txt to jane $ chown jane file.txt # Works on directories as well $ chown jane ~/Downloads ``` ] .right-eq-column[ - Group can be changed by the current group's user's - The user needs to be part of the new group ```bash # Change the group of file.txt to root $ chgrp root file.txt ``` - If we want to change both the owner as well as the group, we can use `chmod` ```bash $ chown jane:users /home/jane ``` ] --- # File metadata: Timestamps .center[![:scale 60%](./images/perms1.png)] - `ls -l` by default shows the `mtime` -- last modified time - The other timestamps can be viewed by the `stat` utility ```bash $ stat /tmp File: /tmp Size: 3800 Blocks: 0 IO Block: 4096 `directory` Device: 23h/35d Inode: 21596 Links: 129 Access: (1777/`drwxrwxrw`t) Uid: ( 0/ root) Gid: ( 0/ root) Context: system_u:object_r:tmp_t:s0 Access: 2020-10-30 13:48:18.249475358 +0100 Modify: 2020-11-02 11:05:48.417238277 +0100 Change: 2020-11-02 11:05:48.417238277 +0100 Birth: - ``` --- # File metadata: Timestamps II .center[![:scale 60%](./images/perms1.png)] .left-eq-column[ The timestamps can be changed with the `touch` utility - `touch` - sets `atime` and `mtime` to the current time (by default) - creates an empty file if it does not exist - `-a`: set `atime` only - `-m`: set `mtime` only - `-t STAMP`: use `STAMP` instead of current date `STAMP` is formatted as `[[CC]YY]MMDDhhmm[.ss]` ] .right-eq-column[ ```bash $ touch newfile.txt $ touch -a newfile.txt # STAMP format: [[CC]YY]MMDDhhmm[.ss] # Everything in [] is optional # # MM == 11 # DD == 02 # hh == 10 # mm == 10 $ touch -m -t 11021010 newfile.txt ``` ] --- class: inverse, middle, center # Hardlinks and Symlinks --- # Links as a concept - Often times we'd like to have a file/directory with the same content available on various parts of the filesystem. .center[![:scale 30%](images/link-diagram.jpg)] -- - It makes little sense to copy the same contents multiple times over (what if it changes?) -- - The solution is called **links** .center[.font-small[Image from https://www.computerhope.com/unix/uln.htm]] --- # Directory as a special file .center[ ![:scale 50%](./images/inodes.png) ] - Notice how the "directory contents" file has entries for both itself (`.`) and its parent directory (`..`) - Since it can contain arbitrary filenames which point to arbitrary `inode` IDs, this can be used for simple implementation of links --- # Hard link .left-eq-column[ - Different filenames (or even paths) link to the same `inode` ID - The attributes as well as contents stay exactly the same - Only possible within a single data device (disk) ] .right-eq-column[ - `inode` ID of a file can be viewed with `ls -i` - `inode` counts the number of links that point to it - When nothing does, the `inode` gets "physically" removed as well ] .clear-both[ ```bash $ echo "This is a file." > file1.txt $ ls -l -i file1.txt 136158 -rw-rw-r-- `1` mrshu mrshu 16 Nov 2 11:20 file1.txt # Create a hardlink called file2.txt from file1.txt $ ln file1.txt file2.txt # The hardlink count has increased $ ls -l -i file2.txt 136158 -rw-rw-r-- `2` mrshu mrshu 16 Nov 2 11:20 file2.txt $ cat file1.txt This is a file. $ cat file2.txt This is a file. ``` ] --- # Symbolic link (symlink) - A "standard shortcut" - A new "text" file gets created; its contents point to the path where the source file is located - The path is evaluated from the position of this new file in the filesystem - When the source file gets removed, the symbolic link continues to live - It is not counted among the hardlinks (e.g. in `ls -l` output) - Can be created across the whole filesystem (not just one data device) .left-eq-column[ .center[ ![:scale 60%](images/link-diagram.jpg) .center[.font-small[Hardlink]] ] ] .right-eq-column[ .center[ ![:scale 60%](images/symlink-diagram.jpg) .center[.font-small[Symlink]] ] ] .center[.font-small[Images from https://www.computerhope.com/unix/uln.htm]] --- # Symbolic link (symlink) ```bash $ echo "This is a file." > file1.txt $ ls -l file1.txt 136158 -rw-rw-r-- `1` mrshu mrshu 16 Nov 2 11:44 file1.txt # Create a symlink called file2.txt pointing to file1.txt $ ln -s file1.txt file2.txt # The hardlink count has not increased $ ls -l -i file2.txt 136159 lrwxrwxrwx `1` mrshu mrshu 9 Nov 2 11:45 `file2.txt` -> file1.txt $ ls -l -i file1.txt 136158 -rw-rw-r-- `1` mrshu mrshu 16 Nov 2 11:44 file1.txt $ cat file1.txt This is a file. $ cat file2.txt This is a file. # Link to the parent directory $ mkdir dir $ cd dir $ ln -s ../file1.txt file3.txt $ ls -l -i file3.txt 393489 lrwxrwxrwx 1 mrshu mrshu 12 Nov 2 11:48 file3.txt -> `../file1.txt` ``` --- class: inverse, middle, center # Useful commands For checking and changing file encoding --- # `file` A useful command for checking the file type (e.g. is it an image, video or a text file). - `file filename` - `-b` simplified output - `-i` output as a mime type ```bash $ file image.jpg image.jpg: JPEG image data, JFIF standard 1.01, resolution (DPI), density 72x72, segment length 16, progressive, precision 8, 317x191, components 3 $ file -bi image.jpg image/jpeg; charset=binary $ file index.html index.html: HTML document, UTF-8 Unicode text, with very long lines $ file -i index.html index.html: text/html; charset=utf-8 ``` --- # `iconv` Convert text form one character encoding to another - `iconv -f [encoding] -t [encoding] -o [outputfile] [inputfile]` - `-f [encoding]`: convert encoding from this charset - `-t [encoding]`: convert encoding to this charset - `-o [outputfile]`: save output to this file (stdout by default) ```bash $ file -bi file_iso.txt text/plain; charset=iso-8859-1 $ iconv -f iso-8859-1 -t utf-8 -o file_utf8.txt file_iso.txt $ file -bi file_utf8.txt text/plain; charset=utf-8 ```