File Handling
File handling is a core part of Linux scripting: creating, copying, moving, syncing, compressing, comparing, finding and validating files safely.
Core Commands
| Command | Purpose | Example |
|---|---|---|
touch | Create empty file | touch app.log |
mkdir -p | Create nested directories | mkdir -p /opt/apps/config |
cp | Copy files | cp app.jar /opt/apps/ |
cp -r | Copy directories | cp -r config/ backup/ |
mv | Move or rename | mv old.log archive.log |
rm | Remove file | rm temp.txt |
rm -rf | Remove recursively | rm -rf temp/ |
find | Find by name/type/size | find /var/log -name '*.log' |
tar | Archive and compress | tar -czvf backup.tar.gz app/ |
zip/unzip | Zip archive management | zip -r app.zip app/ |
rsync | Fast sync and copy | rsync -avz logs/ user@host:/backup/ |
diff | Compare text files | diff a.yml b.yml |
stat | Show metadata | stat app.jar |
md5sum | Verify checksum | md5sum app.jar |
Safe Patterns
cp -v source target mv -v old new rm -i important.txt rsync -avzn source/ target/ # dry run first find . -type f -name '*.log' -mtime +7 -delete
Useful Checks
du -sh * find . -type f | wc -l find /data -type f -size +500M ls -ltr file app.jar