A running list of SSH and Linux commands I use regularly when managing servers. Nothing fancy — just the ones I keep looking up.
Navigation
# Move into a directory
cd /var/www/html/
# Go up two levels
cd ../..
# Where am I?
pwd
# List files (all, long format, human-readable sizes)
ls -alh
Finding Things
# Find a file by name
find . -name "wp-config.php"
# Find with wildcard
find . -name "*.log"
# Find directories only
find . -name "uploads" -type d
# Search inside files (recursive, case-insensitive, show filenames)
grep -ril "search_string" /path/to/directory/
grep flags: r recursive, i case-insensitive, l filenames only, c count matches.
Find + Exec
Run a command on every match from find:
# Set directory permissions to 755
find . -type d -exec chmod 755 {} \;
# Set file permissions to 644
find . -type f -exec chmod 644 {} \;
# Move all .eml files to another folder
find . -name "*.eml" -exec mv -uv {} /home/user/mail/cur \;
# Delete all error_log files
find . -name "error_log" -exec rm -v {} \;
Files & Directories
# Copy a file
cp source.txt /path/to/destination/
# Download a file from a URL
wget https://example.com/file.zip
# Delete a file
rm -v file.txt
# Delete a directory and everything in it
rm -rvf directory_name/
Archives
# Create a tar.gz archive
tar -czvf archive.tar.gz /path/to/folder/
# Extract a tar.gz archive
tar -xzvf archive.tar.gz
Flags: c create, x extract, z gzip compression, v verbose, f filename follows.
MySQL from the Command Line
# Export a database
mysqldump -u db_user -p db_name > backup.sql
# Import a database
mysql -u db_user -p db_name < backup.sql
The -p flag prompts for the password — don't put it inline in the command.
Leave a comment