Lesson 2: File Operations
Goal: Learn to create, copy, move, rename, delete files and directories, and work with links.
Table of Contents
- Create Files — touch
- File Timestamps — stat
- Create Directories — mkdir
- Copy — cp
- Move & Rename — mv
- Delete — rm & rmdir
- Links — ln
- Secure Delete — shred
- Exercises
Create Files — touch
touch creates an empty file or updates the timestamp of an existing file.
Create a new file
touch myfile.txt
Create multiple files at once
touch file1.txt file2.txt file3.txt
Create files with patterns
# Create report-01.txt through report-05.txt
touch report-{01..05}.txt
# Create files with different extensions
touch readme.{md,txt,html}
Verify the files were created
ls -la myfile.txt
Output:
-rw-r--r-- 1 benjamin benjamin 0 Mar 22 10:00 myfile.txt
Notice the size is 0 — the file is empty.
File Timestamps — stat
Every file in Linux has three timestamps. stat shows them all.
stat myfile.txt
Output:
File: myfile.txt
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Access: 2026-03-22 10:00:00.000000000 +0100
Modify: 2026-03-22 10:00:00.000000000 +0100
Change: 2026-03-22 10:00:00.000000000 +0100
Birth: 2026-03-22 10:00:00.000000000 +0100
| Timestamp | Meaning | Updated when... |
|---|---|---|
| Access (atime) | Last read | File was read or opened |
| Modify (mtime) | Last content change | File content was modified |
| Change (ctime) | Last metadata change | Permissions, owner, or name changed |
| Birth | Creation time | File was first created |
Modify timestamps with touch
# Update access and modify time to now
touch myfile.txt
# Set a specific timestamp
touch -t 202501151200 myfile.txt
# Format: YYYYMMDDhhmm → Jan 15, 2025 at 12:00
# Copy timestamp from another file
touch -r reference.txt myfile.txt
# Change only the access time
touch -a myfile.txt
# Change only the modification time
touch -m myfile.txt
Create Directories — mkdir
Create a single directory
mkdir projects
Create nested directories
Without -p, this fails if projects doesn't exist:
# This fails:
mkdir projects/web/frontend
# This works — creates all parent directories:
mkdir -p projects/web/frontend
Create multiple directories at once
mkdir -p project/{src,tests,docs,config}
Result:
project/
├── config/
├── docs/
├── src/
└── tests/
Complex nested structure in one command
mkdir -p myapp/{src/{components,utils,styles},tests,public/{images,fonts},docs}
Result:
myapp/
├── docs/
├── public/
│ ├── fonts/
│ └── images/
├── src/
│ ├── components/
│ ├── styles/
│ └── utils/
└── tests/
Copy — cp
Copy a file
cp original.txt copy.txt
Copy a file to another directory
cp myfile.txt /home/benjamin/Documents/
Copy and rename
cp myfile.txt /home/benjamin/Documents/renamed.txt
Copy a directory (requires -r)
# Without -r, this fails for directories
cp -r projects/ projects-backup/
Useful flags
# Interactive — ask before overwriting
cp -i original.txt copy.txt
# Verbose — show what's being copied
cp -v original.txt copy.txt
# Output: 'original.txt' -> 'copy.txt'
# Preserve permissions, timestamps, and ownership
cp -p original.txt copy.txt
# Combine: recursive, verbose, preserve attributes
cp -rvp projects/ projects-backup/
Copy multiple files to a directory
cp file1.txt file2.txt file3.txt /home/benjamin/Documents/
# Using wildcards
cp *.txt /home/benjamin/Documents/
cp *.{jpg,png} /home/benjamin/Pictures/
Move & Rename — mv
mv does two things: move files and rename them.
Rename a file
mv oldname.txt newname.txt
Move a file to another directory
mv myfile.txt /home/benjamin/Documents/
Move and rename simultaneously
mv myfile.txt /home/benjamin/Documents/renamed.txt
Move multiple files
mv file1.txt file2.txt file3.txt /home/benjamin/Documents/
mv *.log /var/log/archive/
Move a directory
# No -r needed (unlike cp)
mv projects/ /home/benjamin/Documents/
Useful flags
# Interactive — ask before overwriting
mv -i oldfile.txt newfile.txt
# Verbose — show what's being moved
mv -v myfile.txt /tmp/
# Output: renamed 'myfile.txt' -> '/tmp/myfile.txt'
# Don't overwrite existing files
mv -n source.txt destination.txt
Delete — rm & rmdir
Delete a file
rm myfile.txt
Delete with confirmation
rm -i myfile.txt
# Prompt: rm: remove regular file 'myfile.txt'? y
Delete multiple files
rm file1.txt file2.txt file3.txt
rm *.tmp
rm *.log
Delete an empty directory
rmdir empty-folder/
Delete a directory and all its contents
rm -r projects/
Force delete (no confirmation, no errors for missing files)
rm -rf old-project/
rm -rf is extremely dangerous. There is no recycle bin in the terminal. Deleted files are gone forever. Always double-check the path before pressing Enter.
Safe practice: use verbose mode
rm -rv old-project/
This shows every file being deleted, so you can see what's happening.
Links — ln
Linux has two types of links: symbolic links (symlinks) and hard links.
Symbolic links (soft links)
A symlink is a shortcut — a pointer to another file or directory.
# Create a symbolic link
ln -s /path/to/original /path/to/link
# Example: create a shortcut to a config file
ln -s /etc/nginx/nginx.conf ~/nginx-config
# Example: create a shortcut to a directory
ln -s /var/log ~/logs
Verify with ls -l:
ls -l ~/nginx-config
# Output: lrwxrwxrwx 1 benjamin benjamin 23 Mar 22 10:00 nginx-config -> /etc/nginx/nginx.conf
The l at the start means "link", and the -> shows what it points to.
Hard links
A hard link is a second name for the same file data on disk.
ln original.txt hardlink.txt
Key differences:
| Feature | Symbolic Link | Hard Link |
|---|---|---|
| Can link to directories | Yes | No |
| Can cross filesystems | Yes | No |
| Works if original is deleted | No (becomes broken) | Yes (data persists) |
Shows as link in ls -l |
Yes (l prefix) |
No (looks like regular file) |
Check link count
stat original.txt | grep Links
# Output: Links: 2 (if one hard link exists)
Remove a link
rm ~/nginx-config # removes the symlink, not the original
unlink ~/nginx-config # alternative way
Secure Delete — shred
Regular rm doesn't actually erase data — it just removes the reference. shred overwrites the file data before deleting.
# Overwrite and delete a file
shred -vuz secret.txt
Flags:
-v— verbose (show progress)-u— delete the file after overwriting-z— add a final overwrite with zeros (hide shredding)-n 5— overwrite 5 times (default is 3)
# Overwrite 10 times, then delete
shred -vuzn 10 confidential.pdf
shred may not work reliably on SSDs or journaling filesystems (ext4, btrfs) due to wear leveling and journaling.
Exercises
Exercise 1: File creation and inspection
# 1. Create a project directory structure
mkdir -p myproject/{src,tests,docs}
# 2. Create some files
touch myproject/src/{main.py,utils.py,config.py}
touch myproject/tests/test_main.py
touch myproject/docs/README.md
# 3. Verify the structure
tree myproject/
# 4. Check the timestamps of main.py
stat myproject/src/main.py
Exercise 2: Copy and move
# 1. Copy the entire project as a backup
cp -rv myproject/ myproject-backup/
# 2. Rename a file
mv myproject/src/config.py myproject/src/settings.py
# 3. Move docs into src
mv myproject/docs/ myproject/src/
# 4. Verify
tree myproject/
Exercise 3: Links
# 1. Create a file with content
echo "Hello World" > original.txt
# 2. Create a symbolic link
ln -s original.txt symlink.txt
# 3. Create a hard link
ln original.txt hardlink.txt
# 4. Read through both links
cat symlink.txt
cat hardlink.txt
# 5. Delete the original and test both links
rm original.txt
cat hardlink.txt # still works!
cat symlink.txt # broken link — error!
Key Takeaways
touch— create files or update timestampsmkdir -p— create nested directory structurescp -r— copy files and directoriesmv— move and rename (same command)rm -r— delete directories (use with caution!)ln -s— create symbolic links (shortcuts)shred— securely erase sensitive files
Next Lesson: Lesson 3: Viewing & Reading Files →