Find Command
The find command is one of the most powerful tools in Linux for locating files and directories.
Find Files by Name
# Find files by name (case-sensitive)
find /path/to/search -name "filename.txt"
# Find files by name (case-insensitive)
find /path/to/search -iname "*.log"
# Find in current directory
find . -name "*.js"
Find by Type
# Find only directories
find . -type d -name "config"
# Find only files
find . -type f -name "*.md"
# Find symbolic links
find . -type l
Find and Execute Commands
# Find and delete files
find . -name "*.tmp" -delete
# Find and execute a command on each file
find . -name "*.log" -exec rm {} \;
# Find and grep within files
find . -name "*.js" -exec grep -l "TODO" {} +
# Find files containing specific text
find . -name "config_*" -exec grep "DATABASE" {} +
The
+at the end is more efficient than\;as it passes multiple files to grep at once.
File Permissions
Change Directory Permissions
# Set all directories to rwxr-xr-x (755)
find . -type d -exec chmod 755 {} \;
# Set all files to rw-r--r-- (644)
find . -type f -exec chmod 644 {} \;
Understanding Permission Numbers
| Number | Permission | Meaning |
|---|---|---|
| 7 | rwx | Read, Write, Execute |
| 6 | rw- | Read, Write |
| 5 | r-x | Read, Execute |
| 4 | r-- | Read only |
| 0 | --- | No permission |
# Common permission patterns
chmod 755 script.sh # Owner: all, Group/Others: read+execute
chmod 644 config.txt # Owner: read+write, Group/Others: read
chmod 600 secrets.key # Owner only: read+write
chmod +x script.sh # Add execute permission
Disk Management
View Disk Partitions
# Show disk partitions with filesystem type and labels
sudo lsblk -o NAME,FSTYPE,SIZE,MOUNTPOINT,LABEL
# Detailed disk usage
df -h
# Directory size
du -sh /path/to/directory
# Find largest directories
du -h --max-depth=1 | sort -hr | head -10
Mount Partitions
# Mount a partition read-write (when it's mounted read-only)
mount -o remount,rw /
# Mount USB drive
sudo mount /dev/sdb1 /mnt/usb
# Unmount
sudo umount /mnt/usb
Grep & Text Search
# Search recursively with line numbers
grep -rn "search_term" /path/to/search
# Exclude directories from search
grep -rn "exports" --exclude-dir={node_modules,dist,build} .
# Case-insensitive search
grep -ri "error" /var/log/
# Show only filenames
grep -l "pattern" *.js
# Count matches
grep -c "pattern" file.txt
# Invert match (lines NOT containing pattern)
grep -v "debug" app.log
Creating Patches
Patches are essential for sharing code changes and applying fixes.
# Generate a unified patch between directories
diff -rupN original/ modified/ > changes.patch
# Apply a patch
patch -p1 < changes.patch
# Dry run (test without applying)
patch -p1 --dry-run < changes.patch
# Reverse a patch
patch -R -p1 < changes.patch
The
-p1flag strips the first directory component from file paths in the patch.
Process Management
# Find process by name
ps aux | grep nginx
# Get just the PID
ps aux | grep nginx | awk '{print $2}'
# Or more simply
pgrep nginx
# Kill by name
pkill nginx
# Kill by PID
kill -9 12345
# Monitor processes in real-time
top
htop # Better alternative (install with: apt install htop)
Library Dependencies
# List symbols in a library
nm -C -n /path/to/libexample.so
# Check shared library dependencies
ldd /path/to/executable
# Find which package provides a file
dpkg -S /usr/bin/curl # Debian/Ubuntu
rpm -qf /usr/bin/curl # RHEL/CentOS
Image Manipulation
ImageMagick's convert command is powerful for batch image processing.
# Install ImageMagick
sudo apt install imagemagick
# Resize image
convert input.jpg -resize 800x600 output.jpg
# Resize maintaining aspect ratio
convert input.jpg -resize 800x output.jpg
# Add padding/extend canvas
convert input.jpg -gravity center -extent 1000x1000 output.jpg
# Convert format
convert input.png output.jpg
# Batch resize all images
for img in *.jpg; do convert "$img" -resize 50% "resized_$img"; done
Creating Aliases
Add aliases to ~/.bashrc or ~/.bash_aliases for frequently used commands.
# Edit your bashrc
nano ~/.bashrc
# Add aliases
alias ll='ls -la'
alias gs='git status'
alias gp='git pull'
alias dc='docker-compose'
alias k='kubectl'
# Reload bashrc
source ~/.bashrc
System-wide Aliases
# For all users, add to /etc/profile or /etc/bash.bashrc
# Example: safety alias for reboot
alias reboot='echo "Are you sure? Use /sbin/reboot to confirm"'
RPM Package Management
# Reinstall a package
rpm -Uvh --replacepkgs package.rpm
# Query installed packages
rpm -qa | grep nginx
# List files in a package
rpm -ql package-name
# Find which package owns a file
rpm -qf /usr/bin/curl
Quick Reference
| Task | Command |
|---|---|
| Find files by name | find . -name "*.js" |
| Set directory permissions | find . -type d -exec chmod 755 {} \; |
| Show disk usage | df -h |
| Search in files | grep -rn "term" . |
| Create patch | diff -rupN old/ new/ > patch |
| Find process PID | pgrep process-name |
| Resize image | convert in.jpg -resize 800x out.jpg |
Resources
- Bash Beginners Guide
- Linux Man Pages
- ExplainShell - Explains shell commands
Need help with Linux server administration? Contact us for consulting.