file-operations
Scannednpx machina-cli add skill chaterm/terminal-skills/file-operations --openclawFile and Directory Operations
Overview
Linux file system operation skills, including file search, batch operations, permission management, etc.
File Search
find Command
# Search by name
find /path -name "*.log"
find /path -iname "*.LOG" # Case insensitive
# Search by type
find /path -type f # Files
find /path -type d # Directories
find /path -type l # Symbolic links
# Search by time
find /path -mtime -7 # Modified within 7 days
find /path -mtime +30 # Modified more than 30 days ago
find /path -mmin -60 # Modified within 60 minutes
# Search by size
find /path -size +100M # Larger than 100MB
find /path -size -1k # Smaller than 1KB
# Combined conditions
find /path -name "*.log" -mtime +7 -size +10M
locate Command
# Quick search (requires database update)
locate filename
updatedb # Update database
# Case insensitive
locate -i filename
File Operations
Basic Operations
# Copy
cp file1 file2
cp -r dir1 dir2 # Recursive copy directory
cp -p file1 file2 # Preserve attributes
# Move/Rename
mv file1 file2
mv file1 /path/to/dest/
# Delete
rm file
rm -rf dir # Recursive force delete
rm -i file # Interactive confirmation
# Create
touch file # Create empty file
mkdir -p dir1/dir2/dir3 # Recursive create directories
Batch Operations
# Batch rename
rename 's/old/new/' *.txt
for f in *.txt; do mv "$f" "${f%.txt}.md"; done
# Batch delete
find /path -name "*.tmp" -delete
find /path -name "*.log" -mtime +30 -exec rm {} \;
# Batch copy
find /src -name "*.conf" -exec cp {} /dest/ \;
File Content
View Files
cat file # Full content
head -n 20 file # First 20 lines
tail -n 20 file # Last 20 lines
tail -f file # Real-time follow
less file # Paginated view
# Statistics
wc -l file # Line count
wc -w file # Word count
wc -c file # Byte count
File Comparison
diff file1 file2
diff -u file1 file2 # Unified format
diff -r dir1 dir2 # Compare directories
# Side-by-side comparison
sdiff file1 file2
vimdiff file1 file2
Permission Management
View Permissions
ls -la
stat file
Modify Permissions
# Numeric mode
chmod 755 file # rwxr-xr-x
chmod 644 file # rw-r--r--
# Symbolic mode
chmod u+x file # Add execute for user
chmod g-w file # Remove write for group
chmod o=r file # Set read-only for others
chmod a+r file # Add read for all
# Recursive modify
chmod -R 755 dir
Modify Owner
chown user file
chown user:group file
chown -R user:group dir # Recursive modify
Common Scenarios
Scenario 1: Clean Up Large Files
# Find files larger than 100MB
find / -type f -size +100M -exec ls -lh {} \; 2>/dev/null
# Find and sort by size
du -ah /path | sort -rh | head -20
Scenario 2: Find Recently Modified Files
# Files modified within 24 hours
find /path -type f -mtime -1
# Sort by modification time
ls -lt /path | head -20
Scenario 3: Batch Replace File Content
# Single file replacement
sed -i 's/old/new/g' file
# Batch replacement
find /path -name "*.conf" -exec sed -i 's/old/new/g' {} \;
Troubleshooting
| Problem | Solution |
|---|---|
| Permission denied | Use sudo or check file permissions |
| Disk space full | df -h, du -sh * to find large files |
| Special characters in filename | Use quotes or escape rm "file name" |
| Slow deletion of many files | Use rsync --delete or find -delete |
Source
git clone https://github.com/chaterm/terminal-skills/blob/main/linux/file-operations/SKILL.mdView on GitHub Overview
file-operations provides concrete, shell-based patterns for common Linux filesystem tasks. You can locate files with find and locate, manipulate files and directories with cp, mv, rm, touch, and mkdir, inspect or compare contents with cat, diff, and sed, and manage permissions and ownership with chmod and chown.
How This Skill Works
Each capability is illustrated with ready-to-run command blocks you can adapt to your project paths. The skill emphasizes safe, idempotent workflows (for example, using -exec with {} \; and cautious deletion patterns), and demonstrates batch operations via find -exec and -delete, as well as integrating other tools (du, ls, sed, diff) for concrete tasks.
When to Use It
- You need to locate files by name, type, modification time, or size (e.g., find /path -name "*.log", find /path -mtime -7).
- You want to batch rename, copy, move, or delete a set of files across directories (e.g., find /path -name "*.tmp" -delete, for batch copy use -exec cp {}).
- You must adjust permissions or ownership recursively for a project (e.g., chmod -R 755 dir, chown -R user:group dir).
- You want to view, compare, or modify file content efficiently (e.g., cat, head/tail, diff, sed -i across multiple files).
- You are cleaning up space or auditing recent activity (e.g., find / -type f -size +100M -exec ls -lh {} \; and du -ah /path | sort -rh | head -20).
Quick Start
- Open a terminal and navigate to your project or target directory.
- Use find to locate targets (e.g., find /path -type f -name "*.log").
- Choose an operation (copy, move, delete, permissions) and apply it with a safe pattern (e.g., find /path -name "*.log" -mtime +30 -delete).
- Verify results with ls -la, stat, or grep-like checks before finalizing.
Best Practices
- Test destructive operations with a dry run: replace actions like -delete or rm with -print or echo first to confirm targets.
- Limit scope to avoid accidental wide changes: use -type f/-type d, -maxdepth, or -mindepth to constrain searches.
- Prefer explicit, safe prompts for sensitive actions: use -i with rm or -ok with -exec to require confirmation.
- Capture outcomes for auditing: redirect output to a log file (command >log 2>&1) and review it after execution.
- Use clear, scoped patterns for batch operations (e.g., only .conf files, only under /var/www) to minimize risk.
Example Use Cases
- Scenario: Clean Up Large Files ``` find / -type f -size +100M -exec ls -lh {} \; 2>/dev/null ``` Use this to identify large files you may want to archive or delete after review.
- Scenario: Find Recently Modified Files ``` find /path -type f -mtime -1 ``` Identify files touched in the last 24 hours, then inspect with ls -lt /path.
- Scenario: Batch Replace File Content ``` sed -i 's/old/new/g' /path/**/*.conf ``` Or across many files: ``` find /path -name "*.conf" -exec sed -i 's/old/new/g' {} \; ```
- Scenario: Batch Rename Files ``` for f in *.txt; do mv "$f" "${f%.txt}.md"; done ``` Convert a batch of text files to Markdown extensions in the current directory.
- Scenario: Adjust Permissions for Web Content ``` chmod -R 755 /var/www # directories and files readable/executable by everyone as appropriate chown -R www-data:www-data /var/www ``` Set appropriate ownership for a web app, recursively applying to directories and files.
- Scenario: Directory Comparison ``` diff -r dir1 dir2 ``` See differences between two configuration or output directories.
Frequently Asked Questions
Related Skills
network-tools
chaterm/terminal-skills
Linux network tools and diagnostics
shell-scripting
chaterm/terminal-skills
Bash Shell 脚本编写
process-management
chaterm/terminal-skills
Linux process management and control
user-permissions
chaterm/terminal-skills
Linux user and permission management
systemd
chaterm/terminal-skills
Systemd 服务管理
system-admin
chaterm/terminal-skills
Linux system administration and monitoring