关于 file-operations
面向 Linux 文件系统操作的技能指南,覆盖搜索、批量处理、内容查看和权限管理,适用于日常文件维护与排障。
主要功能
- 01按名称和类型搜索文件
- 02复制、移动及批量重命名
- 03查看、比较和统计文件内容
- 04管理文件权限与所有者
- 05查找大文件及近期变更
适用场景
- 01定位日志或指定类型文件
- 02批量清理临时文件和旧日志
- 03检查磁盘占用并定位大文件
- 04调整文件权限或批量替换内容
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ chaterm/ terminal-skills --skill file-operations安装后在 Claude Code 中输入/file-operations确认已加载。
详细介绍
文件和目录操作
概述
Linux 文件系统操作技能,包括文件搜索、批量操作、权限管理等。
文件搜索
find 命令
# 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 命令
# Quick search (requires database update)
locate filename
updatedb # Update database
# Case insensitive
locate -i filename
文件操作
基本操作
# 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 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/ \;
文件内容
查看文件
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
文件比较
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
权限管理
查看权限
ls -la
stat file
修改权限
# 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
修改所有者
chown user file
chown user:group file
chown -R user:group dir # Recursive modify
常见场景
场景 1:清理大文件
# 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
场景 2:查找最近修改的文件
# Files modified within 24 hours
find /path -type f -mtime -1
# Sort by modification time
ls -lt /path | head -20
场景 3:批量替换文件内容
# Single file replacement
sed -i 's/old/new/g' file
# Batch replacement
find /path -name "*.conf" -exec sed -i 's/old/new/g' {} \;
故障排除
| 问题 | 解决方案 |
|---|---|
| 权限被拒绝 | 使用 sudo 或检查文件权限 |
| 磁盘空间已满 | 使用 df -h、du -sh * 查找大文件 |
| 文件名中包含特殊字符 | 使用引号或转义字符,例如 rm "file name" |
| 删除大量文件时速度较慢 | 使用 rsync --delete 或 find -delete |
评论
支持 Markdown