Linux/Unix/macOS Files and Directories: A Beginner's Guide
github.com/kristofer june 2025, v2
Cross-Platform Note: While this guide focuses on Linux/Unix systems, these concepts and commands also apply to macOS, which is built on Unix foundations. macOS uses the same file system hierarchy, permission system, and most of the same commands. Where there are differences, they’ll be noted throughout the guide.
File System Hierarchy
The Linux/Unix/macOS file system is organized in a hierarchical tree structure, starting from the root directory (/
). Unlike Windows with multiple drive letters (C:, D:, etc.), Unix-like systems (including macOS) have a single unified tree where everything branches from the root. This design creates a logical, consistent structure that’s the same across all Unix/Linux/macOS systems. Understanding this hierarchy is essential because it determines where programs, configurations, and data are stored, and knowing these conventions helps you navigate any Unix-based system efficiently.
Key System Directories
Each directory in the file system hierarchy serves a specific purpose, following conventions established decades ago. These aren’t arbitrary choices - they reflect how Unix systems separate different types of files for security, maintenance, and organization. Learning these locations helps you understand where to find configuration files, user data, system programs, and temporary files.
Common Directories (Linux/macOS):
/
: Root directory/home
: User home directories (Linux) //Users
: User home directories (macOS)/etc
: System configuration files/bin
: Essential user commands/sbin
: System administration commands/usr
: User programs and data/var
: Variable data (logs, mail, etc.)/tmp
: Temporary files/opt
: Optional software packages/proc
: Process information (Linux) / Not present in macOS/dev
: Device files/mnt
: Mount point for temporary mounts (Linux) //Volumes
: Mount points (macOS)/media
: Mount point for removable media (Linux)
macOS-Specific Directories:
/Applications
: Installed applications/Library
: System-wide resources and preferences/System
: Core system files (protected by System Integrity Protection)/Users
: User home directories (equivalent to/home
on Linux)
File Types
Unix/Linux/macOS systems treat everything as a file, but there are different types of files that serve different purposes. Regular files contain data or programs, directories organize other files, symbolic links point to other files, and special files represent hardware devices or system communication channels. The first character in the ls -l
output tells you what type of file you’re looking at, which is crucial for understanding what you can do with it.
Platform Note: These file types work identically on Linux and macOS, though macOS has additional file system features like extended attributes and resource forks that may not be visible in standard Unix commands.
1# Regular file (-)
2-rw-r--r-- file.txt
3
4# Directory (d)
5drwxr-xr-x Documents/
6
7# Symbolic link (l)
8lrwxrwxrwx config -> /etc/config
9
10# Character device (c)
11crw-rw-rw- /dev/tty0
12
13# Block device (b)
14brw-rw---- /dev/sda
15
16# Socket (s)
17srwxr-xr-x socket
18
19# Named pipe (p)
20prw-r--r-- pipe
File Permissions
File permissions are Unix/Linux/macOS’s security system, controlling who can read, write, or execute files. This system might seem complex at first, but it’s actually elegant and powerful. Every file has an owner, belongs to a group, and has permissions for “others” (everyone else). Understanding permissions is crucial for security, collaboration, and troubleshooting access problems.
Platform Note: macOS uses the same permission system as Linux, but also includes Access Control Lists (ACLs) and additional security features like System Integrity Protection (SIP) that can override traditional Unix permissions for system files.
Permission Structure
The permission string uses a systematic format that tells you everything about a file’s access rights at a glance. Once you understand this pattern, you can quickly assess file security and determine what actions are allowed.
1-rwxrwxrwx
2│└┬┘└┬┘└┬┘
3│ │ │ └── Others (everyone else)
4│ │ └───── Group
5│ └──────── Owner
6└────────── File type
Permission Types
These three basic permissions combine to control file access. The numbers (4, 2, 1) allow permissions to be expressed mathematically, which is why you see commands like chmod 755
. Each permission has a specific meaning that varies slightly between files and directories.
r
(4): Readw
(2): Writex
(1): Execute-
: No permission
Common Permission Combinations
These numeric combinations represent the most frequently used permission sets. Understanding these patterns helps you quickly set appropriate permissions for different types of files. For example, executable scripts need execute permission, while configuration files typically need only read access for most users.
1# Full permissions (rwx = 7)
2chmod 777 file.txt
3
4# Read and write (rw- = 6)
5chmod 666 file.txt
6
7# Read and execute (r-x = 5)
8chmod 555 file.txt
9
10# Read only (r-- = 4)
11chmod 444 file.txt
Essential File Operations
These operations form the foundation of file management in Unix/Linux/macOS systems. While graphical file managers exist (Finder on macOS, various file managers on Linux), command-line operations are often faster, more precise, and essential for system administration. Many of these commands have options that provide additional functionality, making them incredibly versatile tools.
Platform Compatibility: All the basic file operation commands work identically on Linux and macOS. However, some advanced options or flags might differ between GNU versions (common on Linux) and BSD versions (used on macOS).
Basic Commands
These commands handle the most common file operations you’ll need daily. Each command has numerous options that extend its functionality, but mastering the basic forms will handle most of your file management needs.
1# List files and directories
2ls -la
3
4# Create directory
5mkdir directory_name
6mkdir -p parent/child/grandchild
7
8# Create file
9touch file.txt
10
11# Copy files/directories
12cp source destination
13cp -r source_dir destination_dir
14
15# Move/rename files
16mv old_name new_name
17mv file directory/
18
19# Remove files/directories
20rm file.txt
21rm -r directory
22rm -rf directory # Force remove
23
24# Create symbolic link
25ln -s target_path link_name
File Information
Gathering information about files is essential for understanding what you’re working with, troubleshooting problems, and making informed decisions about file operations. These commands help you examine files without modifying them, which is especially important when dealing with unfamiliar files or system files.
Platform Note: These information commands work the same on Linux and macOS, though macOS may show additional metadata through commands like mdls
(metadata list) that access Spotlight’s database.
1# View file details
2ls -l file.txt
3
4# Show file type
5file filename
6
7# Display file size
8du -sh file_or_directory
9
10# View file contents
11cat file.txt
12less file.txt
13head -n 10 file.txt
14tail -n 10 file.txt
File Ownership
File ownership determines who has ultimate control over a file and is closely tied to the permission system. Every file has an owner (usually the person who created it) and belongs to a group. Ownership changes are common when transferring files between users or when system administration tasks require different access patterns.
Platform Differences: While the ownership concept is identical on Linux and macOS, macOS has additional considerations like wheel group membership for administrative privileges and System Integrity Protection that prevents ownership changes on system files.
Changing Ownership
Changing ownership requires appropriate privileges (usually root access or sudo
) because it affects system security. These commands are essential for system administration, setting up shared directories, and resolving permission conflicts.
macOS Note: On macOS, use sudo
instead of switching to root user, as the root account is disabled by default for security reasons.
1# Change owner
2chown user file.txt
3
4# Change group
5chgrp group file.txt
6
7# Change both owner and group
8chown user:group file.txt
File Search and Location
Finding files in a large file system can be challenging, especially when you don’t remember the exact location or name. The find
command is incredibly powerful and can search based on numerous criteria. Learning to use find effectively can save enormous amounts of time and frustration.
Platform Alternatives: While find
works identically on Linux and macOS, macOS users can also use Spotlight search via the mdfind
command, which can be faster for content-based searches and leverages macOS’s indexing system.
Finding Files
The find
command uses a pattern of specifying where to search, what criteria to match, and what action to take. Each search criterion can be combined with others to create very specific searches. This flexibility makes find one of the most valuable tools for file management.
1# Find by name
2find /path -name "pattern"
3
4# Find by type
5find /path -type f # Regular files
6find /path -type d # Directories
7
8# Find by size
9find /path -size +100M # Files larger than 100MB
10
11# Find by modification time
12find /path -mtime -7 # Modified in last 7 days
Best Practices
- Organize files logically in directories
- Use meaningful file names
- Maintain proper permissions
- Regularly backup important files
- Clean up temporary files
- Be careful with root privileges
Special File Naming Considerations
File naming in Unix/Linux/macOS follows different conventions than some other operating systems. These considerations aren’t just style preferences - they affect how commands work, how files are sorted, and whether your files will be portable across different systems. Following these conventions prevents many common problems.
macOS Considerations: While macOS supports longer filenames and Unicode characters better than traditional Unix systems, following Unix naming conventions ensures compatibility when working with command-line tools or sharing files with Linux systems.
- Case sensitive (
File.txt
≠file.txt
) - Avoid spaces (use underscores or hyphens)
- Hidden files start with a dot (
.config
) - Avoid special characters except
-
,_
, and.
Remember: In Unix/Linux, “everything is a file.” Understanding this concept and the file system structure is crucial for effective system administration and usage.