Chapter 4: Moving Around the File System
The Linux file system stores all information on the computer.
| Directory | Description |
|---|---|
| /bin | Contains common Linux user commands like ls, sort, date, and chmod. |
| /boot | Contains the bootable Linux kernel, initial RAM disk, and boot loader configuration files (GRUB). |
| /dev | Contains files representing system device access points. These include terminals (tty*), hard drives (hd* or sd*), RAM (ram*), and CD-ROMs (cd*). Users can access devices directly via these files, though apps usually hide them. |
| /etc | Contains administrative configuration files. Most are plain text and can be edited with any text editor if you have the right permissions. |
| /home | Contains directories for every user with a login account (except root, which uses /root). |
| /media | A standard location for auto-mounting devices (especially removable media). Usually uses the volume name as the mount point. |
| /lib | Contains shared libraries needed by apps in /bin and /sbin to boot the system. |
| /mnt | Before /media became standard, /mnt was the common mount point for many devices. Some bootable Linux systems still use it for partitions and remote file systems. Many use it for temporary mounts. |
| /misc | Sometimes used for auto-mounting file systems on request. |
| /opt | Directory structure for storing add-on application software. |
| /proc | Contains information about system resources. |
| /root | The home directory for the root user. Kept separate from /home for security reasons. |
| /sbin | Contains administrative commands and daemons. |
| /sys | Contains parameters for tuning block storage and managing cgroups. |
| /tmp | Contains temporary files used by applications. |
| /usr | Contains user docs, games, graphics files (X11), libraries (lib), and other commands/files not needed during boot. Files in /usr are generally static after installation and can technically be mounted as read-only. |
| /var | Contains data directories used by various apps. Includes files for FTP (/var/ftp) or Web (/var/www) servers, system logs (/var/log), and spool files (/var/spool, like mail/cups). Files here change frequently. On servers, /var is often a separate partition. |
4.1 Basic File System Commands
cd, pwd, mkdir, rmdir, ls, touch
4.2 Using Metacharacters and Operators
Certain special characters are known as metacharacters or operators.
4.2.1 File Matching Metacharacters (Wildcards)
| Metacharacter | Description |
|---|---|
| * | Matches any number of characters |
| ? | Matches any single character |
| […] | Matches any character within the brackets; can include ranges of letters or numbers separated by a hyphen. |
Example: ls [a-g]* lists files or directories starting with letters a through g.
4.2.2 File Redirection Metacharacters
| Metacharacter | Description |
|---|---|
| < | Directs file content into a command. This is default behavior and often omitted. Example: less bigfile is same as less < bigfile. |
| > | Directs standard output (stdout) to a file. Overwrites the file if it exists. |
| 2> | Directs standard error (stderr) to a file. |
| &> | Directs both stdout and stderr to a file. |
| » | Appends command output to the end of an existing file. |
Example command:
| |
Formats the man page (man), strips backspaces (col -b), and saves output to /tmp/chmod (overwriting it if it exists).
| |
Appends Hello World! to the file ~/hello.
Another type of redirection is called here text (or here document):
| |
This uses the ed editor to automate adding a DNS server IP to /etc/resolv.conf (usually in a script run by root) using the content between the resendit tags.
4.2.3 Brace Expansion
Using {} allows you to expand a set of characters across filenames, directories, or arguments:
| |
Creates 5 files: memo1 through memo5.
| |
Creates 9 files.
| |
Creates 30 files from a1 to f5.
4.3 Listing Files and Directories
Usually, ls is aliased to ls --color=auto. Check aliases with:
| |
Use ls -la to see detailed info (-l) and hidden files (-a).
| Column | File | Directory |
|---|---|---|
| 1 | Permissions | Permissions |
| 2 | Number of links | Number of links |
| 3 | Owner | Owner |
| 4 | Group | Group |
| 5 | Size in bytes | Size of the directory file itself (not contents) |
| 6 | Last modification date/time | Last modification date/time |
| 7 | Filename | Directory name |
Notes:
- Time/date format depends on the
LANGvariable. - An executable might have
spermissions (-rwsr-sr-x), meaning any user can run it, but the process runs with the ownership of the app owner/group (SetUID/SetGID). - If permissions end in
t(drwxrwxr-t), it’s a “sticky bit”. Users can add files, but cannot delete other users’ files. - SetGID on a directory makes new files inherit the directory’s group. If you see capital
SorTinstead of execution bits, it means SetGID or the sticky bit is set, but the execution bit is off. - A plus sign at the end (
-rw-rw-r-+) indicates extended attributes like ACLs or SELinux. A dot indicates SELinux attributes.
cd ~yexca goes to yexca’s home. cd - returns to the previous working directory (stored in $OLDPWD). cd . refers to the current directory ($PWD).
ls flags: -t sorts by modification time; -F adds / to directories, * to executables, and @ to symlinks; --hide=yexca hides specific files/dirs; -S sorts by size; -d shows info about the directory itself instead of its contents.
4.4 Understanding File Permissions and Ownership
The first column of ls -l:
The first character is the file type:
| Letter | Type |
|---|---|
| - | File |
| d | Directory |
| l | Symbolic link |
| b | Block device |
| c | Character device |
| s | Socket |
| p | Named pipe |
The next nine characters are permissions: first three for user (u), middle three for group (g), last three for others (o). (Acronym: ugo).
Permissions mean slightly different things for files vs. directories:
| Permission | File | Directory |
|---|---|---|
| Read | View file content | List contents of the directory |
| Write | Change content, rename, or delete | Add or delete files/subdirectories within it |
| Execute | Run file as a program | Enter the directory (cd), search it, or access file metadata |
Check any file/dir permissions with ls -ld.
In Fedora and RHEL, new users get their own group with the same name. This is the User Private Group (UPG) scheme.
4.4.1 Using chmod (Numeric) to Change Permissions
r = 4; w = 2; x = 1
| |
Recursively (-R) sets ~/myfile and its contents to 755 (rwxr-xr-x).
4.4.2 Using chmod (Symbolic) to Change Permissions
| |
| |
4.4.3 Using umask for Default Permissions
Normal user defaults: files rw-rw-r--, directories rwxrwxr-x.
Root user defaults: files rw-r--r--, directories rwxr-xr-x.
These are set by umask. Type umask to see the value (e.g., 0002).
The umask value is subtracted from 666 (files) or 777 (dirs). A umask of 002 results in 775 for dirs and 644 for files (execution bits are off by default for files).
- To change umask temporarily:
umask [value]. Example:umask 000creates wide-open files/dirs. - To make it permanent, add the
umaskcommand to your.bashrcfile.
4.4.4 Changing File Ownership
Only root can change ownership.
| |
Changes /etc/file to be owned by user and group. Use -R for recursive changes.
To change just the owner: chown yexca /etc/file.
4.5 Moving, Copying, and Deleting Files
mv, cp, rm, rmdir
mv -i: interactive mode, prevents accidental overwrites. mv -b: creates a backup of existing files before overwriting.
cp -a: Archive mode, preserves timestamps and attributes.
rm -f: Force delete, ignores non-existent files and never prompts. (Be careful with rm -rf /).