Note: UNIX variants have slightly different implementations of various subsystems.
The UNIX filesystem contains several different types of files:
/
) located at the top of the tree./dev
):Three Default I/O Channels:
- Standard Input (
stdin
): Where a program expects to find input- Standard Output (
stdout
): Where a program writes its output- Standard Error (
stderr
): Where a program writes error messages
Interprocess Communication:
- Pipe: One-way data channel that allows two processes to be linked.
- Acts as a temporary file which exists to hold data from one command until it’s read by another.
- Socket: Two-way high-speed data channel that allows two processes on two machines to talk.
Remember: File types aren’t determined by extension on UNIX.
- You can use the
file
command to find a file’s type.
On File Names:
Hidden Files: Files that begin with a dot (.
)
.zshrc
, .mailrc
Reserved Filenames:
/
): Root Directory.
): Current directory..
): Parent directory~
): Home directoryPathnames: Specify where a file is located in the hierarchically organized file system.
/
Two Kinds of Pathnames:
/
)/usr/bin/bash
, /etc/portage/package.use/blender
../../daemon.sh
, ./markdown/06 - UNIX Files.md
Directory Tree: All the files in the UNIX file system are organized into a multi-level hierarchy.
- The UNIX file system can be envisioned as an inverted tree.
- The very top of the file system is root (
/
), all other files are descendants of root.- The number of levels is largely arbitrary, but most systems have some organizational similarities.
Linux File Hierarchy Structure (FHS): Defines the directory structure and contents in UNIX-like operating systems.
/etc/X11
only exists if the X Window System is installed, etc./
: Root/root
is the root user’s home directory, not root itself./bin
: User binariesps
, ls
, ping
, grep
, cp
/sbin
: System binariesiptables
, reboot
, fdisk
, ifconfig
, swapon
/etc
: Configuration files/etc/resolve.conf
, /etc/hostname
/dev
: Device files/dev/tty1
, /dev/input/mouse0
, /dev/nvme0
/proc
: Process information/proc/{pid}
stores information about the process with that particular PID, /proc/uptime
stores information about system uptime/var
: Variable files/var/log
/var/lib
, /var/mail
/tmp
: Temporary files/usr
: User programs/usr/bin
stores binary files for higher-level user programsat
, awk
, cc
, less
, scp
/usr/sbin
stores binary files for system administratorsatd
, cron
, sshd
, useradd
, userdel
/usr/lib
stores libraries for /usr/bin
and /usr/sbin
/usr/local
stores user’s programs installed from source/usr/local/apache2
/home
: Home directories/home/inevitabby
, /home/larry
, /home/tkaczynski
/boot
: Boot loader filesinitramfs-6.1.67-gentoo-dist.img
, vmlinuz-6.1.67-gentoo-dist
/lib
: System libraries/bin
and /sbin
ld*
or lib*.so.*
ld-2.11.1.so
, libncurses.so.5.7
/opt
: Optional add-on apps/mnt
: Mount directory/media
: Removable devices/media/cdrom
, media/floppy
. media/cdrecorder
/srv
: Service data/src/cvs
contains CVS-related data.Remember: The
/usr
variants of folders that can be found under root contain higher-level versions of their counterparts!
- For example,
/usr/bin
contains higher-level system utilities while/bin
contains essential lower-level utilities.
UNIX is a multi-user system.
UNIX systems have one or more users, identified by numeric uid and username.
Command:
id
lets you view your uid, default groups, and which groups your belong to.
Every file/directory has:
Permissions lets us answer questions like:
- Can this file be read or written?
- Can this program be run?
- Can this hardware be used?
- Can this process be stopped?
Permission | For a File | For a Directory |
---|---|---|
read | Contents can be viewed | Contents can be listed, but not searched |
write | Contents can be changed or deleted | File entries can be added or removed |
execute | File can be run as a program | Directory can be searched, and you can cd to it |
Three Types of Permissions:
r
): Process may read contents of file, or list directory contents.w
): Process may write contents of file, or add/remove directory contents.x
): Process may execute file, or open files in directory or subdirectories.Three Sets of Permissions:
u
): Permissions for ownerg
): Permissions for group (1 group per file)o
): Permissions for everyone on the system.Note: Every user is responsible for controlling access to their files and directories.
- Use this power with care!
When using ls -l
(long listing), file permissions are given in the following format—
r
)w
)x
)r
)w
)x
)r
)w
)x
)—which are displayed as: -rwxrwxrwx
- The first character shows file type:
1. File: -
2. Directory: d
3. Link: l
- If a r
, w
, or x
is replaced by a -
, that permission is denied for that respective set.
- Starting after the file type character, the:
* First RWX
triplet are for the user owner,
* Middle RWX
triplet are for the group, and
* Last RWX
triplet are for others.
Examples: Reading
ls -a
-rwxr-xr-x 1 larry larry 15480 Dec 28 23:16 a.out
- A file that everyone has execute and read permissions for, but only the owner has write.
drwxr-xr-x 1 larry larry 0 Dec 28 23:17 directory
- A folder that everyone has execute and read permissions for, but only the owner has write.
-rw-r--r-- 1 larry larry 90 Dec 28 23:16 source.c
- A file that everyone has read permissions for, but only the owner has write.
chmod
: Change file permissionschmod [permission] [filename]
chown
: Change file ownerchown [username] [filename]
chgrp
: Change file groupchgrp [groupname] [filename]
umask
: User file creation mode maskPermission settings use octal numbers:
Permission | Binary | Octal |
---|---|---|
r | 100 | 4 |
w | 010 | 2 |
x | 001 | 1 |
None | 000 | 0 |
r
, w
, and x
is actually an on/off toggle, and the math is pretty simple.Example: Converting permissions to octal equivalents
Permission Binary Octal rwx rwx rwx
111 111 111
7 7 7
rwx r-x r-x
111 101 101
7 5 5
rw- r-- r--
110 100 100
6 4 4
r-- --- ---
100 000 000
4 0 0
- These codes (
777
,755
,644
, and400
) are the most-common codes.
Example: Using
chmod
with symbolic access mode ({u,g,o}/{r,w,x}
) and octal access modeFormat:
chmod [ugoa] [+-=] [rwx] [file/dir]
[ugoa]
: User, group, other, and all, respectively. (Defaults to user)[+-=]
: Add, remove, and set permissions, respectively.[rwx]
: Permission code.[file/dir]
: File or directory to apply it to.$ chmod +x daemon.sh
- Give execute permission to
daemon.sh
for current owner.$ chmod o -r unreadable.txt
- Remove read permission from
unreadable.txt
$ chmod a=r-x file
- Set permissions to
r-x
forunreadable.txt
for all.
- Notice how when using
=
we need to specify all three permission codes.$ chmod go-w unwriteable.txt
- Remove write permission from
unwriteable.txt
for group and other.$ chmod g:cs2600-x unexecutable
- Remove execute permissions from
unexecutable
from the cs2600 group.
- Note: When a specific group isn’t given, your default group is the one that’s modified by chmod.
$ chmod 600 my_file
- Set permissions for owner to be able to read and write, give no permissions to group and other.
$ chmod 755 our_file
- Set permissions for owner to be able to read, write, and execute; give only read and write permissions to group and other.