| name | file-operations |
| description | Linux file and directory operations |
| version | 1.0.0 |
| author | terminal-skills |
| tags | ["linux","file","directory","find","permissions"] |
File and Directory Operations
Overview
Linux file system operation skills, including file search, batch operations, permission management, etc.
File Search
find Command
find /path -name "*.log"
find /path -iname "*.LOG"
find /path -type f
find /path -type d
find /path -type l
find /path -mtime -7
find /path -mtime +30
find /path -mmin -60
find /path -size +100M
find /path -size -1k
find /path -name "*.log" -mtime +7 -size +10M
locate Command
locate filename
updatedb
locate -i filename
File Operations
Basic Operations
cp file1 file2
cp -r dir1 dir2
cp -p file1 file2
mv file1 file2
mv file1 /path/to/dest/
rm file
rm -rf dir
rm -i file
touch file
mkdir -p dir1/dir2/dir3
Batch Operations
rename 's/old/new/' *.txt
for f in *.txt; do mv "$f" "${f%.txt}.md"; done
find /path -name "*.tmp" -delete
find /path -name "*.log" -mtime +30 -exec rm {} \;
find /src -name "*.conf" -exec cp {} /dest/ \;
File Content
View Files
cat file
head -n 20 file
tail -n 20 file
tail -f file
less file
wc -l file
wc -w file
wc -c file
File Comparison
diff file1 file2
diff -u file1 file2
diff -r dir1 dir2
sdiff file1 file2
vimdiff file1 file2
Permission Management
View Permissions
ls -la
stat file
Modify Permissions
chmod 755 file
chmod 644 file
chmod u+x file
chmod g-w file
chmod o=r file
chmod a+r file
chmod -R 755 dir
Modify Owner
chown user file
chown user:group file
chown -R user:group dir
Common Scenarios
Scenario 1: Clean Up Large Files
find / -type f -size +100M -exec ls -lh {} \; 2>/dev/null
du -ah /path | sort -rh | head -20
Scenario 2: Find Recently Modified Files
find /path -type f -mtime -1
ls -lt /path | head -20
Scenario 3: Batch Replace File Content
sed -i 's/old/new/g' file
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 |