Shell and files

Files concepts that are relevant to the shell

view on github

Working with files

Table of contents

  1. Linux and files
  2. Files permissions
  3. Example

Linux and files

  • The first thing we need to appreciate with linux is that under the hood, everything is actually a file :
    • A text file is a file.
    • A directory is a file.
    • Your keyboard is a file (one that the system only reads from only).
    • Your monitor is a file (one that the system only writes to).
    • Etc ...
  • In other systems such as Windows, the system uses file extension to determine what type of file it is.
  • In Linux, the system actually ignores the extension and looks inside the file to determine its type.

Files permissions

  • Linux permissions dictate 3 things you may do with a file (read, write and execute).
  • Each permission is represented by a single letter :
    • r read - view the contents of the file.
    • w write - change the contents of the file.
    • x execute - run the file if it is an executable (program or script).
  • For a given file, 3 different sets of users are given permissions :
    • owner - a single user who owns the file. (typically the creator the file but ownership may be granted to someone else).
    • group - every file belongs to a single group of users.
    • others - everyone else who is not the owner nor a member of the group.
  • chmod sets permissions on a file for owner, group, others.
  • Permissions shorthand is a 3 octal digits number that set permissions for owner, group, others.
  • Each digit sets r, w, x permissions for the intended target.
  • For instance, chmod 644 /regular_file sets the permissions for /regular_file to -rw-r--r--.
  • The same series of permissions may be used for directories but behave differently :
    • r - read the contents of the directory (ie perform an ls on it).
    • w - write into the directory (ie create files and directories in it).
    • x - enter that directory (ie cd into it).
  • On a Linux system there are only 2 people usually who may change file or directory permissions :
    1. The owner of the file or directory.
    2. The root user.

Example

  • It is typical for a system to run a webserver and allow users to each have their own web space.
  • A common setup is that if you place a directory in your home directory called public-html then the webserver will read and display its contents.
  • The webserver runs as a different user however so by default it does not have access to get in and read those files.
  • This is a situation where it is necessary to grant read access on your home directory so that the webserver user may access its contents.