$npx -y skills add chaterm/terminal-skills --skill file-operationsLinux file and directory operations
| 1 | # File and Directory Operations |
| 2 | |
| 3 | ## Overview |
| 4 | Linux file system operation skills, including file search, batch operations, permission management, etc. |
| 5 | |
| 6 | ## File Search |
| 7 | |
| 8 | ### find Command |
| 9 | ```bash |
| 10 | # Search by name |
| 11 | find /path -name "*.log" |
| 12 | find /path -iname "*.LOG" # Case insensitive |
| 13 | |
| 14 | # Search by type |
| 15 | find /path -type f # Files |
| 16 | find /path -type d # Directories |
| 17 | find /path -type l # Symbolic links |
| 18 | |
| 19 | # Search by time |
| 20 | find /path -mtime -7 # Modified within 7 days |
| 21 | find /path -mtime +30 # Modified more than 30 days ago |
| 22 | find /path -mmin -60 # Modified within 60 minutes |
| 23 | |
| 24 | # Search by size |
| 25 | find /path -size +100M # Larger than 100MB |
| 26 | find /path -size -1k # Smaller than 1KB |
| 27 | |
| 28 | # Combined conditions |
| 29 | find /path -name "*.log" -mtime +7 -size +10M |
| 30 | ``` |
| 31 | |
| 32 | ### locate Command |
| 33 | ```bash |
| 34 | # Quick search (requires database update) |
| 35 | locate filename |
| 36 | updatedb # Update database |
| 37 | |
| 38 | # Case insensitive |
| 39 | locate -i filename |
| 40 | ``` |
| 41 | |
| 42 | ## File Operations |
| 43 | |
| 44 | ### Basic Operations |
| 45 | ```bash |
| 46 | # Copy |
| 47 | cp file1 file2 |
| 48 | cp -r dir1 dir2 # Recursive copy directory |
| 49 | cp -p file1 file2 # Preserve attributes |
| 50 | |
| 51 | # Move/Rename |
| 52 | mv file1 file2 |
| 53 | mv file1 /path/to/dest/ |
| 54 | |
| 55 | # Delete |
| 56 | rm file |
| 57 | rm -rf dir # Recursive force delete |
| 58 | rm -i file # Interactive confirmation |
| 59 | |
| 60 | # Create |
| 61 | touch file # Create empty file |
| 62 | mkdir -p dir1/dir2/dir3 # Recursive create directories |
| 63 | ``` |
| 64 | |
| 65 | ### Batch Operations |
| 66 | ```bash |
| 67 | # Batch rename |
| 68 | rename 's/old/new/' *.txt |
| 69 | for f in *.txt; do mv "$f" "${f%.txt}.md"; done |
| 70 | |
| 71 | # Batch delete |
| 72 | find /path -name "*.tmp" -delete |
| 73 | find /path -name "*.log" -mtime +30 -exec rm {} \; |
| 74 | |
| 75 | # Batch copy |
| 76 | find /src -name "*.conf" -exec cp {} /dest/ \; |
| 77 | ``` |
| 78 | |
| 79 | ## File Content |
| 80 | |
| 81 | ### View Files |
| 82 | ```bash |
| 83 | cat file # Full content |
| 84 | head -n 20 file # First 20 lines |
| 85 | tail -n 20 file # Last 20 lines |
| 86 | tail -f file # Real-time follow |
| 87 | less file # Paginated view |
| 88 | |
| 89 | # Statistics |
| 90 | wc -l file # Line count |
| 91 | wc -w file # Word count |
| 92 | wc -c file # Byte count |
| 93 | ``` |
| 94 | |
| 95 | ### File Comparison |
| 96 | ```bash |
| 97 | diff file1 file2 |
| 98 | diff -u file1 file2 # Unified format |
| 99 | diff -r dir1 dir2 # Compare directories |
| 100 | |
| 101 | # Side-by-side comparison |
| 102 | sdiff file1 file2 |
| 103 | vimdiff file1 file2 |
| 104 | ``` |
| 105 | |
| 106 | ## Permission Management |
| 107 | |
| 108 | ### View Permissions |
| 109 | ```bash |
| 110 | ls -la |
| 111 | stat file |
| 112 | ``` |
| 113 | |
| 114 | ### Modify Permissions |
| 115 | ```bash |
| 116 | # Numeric mode |
| 117 | chmod 755 file # rwxr-xr-x |
| 118 | chmod 644 file # rw-r--r-- |
| 119 | |
| 120 | # Symbolic mode |
| 121 | chmod u+x file # Add execute for user |
| 122 | chmod g-w file # Remove write for group |
| 123 | chmod o=r file # Set read-only for others |
| 124 | chmod a+r file # Add read for all |
| 125 | |
| 126 | # Recursive modify |
| 127 | chmod -R 755 dir |
| 128 | ``` |
| 129 | |
| 130 | ### Modify Owner |
| 131 | ```bash |
| 132 | chown user file |
| 133 | chown user:group file |
| 134 | chown -R user:group dir # Recursive modify |
| 135 | ``` |
| 136 | |
| 137 | ## Common Scenarios |
| 138 | |
| 139 | ### Scenario 1: Clean Up Large Files |
| 140 | ```bash |
| 141 | # Find files larger than 100MB |
| 142 | find / -type f -size +100M -exec ls -lh {} \; 2>/dev/null |
| 143 | |
| 144 | # Find and sort by size |
| 145 | du -ah /path | sort -rh | head -20 |
| 146 | ``` |
| 147 | |
| 148 | ### Scenario 2: Find Recently Modified Files |
| 149 | ```bash |
| 150 | # Files modified within 24 hours |
| 151 | find /path -type f -mtime -1 |
| 152 | |
| 153 | # Sort by modification time |
| 154 | ls -lt /path | head -20 |
| 155 | ``` |
| 156 | |
| 157 | ### Scenario 3: Batch Replace File Content |
| 158 | ```bash |
| 159 | # Single file replacement |
| 160 | sed -i 's/old/new/g' file |
| 161 | |
| 162 | # Batch replacement |
| 163 | find /path -name "*.conf" -exec sed -i 's/old/new/g' {} \; |
| 164 | ``` |
| 165 | |
| 166 | ## Troubleshooting |
| 167 | |
| 168 | | Problem | Solution | |
| 169 | |---------|----------| |
| 170 | | Permission denied | Use `sudo` or check file permissions | |
| 171 | | Disk space full | `df -h`, `du -sh *` to find large files | |
| 172 | | Special characters in filename | Use quotes or escape `rm "file name"` | |
| 173 | | Slow deletion of many files | Use `rsync --delete` or `find -delete` | |