Welcome to Part 8 of the LFCS Certification - Phase 1 series! You've mastered navigation with ls, pwd, and cd. Now it's time to learn about the touch command and understand file timestamps - a fundamental concept that every Linux system administrator must know.
๐ฏ What You'll Learn: In this comprehensive guide, you'll master:
- What the
touchcommand does and why it's essential - Creating empty files quickly
- Understanding Linux's three types of timestamps (atime, mtime, ctime)
- How to read timestamps in
ls -loutput - Updating file timestamps without modifying content
- The difference between access time and modification time
- Why timestamps matter for backups, troubleshooting, and security
- Using
touchwith different timestamp options - Viewing all three timestamps with
statcommand - Real-world scenarios where timestamps are critical
- 20+ comprehensive practice labs with solutions
Series: LFCS Certification Preparation - Phase 1 (Post 8 of 52) Previous: Part 7 - Essential Navigation Commands (ls, pwd, cd, whoami) Next: Part 9 - Understanding the passwd Command
What is the touch Command?
The touch command has two main purposes:
- Create empty files if they don't exist
- Update timestamps of existing files without changing their content
At first glance, it seems like a simple command. But understanding touch opens the door to understanding how Linux tracks file changes - critical knowledge for system administration!
Basic touch Syntax
touch [OPTIONS] filename
Creating Empty Files with touch
Your First touch Command
Let's create an empty file:
[centos9@centos ~]$ touch hello
What happened:
- Command:
touch hello - Result: Created an empty file named
hello - If
helloalready existed, touch would just update its timestamp
Verify the file was created:
[centos9@centos ~]$ ls -l hello
-rw-r--r--. 1 centos9 centos9 0 Oct 31 15:24 hello
Notice:
- File size is 0 bytes (it's empty)
- Timestamp shows when the file was created:
Oct 31 15:24 - Permissions are
-rw-r--r--(default permissions)
Creating Multiple Files at Once
You can create multiple files with one command:
[centos9@centos ~]$ touch file1.txt file2.txt file3.txt
[centos9@centos ~]$ ls -l file*.txt
-rw-r--r--. 1 centos9 centos9 0 Oct 31 15:30 file1.txt
-rw-r--r--. 1 centos9 centos9 0 Oct 31 15:30 file2.txt
-rw-r--r--. 1 centos9 centos9 0 Oct 31 15:30 file3.txt
What happened:
- All three files created simultaneously
- All have the same timestamp (created at the same time)
- All are empty (0 bytes)
๐ก Quick Tip: Creating empty files with touch is faster than using text editors for placeholder files, test files, or when you need to create file structures quickly.
Understanding Linux File Timestamps
Linux tracks three different timestamps for every file. Understanding these is crucial for the LFCS exam and real-world system administration.
The Three Types of Timestamps
| Timestamp | Full Name | What It Tracks |
|---|---|---|
| atime | Access Time | Last time file content was read |
| mtime | Modification Time | Last time file content was modified |
| ctime | Change Time | Last time file metadata (permissions, ownership) or content changed |
Detailed Explanation of Each Timestamp
1. atime (Access Time)
- Updated when you read file content
- Examples:
cat file.txt,grep pattern file.txt,less file.txt - NOT updated by
ls(just lists the file, doesn't read content)
2. mtime (Modification Time)
- Updated when you change file content
- Examples: editing a file, appending to a file, overwriting
- This is the timestamp shown in
ls -lby default
3. ctime (Change Time)
- Updated when file metadata changes OR content changes
- Examples: changing permissions (
chmod), changing ownership (chown), editing content - You CANNOT manually set ctime - it's always set by the system
โ ๏ธ Important: ctime is NOT "creation time"! Linux doesn't track creation time in traditional filesystems (ext4). The ctime is "change time" - when the file's inode was last changed.
What ls -l Shows
By default, ls -l shows mtime (modification time):
[centos9@centos ~]$ ls -l hello
-rw-r--r--. 1 centos9 centos9 0 Oct 31 15:24 hello
โโโโโโโโโโโโโโโ
This is mtime
To see other timestamps:
# Show atime (access time)
ls -lu hello
# Show ctime (change time)
ls -lc hello
Updating Timestamps with touch
When you run touch on an existing file, it updates the timestamps without changing the content!
Example: Updating Timestamps
# Create a file and check timestamp
[centos9@centos ~]$ touch hello
[centos9@centos ~]$ ls -l hello
-rw-r--r--. 1 centos9 centos9 0 Oct 31 15:24 hello
# Wait a minute, then touch again
[centos9@centos ~]$ touch hello
[centos9@centos ~]$ ls -l hello
-rw-r--r--. 1 centos9 centos9 0 Oct 31 15:25 hello
What happened:
- First
touch: Created file at15:24 - Second
touch: Updated timestamp to15:25 - File size remained 0 (no content added)
- Only the timestamp changed!
๐ก Real-World Use Case: System administrators use touch to prevent files from being deleted by automated cleanup scripts that remove "old" files. Touching a file makes it appear recently modified.
Using the stat Command
The stat command shows all three timestamps plus detailed file information:
[centos9@centos ~]$ stat hello
File: hello
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: fd00h/64768d Inode: 35234567 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 1000/ centos9) Gid: ( 1000/ centos9)
Access: 2024-10-31 15:24:15.123456789 -0400
Modify: 2024-10-31 15:24:15.123456789 -0400
Change: 2024-10-31 15:24:15.123456789 -0400
Birth: -
What we see:
- Access: atime (last time content was read)
- Modify: mtime (last time content was modified)
- Change: ctime (last time metadata or content changed)
- Birth: Creation time (not tracked on most Linux filesystems)
Watching Timestamps Change
Let's see timestamps change in real-time:
# Create a file
[centos9@centos ~]$ touch testfile.txt
# Check all timestamps
[centos9@centos ~]$ stat testfile.txt
Access: 2024-10-31 16:00:00.000000000 -0400
Modify: 2024-10-31 16:00:00.000000000 -0400
Change: 2024-10-31 16:00:00.000000000 -0400
# Add content (changes mtime and ctime)
[centos9@centos ~]$ echo "Hello World" > testfile.txt
# Check timestamps again
[centos9@centos ~]$ stat testfile.txt
Access: 2024-10-31 16:00:00.000000000 -0400 โ Unchanged
Modify: 2024-10-31 16:01:30.000000000 -0400 โ Updated!
Change: 2024-10-31 16:01:30.000000000 -0400 โ Updated!
# Read the file (changes atime)
[centos9@centos ~]$ cat testfile.txt
Hello World
# Check timestamps again
[centos9@centos ~]$ stat testfile.txt
Access: 2024-10-31 16:02:45.000000000 -0400 โ Updated!
Modify: 2024-10-31 16:01:30.000000000 -0400 โ Unchanged
Change: 2024-10-31 16:01:30.000000000 -0400 โ Unchanged
Advanced touch Options
Setting Specific Timestamps
You can set timestamps to specific dates and times:
# Set timestamp to specific date/time
touch -t 202310151430.00 myfile.txt
# Format: YYYYMMDDhhmm.ss
# Example breakdown:
# 2023 - Year
# 10 - Month (October)
# 15 - Day
# 14 - Hour (2 PM)
# 30 - Minutes
# .00 - Seconds (optional)
Using a Reference File
Copy timestamp from one file to another:
# Make newfile.txt have the same timestamp as oldfile.txt
touch -r oldfile.txt newfile.txt
touch Options Summary
| Option | What It Does | Example |
|---|---|---|
-a | Change only access time (atime) | touch -a file.txt |
-m | Change only modification time (mtime) | touch -m file.txt |
-c | Do not create file if it doesn't exist | touch -c file.txt |
-t | Use specified time instead of current | touch -t 202310151430 file.txt |
-r | Use timestamp from reference file | touch -r ref.txt file.txt |
-d | Use human-readable date string | touch -d "2 days ago" file.txt |
Why Timestamps Matter for System Administration
Understanding timestamps is essential for many sysadmin tasks:
1. Backup Systems
Backup tools use mtime to determine which files need backing up:
- Incremental backups copy only files modified since last backup
- If mtime hasn't changed, file is skipped
2. Troubleshooting
When investigating system issues:
- Check which config files were recently modified
- Find files accessed during a security incident
- Identify when problems started by correlating timestamps
Example: Find recently modified config files
find /etc -name "*.conf" -mtime -1
# Shows .conf files modified in last 24 hours
3. Log Rotation
Log management systems use timestamps to:
- Determine when to rotate logs
- Archive old log files
- Delete logs past retention period
4. Security Auditing
Timestamps help detect suspicious activity:
- Files accessed at unusual times
- Configuration changes during incidents
- Unauthorized file modifications
5. Automated Cleanup
Scripts that delete "old" files rely on timestamps:
# Delete files older than 30 days
find /tmp -type f -mtime +30 -delete
Real-World Timestamp Scenarios
Scenario 1: Preventing Cleanup Script Deletion
Your important file is about to be deleted by a cleanup script that removes files older than 7 days:
# This file is 6 days old and about to be deleted tomorrow
ls -l important-report.txt
-rw-r--r--. 1 centos9 centos9 1024 Oct 24 14:30 important-report.txt
# Touch it to reset the timestamp
touch important-report.txt
# Now it appears to have been modified today!
ls -l important-report.txt
-rw-r--r--. 1 centos9 centos9 1024 Oct 31 10:00 important-report.txt
Scenario 2: Testing Backup Scripts
Verify your backup script correctly identifies modified files:
# Create test files
touch file1.txt file2.txt file3.txt
# Run backup (backs up all three)
# Modify only file2.txt
echo "changed" > file2.txt
# Run incremental backup
# Should only backup file2.txt since it's the only one with changed mtime
Scenario 3: Troubleshooting After System Issues
Find what changed around the time of a system crash:
# System crashed at 3:45 PM on Oct 31
# Find files modified between 3:30 PM and 4:00 PM
find /etc -type f -newermt "2024-10-31 15:30" ! -newermt "2024-10-31 16:00"
๐งช Practice Labs
Let's master touch and timestamps with hands-on practice!
Lab 1: Basic File Creation (Beginner)
Tasks:
- Create an empty file called
test1.txt - Verify it exists and check its size
- Create three files at once:
a.txt,b.txt,c.txt - List all the files you created with timestamps
Click to reveal solution
# Task 1: Create test1.txt
touch test1.txt
# Task 2: Verify and check size
ls -l test1.txt
# Output shows: 0 bytes (empty file)
# Task 3: Create three files at once
touch a.txt b.txt c.txt
# Task 4: List all files with timestamps
ls -l test1.txt a.txt b.txt c.txt
Expected output:
-rw-r--r--. 1 centos9 centos9 0 Oct 31 16:00 a.txt
-rw-r--r--. 1 centos9 centos9 0 Oct 31 16:00 b.txt
-rw-r--r--. 1 centos9 centos9 0 Oct 31 16:00 c.txt
-rw-r--r--. 1 centos9 centos9 0 Oct 31 16:00 test1.txt
Lab 2: Understanding Timestamps (Beginner)
Tasks:
- Create a file named
timestamp-test.txt - Use
statto see all three timestamps - Note the current timestamps
- Wait 30 seconds, then run
touchon the file again - Check timestamps again - what changed?
Click to reveal solution
# Task 1: Create file
touch timestamp-test.txt
# Task 2: View all timestamps
stat timestamp-test.txt
# Task 3: Note the timestamps
# All three should be identical (just created)
# Task 4: Wait and touch again
sleep 30 # Wait 30 seconds
touch timestamp-test.txt
# Task 5: Check timestamps again
stat timestamp-test.txt
What changed:
- Access time (atime): Updated to current time
- Modify time (mtime): Updated to current time
- Change time (ctime): Updated to current time
- All three timestamps are now 30 seconds later than before
Lab 3: Reading vs Modifying Files (Intermediate)
Tasks:
- Create a file
content-test.txtwith some content - Check all timestamps with
stat - Read the file with
cat(don't modify it) - Check timestamps again - what changed?
- Now modify the file by adding more content
- Check timestamps again - what changed this time?
Click to reveal solution
# Task 1: Create file with content
echo "Initial content" > content-test.txt
# Task 2: Check timestamps
stat content-test.txt
# Note: All three timestamps are identical
# Task 3: Read the file
cat content-test.txt
# Task 4: Check timestamps
stat content-test.txt
# atime (Access) should be updated
# mtime (Modify) and ctime (Change) should be unchanged
# Task 5: Modify the file
echo "More content" >> content-test.txt
# Task 6: Check timestamps
stat content-test.txt
# mtime (Modify) updated - content changed
# ctime (Change) updated - file changed
# atime (Access) may or may not update (depends on filesystem settings)
Key Learning:
- Reading a file: Updates atime only
- Modifying content: Updates mtime and ctime
- Changing permissions: Updates ctime only
Lab 4: Using ls with Different Timestamp Options (Intermediate)
Tasks:
- Create a file and add content to it
- Read the file (to update atime)
- Use
ls -lto see the default timestamp - Use
ls -luto see access time - Use
ls -lcto see change time - Compare the three outputs
Click to reveal solution
# Task 1: Create file with content
echo "Hello World" > myfile.txt
# Task 2: Read the file
cat myfile.txt
# Task 3: Default ls -l (shows mtime)
ls -l myfile.txt
# Task 4: Show access time (atime)
ls -lu myfile.txt
# Task 5: Show change time (ctime)
ls -lc myfile.txt
# Task 6: Compare outputs
echo "=== Modification time (mtime) ==="
ls -l myfile.txt
echo "=== Access time (atime) ==="
ls -lu myfile.txt
echo "=== Change time (ctime) ==="
ls -lc myfile.txt
What you'll notice:
ls -lshows when file was last modified (content changed)ls -lushows when file was last accessed (read)ls -lcshows when file metadata/content last changed
Lab 5: Preventing File Creation (Intermediate)
Tasks:
- Try to touch a non-existent file normally - it creates the file
- Delete that file
- Use
touch -con a non-existent file - it should NOT create the file - Create a file, then use
touch -con it - timestamps should update - Explain when
-coption is useful
Click to reveal solution
# Task 1: Normal touch creates file
touch newfile.txt
ls -l newfile.txt
# File exists!
# Task 2: Delete the file
rm newfile.txt
# Task 3: touch -c does NOT create file
touch -c nonexistent.txt
ls -l nonexistent.txt
# Output: ls: cannot access 'nonexistent.txt': No such file or directory
# Task 4: touch -c updates existing file
touch test-existing.txt # Create it first
ls -l test-existing.txt
sleep 2
touch -c test-existing.txt # Update timestamp
ls -l test-existing.txt
# Timestamp updated, file exists
# Task 5: When is -c useful?
echo "The -c option is useful when:"
echo "1. You only want to update timestamps of existing files"
echo "2. You don't want to accidentally create files"
echo "3. In scripts where file should already exist"
Real-world use: In scripts where you want to update timestamps only if the file exists, but not create it accidentally.
Lab 6: Setting Specific Timestamps (Advanced)
Tasks:
- Create a file and set its timestamp to January 1, 2024 at 10:00 AM
- Verify the timestamp with
ls -l - Create another file with timestamp 7 days ago
- Use
findto locate files modified in the last 10 days - Verify both files are in the results
Click to reveal solution
# Task 1: Set specific timestamp (Jan 1, 2024 10:00 AM)
touch -t 202401011000 oldfile.txt
# Task 2: Verify
ls -l oldfile.txt
# Should show: Jan 1 10:00 oldfile.txt
# Task 3: Set timestamp to 7 days ago
touch -d "7 days ago" recent.txt
# Or use:
touch -d "2024-10-24" recent.txt
# Task 4: Find files modified in last 10 days
find . -name "*.txt" -mtime -10
# Task 5: Verify
# recent.txt should appear (7 days old)
# oldfile.txt should NOT appear (months old)
Explanation:
-tformat:YYYYMMDDhhmm-daccepts human-readable dates: "7 days ago", "yesterday", "2024-10-31"find -mtime -10means "modified within last 10 days"
Lab 7: Using Reference Files (Advanced)
Tasks:
- Create two files:
original.txtandcopy.txt - Put different timestamps on them (wait between creations)
- Make
copy.txthave the same timestamp asoriginal.txt - Verify both files now have identical timestamps
- Explain when this technique is useful
Click to reveal solution
# Task 1: Create first file
touch original.txt
echo "original" > original.txt
# Task 2: Wait and create second file
sleep 3
touch copy.txt
echo "copy" > copy.txt
# Check timestamps - they're different
ls -l original.txt copy.txt
# Task 3: Make copy.txt match original.txt timestamp
touch -r original.txt copy.txt
# Task 4: Verify timestamps are identical
ls -l original.txt copy.txt
# Timestamps should now match!
# Detailed verification
stat original.txt | grep Modify
stat copy.txt | grep Modify
# Task 5: When is this useful?
echo "Using -r (reference file) is useful when:"
echo "1. Synchronizing timestamps between files"
echo "2. Restoring original timestamps after operations"
echo "3. Making copied files appear to be same age as originals"
echo "4. Testing backup systems that rely on timestamps"
Lab 8: Timestamp Detective Work (Advanced)
Scenario: You suspect someone modified a configuration file. Investigate!
Tasks:
- Create a "config file" with specific old timestamp
- "Modify" it by changing its content
- Use different timestamp views to identify:
- When was it last modified?
- When was it last accessed?
- When did metadata last change?
- Explain what each timestamp tells you
Click to reveal solution
# Task 1: Create config file dated 30 days ago
touch -d "30 days ago" config.conf
echo "original_setting=true" > config.conf
# Reset timestamp after writing content
touch -d "30 days ago" config.conf
# Task 2: "Modify" it today (simulate someone changing it)
echo "original_setting=false" >> config.conf
# Task 3: Investigate with different views
echo "=== Investigation Results ==="
echo "Modification time (when content changed):"
ls -l config.conf
stat config.conf | grep Modify
echo "Access time (when last read):"
ls -lu config.conf
stat config.conf | grep Access
echo "Change time (when file/metadata changed):"
ls -lc config.conf
stat config.conf | grep Change
# Task 4: Interpretation
echo ""
echo "=== What the timestamps tell us ==="
echo "mtime: Shows TODAY - file content was modified recently!"
echo "ctime: Shows TODAY - file changed recently (content or metadata)"
echo "atime: May show today or recently (when file was read)"
echo ""
echo "Conclusion: File was modified today, even though it was created 30 days ago"
Key Learning: You can't hide modifications! Even if someone tries to reset timestamps with touch, the ctime always reveals recent changes (you can't manually set ctime).
Lab 9: Finding Recently Modified Files (Expert)
Tasks:
- Create a directory structure with multiple files
- Set different timestamps on different files (some old, some recent)
- Find all files modified in the last 7 days
- Find all files modified MORE than 30 days ago
- Find files modified between 5 and 10 days ago
Click to reveal solution
# Task 1: Create directory structure
mkdir -p testdir/{old,recent,mixed}
cd testdir
# Task 2: Create files with different ages
# Very old files (90 days ago)
touch -d "90 days ago" old/ancient1.txt old/ancient2.txt
# Recent files (3 days ago)
touch -d "3 days ago" recent/new1.txt recent/new2.txt
# Mixed ages
touch -d "60 days ago" mixed/mid1.txt
touch -d "8 days ago" mixed/mid2.txt
touch -d "yesterday" mixed/mid3.txt
# Task 3: Find files modified in last 7 days
echo "=== Files modified in last 7 days ==="
find . -type f -mtime -7
# Should show: new1.txt, new2.txt, mid2.txt, mid3.txt
# Task 4: Find files modified MORE than 30 days ago
echo "=== Files older than 30 days ==="
find . -type f -mtime +30
# Should show: ancient1.txt, ancient2.txt, mid1.txt
# Task 5: Find files modified between 5 and 10 days ago
echo "=== Files modified between 5-10 days ago ==="
find . -type f -mtime +5 -mtime -10
# Should show: mid2.txt (8 days old)
# Cleanup
cd ..
Explanation of find -mtime:
-mtime -7= modified within last 7 days (less than 7 days old)-mtime +30= modified more than 30 days ago-mtime +5 -mtime -10= modified between 5 and 10 days ago-mtime 7= modified exactly 7 days ago (rarely used)
Lab 10: Real-World Backup Simulation (Expert)
Scenario: Simulate an incremental backup system that only backs up modified files.
Tasks:
- Create a "source" directory with several files
- Create a "backup" directory
- Run "full backup" (copy all files, preserve timestamps)
- Modify some source files
- Run "incremental backup" (only copy files newer than backup)
- Verify only modified files were copied
Click to reveal solution
# Task 1: Create source files
mkdir -p backup-lab/source backup-lab/backup
cd backup-lab/source
echo "File 1 content" > file1.txt
echo "File 2 content" > file2.txt
echo "File 3 content" > file3.txt
echo "File 4 content" > file4.txt
# Task 2: Backup directory already created above
# Task 3: Full backup (copy all, preserve timestamps)
echo "=== Running FULL backup ==="
cp -p *.txt ../backup/
ls -l ../backup/
# All 4 files copied
# Create a marker file to track when backup ran
touch ../backup/.last-backup
# Task 4: Simulate work - modify some files
sleep 2
echo "Modified content" >> file2.txt
echo "Also modified" >> file4.txt
# Task 5: Incremental backup - only newer than last backup
echo "=== Running INCREMENTAL backup ==="
find . -name "*.txt" -newer ../backup/.last-backup -exec cp -p {} ../backup/ \;
# Or more explicitly:
# for file in *.txt; do
# if [ "$file" -nt "../backup/$file" ]; then
# cp -p "$file" ../backup/
# echo "Backed up: $file"
# fi
# done
# Task 6: Verify
echo "=== Verification ==="
echo "Source files modified times:"
ls -lt *.txt
echo "Backup files modified times:"
ls -lt ../backup/*.txt
echo "Files modified after last backup:"
find . -name "*.txt" -newer ../backup/.last-backup
# Should only show file2.txt and file4.txt
# Update backup marker
touch ../backup/.last-backup
# Cleanup
cd ../..
What you learned:
- Incremental backups rely on mtime comparisons
-newerfinds files modified after a reference file- Preserving timestamps (
cp -p) is crucial for backup systems - Real backup tools use this same concept!
๐ Best Practices
Using touch Command
-
Create placeholders for scripts
# Script expects these files to exist touch /var/log/myapp.log touch /tmp/lockfile -
Reset timestamps to prevent deletion
# Keep important files from being auto-deleted touch important-file.txt -
Don't create files unnecessarily
# Use -c when you only want to update existing files touch -c /var/log/*.log # Won't create new logs -
Preserve timestamps when copying
cp -p original.txt copy.txt # Preserves timestamps
Understanding Timestamps
-
Check mtime before making changes
ls -l /etc/passwd # Note the timestamp # Make your changes ls -l /etc/passwd # Verify it changed -
Use stat for detailed timestamp info
stat filename # Shows all three timestamps -
Sort by timestamp for troubleshooting
ls -lt /var/log # Newest logs first ls -ltr /etc # Oldest files first -
Be aware of filesystem mount options
- Some systems mount with
noatimeto improve performance - This prevents atime updates on file reads
- Check with:
mount | grep noatime
- Some systems mount with
For System Administration
-
Document timestamp-sensitive operations
# Before modifying configs ls -l /etc/nginx/nginx.conf >> change-log.txt -
Use find with timestamps for auditing
# Find suspicious modifications find /etc -mtime -1 -type f -
Understand backup implications
- Know which timestamp your backup system uses
- Test backup/restore with timestamp verification
-
Security consideration
- Attackers can modify mtime and atime with
touch - But they CANNOT modify ctime manually
- Check ctime for evidence of tampering
- Attackers can modify mtime and atime with
๐จ Common Pitfalls to Avoid
Pitfall 1: Confusing ctime with "Creation Time"
# WRONG understanding:
# "ctime is when the file was created"
# CORRECT understanding:
# "ctime is CHANGE time - when file metadata or content last changed"
# Linux doesn't track creation time in traditional filesystems!
โ ๏ธ Important: ctime is change time, NOT creation time! It updates whenever file content or metadata changes.
Pitfall 2: Assuming touch Always Creates Files
# This won't create a file if it doesn't exist
touch -c nonexistent.txt
# No error, but file won't be created!
# Use touch without -c to create files
Pitfall 3: Forgetting touch Updates All Timestamps
# Running touch updates atime, mtime, AND ctime
touch myfile.txt
# If you only want to update specific timestamps:
touch -a myfile.txt # Only atime
touch -m myfile.txt # Only mtime
Pitfall 4: Not Preserving Timestamps When Copying
# BAD - loses original timestamps:
cp file1.txt file2.txt
# GOOD - preserves timestamps:
cp -p file1.txt file2.txt
Pitfall 5: Relying on atime in Modern Systems
# Many systems mount with 'noatime' or 'relatime'
# atime may not update as expected!
# Check your mount options:
mount | grep -E "noatime|relatime"
Pitfall 6: Thinking You Can Hide Changes
# Someone modifies a file
echo "hacked" >> /etc/passwd
# Then tries to hide it by resetting timestamp
touch -t 202301010000 /etc/passwd
# But ctime STILL shows recent change!
stat /etc/passwd
# Change time will show TODAY - can't be hidden!
๐ Command Cheat Sheet
touch Command
# Basic usage
touch file.txt # Create empty file or update timestamps
touch file1 file2 file3 # Create/update multiple files
# Create with specific timestamp
touch -t YYYYMMDDhhmm file.txt # Specific date/time
touch -t 202401011200 file.txt # Jan 1, 2024 12:00 PM
touch -d "yesterday" file.txt # Human-readable date
touch -d "7 days ago" file.txt # Relative date
touch -d "2024-10-31 15:00" file.txt # Specific date and time
# Update specific timestamps
touch -a file.txt # Update only access time (atime)
touch -m file.txt # Update only modification time (mtime)
# Options
touch -c file.txt # Don't create if doesn't exist
touch -r ref.txt file.txt # Copy timestamp from ref.txt
# Create with specific permissions (requires combining with chmod)
touch newfile && chmod 600 newfile
Viewing Timestamps
# Using ls
ls -l file.txt # Show mtime (default)
ls -lu file.txt # Show atime (access time)
ls -lc file.txt # Show ctime (change time)
ls -lt # Sort by mtime, newest first
ls -ltu # Sort by atime, newest first
ls -ltc # Sort by ctime, newest first
# Using stat (shows all timestamps)
stat file.txt # Detailed file information
stat -c %y file.txt # Show only mtime
stat -c %x file.txt # Show only atime
stat -c %z file.txt # Show only ctime
Finding Files by Timestamp
# Using find
find . -mtime -7 # Modified within last 7 days
find . -mtime +30 # Modified more than 30 days ago
find . -mtime -10 -mtime +5 # Modified between 5 and 10 days ago
find . -atime -1 # Accessed within last 24 hours
find . -ctime -1 # Changed within last 24 hours
find . -newer file.txt # Modified after file.txt
find . -anewer file.txt # Accessed after file.txt
# More precise (minutes instead of days)
find . -mmin -60 # Modified in last 60 minutes
find . -mmin +120 # Modified more than 120 minutes ago
๐ฏ Key Takeaways
-
touch has two purposes:
- Create empty files
- Update file timestamps without changing content
-
Linux tracks three timestamps:
- atime: Last access (reading file content)
- mtime: Last modification (changing file content)
- ctime: Last change (content OR metadata like permissions)
-
ls -l shows mtime by default:
- Use
ls -lufor atime - Use
ls -lcfor ctime - Use
statto see all three
- Use
-
ctime is NOT creation time - it's change time and can't be manually set
-
Timestamps are crucial for:
- Backup systems (incremental backups)
- Troubleshooting (what changed when?)
- Security auditing (detecting unauthorized changes)
- Automated cleanup (deleting old files)
-
touch can set specific timestamps with
-tor-doptions -
You can't hide changes - even if you reset timestamps, ctime reveals recent modifications
-
Always preserve timestamps when copying important files (
cp -p)
๐ What's Next?
You've mastered the touch command and understand file timestamps! These concepts are foundational for file management, backups, and system administration. In the next post, we'll learn about the passwd command - how to manage user passwords and understand password security in Linux.
Coming up in Part 9: Understanding the passwd Command
- Changing user passwords
- Password complexity requirements
- Understanding password prompts and warnings
- When regular users need sudo for passwd
- Password security best practices
- And much more!
๐ Congratulations! You've completed Part 8 of the LFCS Certification series. You now understand file timestamps and can use the touch command effectively. This knowledge is essential for backup systems, troubleshooting, and understanding how Linux tracks file changes.
Practice Exercise: Create a test directory, create files with different timestamps, and practice finding them with find command. Use stat to examine their timestamps. The more you practice, the more intuitive timestamps become!

