Skip to main content

File va Directory Operations

Advanced File Operations (Ilg'or Fayl Operatsiyalari)

find - Find Files and Directories

Fayllar va kataloglarni izlash uchun eng kuchli vosita.

# Asosiy sintaks
find [path] [expression] [action]

# Name bo'yicha izlash
find /home -name "*.txt" # .txt fayllarni izlash
find . -name "config*" # config bilan boshlangan fayllar
find /etc -iname "*.CONF" # Case-insensitive izlash

# Type bo'yicha izlash
find /var -type f # Faqat fayllar
find /var -type d # Faqat kataloglar
find /dev -type l # Symbolic link'lar
find /var -type s # Socket fayllar

# Size bo'yicha izlash
find /var/log -size +100M # 100MB dan katta fayllar
find /tmp -size -1M # 1MB dan kichik fayllar
find . -size 0 # Bo'sh fayllar
find /home -size +1G -size -5G # 1GB dan katta, 5GB dan kichik

# Time bo'yicha izlash
find /var/log -mtime -7 # 7 kun ichida o'zgargan fayllar
find /tmp -mtime +30 # 30 kundan eski fayllar
find . -mmin -60 # 60 daqiqa ichida o'zgargan
find /var -atime +7 # 7 kundan beri ochilmagan

# Permission bo'yicha izlash
find /var -perm 644 # Aniq 644 permission
find /bin -perm -755 # Kamida 755 permission
find /home -perm /222 # Write permission bor

# User/Group bo'yicha izlash
find /home -user john # john foydalanuvchisiga tegishli
find /var -group www-data # www-data guruhiga tegishli
find / -nouser # Hech qaysi foydalanuvchiga tegishli emas

DevOps uchun amaliy find misollar:

# Old log files cleanup
find /var/log -name "*.log" -mtime +30 -delete

# Find large Docker images
find /var/lib/docker -name "*.json" -size +100M

# Find config files
find /etc -name "*.conf" -o -name "*.cfg"

# Find SUID/SGID files (security audit)
find /usr -perm -4000 -o -perm -2000

# Find recently modified Kubernetes manifests
find ./k8s -name "*.yaml" -mtime -1

# Find empty directories for cleanup
find /tmp -type d -empty -delete

# Find files owned by specific user
find /opt -user nginx -type f

# Find world-writable files (security risk)
find /var -perm -002 -type f

# Find broken symbolic links
find /usr -type l ! -exec test -e {} \; -print

Fayl nomlarini tez izlash uchun ma'lumotlar bazasidan foydalanadi.

# locate ni ishlatish
locate nginx.conf # nginx.conf nomli fayllarni topish
locate "*.log" | head -20 # .log fayllarni topish

# Ma'lumotlar bazasini yangilash
sudo updatedb # Ma'lumotlar bazasini yangilash

# locate with regex
locate -r "nginx.*\.conf$" # regex pattern

# Case insensitive
locate -i NGINX.CONF

which va whereis - Command Location

Buyruqlarning joylashuvini topish.

# which - buyruq joylashuvini topish
which python3 # /usr/bin/python3
which docker # /usr/bin/docker
which kubectl # /usr/local/bin/kubectl

# whereis - buyruq, manuals va source code joylashuvi
whereis nginx # nginx: /usr/sbin/nginx /etc/nginx /usr/share/man/man8/nginx.8.gz

# type - buyruq turi va joylashuvi
type ls # ls is aliased to `ls --color=auto'
type cd # cd is a shell builtin

File Content Operations (Fayl Mazmuni Operatsiyalari)

head va tail - File Beginning and End

Faylning boshi va oxirini ko'rish.

# head - faylning boshi
head file.txt # Birinchi 10 qator
head -n 20 file.txt # Birinchi 20 qator
head -c 100 file.txt # Birinchi 100 byte

# tail - faylning oxiri
tail file.txt # Oxirgi 10 qator
tail -n 20 file.txt # Oxirgi 20 qator
tail -c 100 file.txt # Oxirgi 100 byte

# tail -f - real-time monitoring
tail -f /var/log/nginx/access.log # Log faylni real-time da kuzatish
tail -F /var/log/app.log # File rotation ni ham handle qiladi

# Multiple files
head -n 5 file1.txt file2.txt
tail -n 5 *.log

DevOps uchun amaliy misollar:

# Application logs monitoring
tail -f /var/log/app/*.log

# Kubernetes pod logs
kubectl logs -f deployment/app

# Nginx access logs - last 100 entries
tail -n 100 /var/log/nginx/access.log

# Error logs - first error
head -n 1 /var/log/nginx/error.log

# Multiple log files monitoring
tail -f /var/log/nginx/*.log

# Show recent deployment logs
tail -n 50 /var/log/deployment.log

less va more - Paging Through Files

Katta fayllarni sahifalab ko'rish.

# less - advanced pager
less /var/log/syslog
# Navigation:
# Space - keyingi sahifa
# b - oldingi sahifa
# q - chiqish
# /pattern - izlash
# n - keyingi match
# N - oldingi match
# G - fayl oxiri
# g - fayl boshi

# more - simple pager
more /etc/passwd

# less with options
less -N file.txt # Line numbers bilan
less -S file.txt # No line wrapping
less +G file.txt # Fayl oxiridan boshlash

grep - Pattern Matching

Fayllarda matn izlash uchun eng muhim vosita.

# Asosiy foydalanish
grep "pattern" file.txt
grep "error" /var/log/nginx/error.log

# Useful options
grep -i "error" file.txt # Case insensitive
grep -v "INFO" file.txt # Inverse match (exclude)
grep -n "error" file.txt # Line numbers bilan
grep -c "error" file.txt # Faqat count
grep -l "error" *.log # Faqat file names
grep -r "pattern" /path/ # Recursive search
grep -A 3 "error" file.txt # 3 qator keyin ham
grep -B 3 "error" file.txt # 3 qator oldin ham
grep -C 3 "error" file.txt # 3 qator oldin va keyin

# Regular expressions
grep "^error" file.txt # Qator boshida
grep "error$" file.txt # Qator oxirida
grep "err.*or" file.txt # err va or orasida istalgan belgilar
grep "error\|warning" file.txt # error yoki warning

# Extended regex
grep -E "error|warning" file.txt
grep -P "\d{3}-\d{3}-\d{4}" file.txt # Perl regex

DevOps uchun amaliy grep misollar:

# Error logs analysis
grep -i "error\|exception\|fail" /var/log/app.log

# Find specific HTTP status codes
grep " 500 " /var/log/nginx/access.log
grep " 4[0-9][0-9] " /var/log/nginx/access.log

# Security analysis
grep "Failed password" /var/log/auth.log
grep -i "hack\|attack\|exploit" /var/log/security.log

# Configuration search
grep -r "database" /etc/
grep "listen.*80" /etc/nginx/sites-enabled/*

# Docker logs analysis
docker logs container_name 2>&1 | grep -i error

# Kubernetes events
kubectl get events | grep -i warning

# Find processes
ps aux | grep nginx
ps aux | grep -v grep | grep nginx

# Network connections
netstat -tulpn | grep :80
ss -tulpn | grep :443

# System information
lscpu | grep "Model name"
free -h | grep Mem

File Comparison (Fayl Taqqoslash)

diff - Compare Files

Fayllar orasidagi farqni ko'rsatish.

# Asosiy foydalanish
diff file1.txt file2.txt

# Useful options
diff -u file1.txt file2.txt # Unified format
diff -c file1.txt file2.txt # Context format
diff -i file1.txt file2.txt # Case insensitive
diff -w file1.txt file2.txt # Ignore whitespace
diff -r dir1/ dir2/ # Recursive directory comparison

# Side by side comparison
diff -y file1.txt file2.txt
diff -y -W 120 file1.txt file2.txt # Custom width

cmp - Binary Comparison

Fayllarni byte-by-byte taqqoslash.

cmp file1 file2                # Silent comparison
cmp -l file1 file2 # Show all differences
cmp -s file1 file2 # Silent mode (exit code only)

DevOps uchun comparison misollar:

# Config file changes
diff -u nginx.conf.old nginx.conf

# Kubernetes manifest changes
diff -u deployment-v1.yaml deployment-v2.yaml

# Directory structures
diff -r /etc/nginx/ /backup/nginx/

# Binary files
cmp image1.jpg image2.jpg

# Script changes with context
diff -c deploy-v1.sh deploy-v2.sh

File Linking (Fayl Bog'lash)

# Hard link yaratish
ln source.txt hardlink.txt

# Symbolic link yaratish
ln -s source.txt symlink.txt
ln -s /path/to/source /path/to/link

# Link'larni ko'rish
ls -li # inode numbers bilan
ls -la # symbolic link'lar ko'rinadi

# Absolute vs relative symbolic links
ln -s /absolute/path/file.txt absolute_link
ln -s ../relative/path/file.txt relative_link

DevOps uchun linking misollar:

# Configuration management
ln -s /etc/nginx/sites-available/mysite /etc/nginx/sites-enabled/

# Application versions
ln -s /opt/app-v2.1.0 /opt/app-current

# Log file management
ln -s /var/log/app/app.log /var/log/current-app.log

# Binary management
ln -s /usr/local/bin/kubectl-1.25 /usr/local/bin/kubectl

# Docker volume links
ln -s /data/mysql /var/lib/mysql

File Archiving and Compression (Arxivlash va Siqish)

tar - Archive Files

Fayllarni arxivlash va chiqarish.

# Archive yaratish
tar -cvf archive.tar file1 file2 dir1/
tar -czvf archive.tar.gz dir/ # gzip compression
tar -cjvf archive.tar.bz2 dir/ # bzip2 compression
tar -cJvf archive.tar.xz dir/ # xz compression

# Archive ni ko'rish
tar -tvf archive.tar # Archive mazmuni
tar -tzvf archive.tar.gz

# Archive ni chiqarish
tar -xvf archive.tar # Extract
tar -xzvf archive.tar.gz
tar -xjvf archive.tar.bz2

# Specific files extract
tar -xvf archive.tar file1.txt
tar -xzvf archive.tar.gz "*.conf"

# Exclude files
tar --exclude="*.log" -czvf backup.tar.gz /opt/app/

Compression Tools

# gzip/gunzip
gzip file.txt # file.txt.gz yaratadi
gunzip file.txt.gz # file.txt ga qaytaradi
gzip -d file.txt.gz # gunzip bilan bir xil

# zip/unzip
zip archive.zip file1 file2
zip -r archive.zip directory/
unzip archive.zip
unzip -l archive.zip # Archive mazmuni

# 7zip
7z a archive.7z file1 file2
7z x archive.7z

DevOps uchun archiving misollar:

# Application backup
tar -czvf app-backup-$(date +%Y%m%d).tar.gz /opt/myapp/

# Database backup with compression
mysqldump mydb | gzip > mydb-$(date +%Y%m%d).sql.gz

# Config backup
tar --exclude="*.log" -czvf config-backup.tar.gz /etc/

# Docker context
tar -czf docker-context.tar.gz Dockerfile docker-compose.yml src/

# Kubernetes manifests
tar -czvf k8s-manifests-$(date +%Y%m%d).tar.gz *.yaml

# Log archive for analysis
tar -czvf logs-$(date +%Y%m%d).tar.gz /var/log/*.log

# Restore from backup
tar -xzvf app-backup-20231201.tar.gz -C /opt/

# Extract specific files
tar -xzvf backup.tar.gz "*/config/*.conf"

File Permissions and Ownership (Ruxsatlar va Egalik)

chmod - Change File Permissions

# Numeric mode
chmod 755 file.txt # rwxr-xr-x
chmod 644 file.txt # rw-r--r--
chmod 600 file.txt # rw-------
chmod 777 file.txt # rwxrwxrwx (dangerous!)

# Symbolic mode
chmod u+x file.txt # User uchun execute qo'shish
chmod g-w file.txt # Group dan write olib tashlash
chmod o=r file.txt # Other uchun faqat read
chmod a+r file.txt # All uchun read qo'shish

# Recursive
chmod -R 755 directory/ # Rekursiv ravishda

# Special permissions
chmod u+s file # SUID bit
chmod g+s directory # SGID bit
chmod +t directory # Sticky bit

chown - Change Ownership

# Owner o'zgartirish
chown user file.txt
chown user:group file.txt
chown :group file.txt # Faqat group

# Recursive
chown -R user:group directory/

# Reference file
chown --reference=ref_file target_file

DevOps uchun permissions misollar:

# Web files
chown -R www-data:www-data /var/www/html/
chmod -R 644 /var/www/html/
chmod -R 755 /var/www/html/scripts/

# Application files
chown -R app:app /opt/myapp/
chmod 755 /opt/myapp/bin/*
chmod 600 /opt/myapp/config/secrets.conf

# SSH keys
chmod 700 ~/.ssh/
chmod 600 ~/.ssh/id_rsa
chmod 644 ~/.ssh/id_rsa.pub
chmod 600 ~/.ssh/authorized_keys

# Scripts
chmod +x deploy.sh
chmod 755 /usr/local/bin/monitoring-script.sh

# Log files
chown -R syslog:adm /var/log/
chmod 640 /var/log/*.log

# Docker socket
chown root:docker /var/run/docker.sock
chmod 660 /var/run/docker.sock

Practical DevOps Scenarios (Amaliy DevOps Stsenarilar)

1. Log Analysis va Cleanup

#!/bin/bash
# Log analysis script

LOG_DIR="/var/log"
DAYS_TO_KEEP=30

# Find large log files
echo "=== Large log files (>100MB) ==="
find $LOG_DIR -name "*.log" -size +100M -exec ls -lh {} \;

# Find old log files
echo "=== Old log files (>$DAYS_TO_KEEP days) ==="
find $LOG_DIR -name "*.log" -mtime +$DAYS_TO_KEEP

# Archive old logs
echo "=== Archiving old logs ==="
find $LOG_DIR -name "*.log" -mtime +$DAYS_TO_KEEP -exec gzip {} \;

# Delete very old archives
find $LOG_DIR -name "*.gz" -mtime +90 -delete

# Log summary
echo "=== Log directory summary ==="
du -sh $LOG_DIR/*

2. Application Deployment

#!/bin/bash
# Application deployment script

APP_NAME="myapp"
VERSION="v2.1.0"
DEPLOY_DIR="/opt"
BACKUP_DIR="/backup"

# Backup current version
if [ -L "$DEPLOY_DIR/$APP_NAME-current" ]; then
CURRENT_VERSION=$(readlink $DEPLOY_DIR/$APP_NAME-current | xargs basename)
echo "Backing up current version: $CURRENT_VERSION"
tar -czvf $BACKUP_DIR/$APP_NAME-$CURRENT_VERSION-$(date +%Y%m%d).tar.gz \
-C $DEPLOY_DIR $CURRENT_VERSION
fi

# Deploy new version
echo "Deploying $APP_NAME $VERSION"
tar -xzvf $APP_NAME-$VERSION.tar.gz -C $DEPLOY_DIR

# Update symlink
ln -sfn $DEPLOY_DIR/$APP_NAME-$VERSION $DEPLOY_DIR/$APP_NAME-current

# Set permissions
chown -R app:app $DEPLOY_DIR/$APP_NAME-$VERSION
chmod 755 $DEPLOY_DIR/$APP_NAME-$VERSION/bin/*

echo "Deployment completed successfully"

3. Security Audit

#!/bin/bash
# Security audit script

echo "=== Security Audit Report ==="
echo "Date: $(date)"
echo

# Check SUID/SGID files
echo "=== SUID/SGID Files ==="
find /usr -perm -4000 -o -perm -2000 2>/dev/null

# Check world-writable files
echo "=== World-writable Files ==="
find /var -perm -002 -type f 2>/dev/null | head -20

# Check for unusual processes
echo "=== Running Processes ==="
ps aux | grep -v "^\[" | sort -k3 -nr | head -10

# Check network connections
echo "=== Active Network Connections ==="
netstat -tulpn | grep LISTEN

# Check recent logins
echo "=== Recent Logins ==="
lastlog | grep -v "Never"

# Check SSH attempts
echo "=== Failed SSH Attempts ==="
grep "Failed password" /var/log/auth.log 2>/dev/null | tail -10

Bu tutorial file va directory operatsiyalari bo'yicha DevOps engineers uchun zarur bo'lgan barcha asosiy ma'lumotlarni qamrab oladi. Amaliy misollar orqali real production environment'da foydalanish ko'nikmalarini oshirish mumkin.