Package Management
Find Packages with pkgs.org
pkgs.org is a Linux package search website that helps you find software packages for various Linux distributions.
# Check installed package versions (Red Hat/CentOS)
rpm -qa | grep docker
Process Management
Run Commands in Background
# Run in background (will stop when terminal closes)
cmd &
# Run in background (continues after terminal closes)
nohup cmd &
Control Background Jobs
Ctrl + Z
- Pause the current foreground job and move it to backgroundjobs
- List all background jobs (jobs -l
shows PIDs)fg
- Bring the most recent background job to foregroundfg %N
- Bring job number N to foregroundbg
- Resume the most recent job in the backgroundbg %N
- Resume job number N in the background
Using fg Command
# Restore the most recent background job
fg
# Restore a specific job by number
fg 1
# Restore using % syntax
fg %2
# Restore by process name
fg %vim
# List all background jobs
jobs
DNS Operations
Lookup CNAME Records
CNAME (Canonical Name) records map one domain name to another, commonly used for subdomains or third-party hosted services.
# Using dig
dig example.com cname
# Output: example.com. 300 IN CNAME target-domain.com.
# Using host command
host -t CNAME example.com
# Output: example.com is an alias for target-domain.com.
# Using nslookup
nslookup -type=CNAME example.com
# Output: example.com canonical name = target-domain.com.
# Check multiple domains at once
dig +short CNAME www.example.com blog.example.com
# See full DNS lookup path
dig +trace example.com
File System Operations
Find Large Files
# Find and display the 3 largest files/folders in current directory
du -sh ./* | sort -rh | head -3
# Find files larger than 100MB
find . -type f -size +100M
# Find large files and show human-readable sizes
find . -type f -size +50M -exec du -h {} \; | sort -rh
Dealing with Deleted Files That Still Use Space
When you delete a file but the disk space isn’t freed, it’s usually because a process is still using the file.
# Find deleted files still in use
lsof | grep deleted
# Clear file content without deleting the file
echo "" > /var/log/large_logfile.log
# Or more safely:
truncate -s 0 /var/log/large_logfile.log
Search Text in Files Recursively
# Search for text in all files in current directory and subdirectories
grep -r "search text" .
# Limit search to specific file types
grep -r "search text" --include="*.php" .
# Show line numbers in results
grep -rn "search text" .
# Case-insensitive search
grep -ri "search text" .
Script Safety with set
The set
command controls shell behavior and can make scripts safer.
#!/usr/bin/env bash
# Stop script if any command fails
set -o errexit # Same as set -e
# Treat unset variables as errors
set -o nounset # Same as set -u
# Make pipeline fail if any command in it fails
set -o pipefail
# Print commands before execution (useful for debugging)
set -o xtrace # Same as set -x
# Common combined safety settings
set -euxo pipefail
Error Handling Patterns
# Stop if command fails
command || exit 1
# With error message
command || { echo "Command failed"; exit 1; }
# Using if statement
if ! command; then
echo "Command failed"
exit 1
fi
# Other way
command
if [ "$?" -ne 0 ]; then echo "command failed"; exit 1; fi
# Execute second command only if first succeeds
command1 && command2
Navigation Shortcuts
Using CDPATH
CDPATH creates shortcuts for the cd
command by checking multiple locations.
# Add directories to CDPATH
export CDPATH=.:/etc:~/projects
# Now you can directly cd to subdirectories of those paths
cd project1 # Changes to ~/projects/project1
cd mail # Changes to /etc/mail
# Unset CDPATH (useful in scripts for predictability)
unset CDPATH
System Configuration
Set Timezone in Ubuntu
# Interactive timezone selection
sudo dpkg-reconfigure tzdata
# Set timezone directly
sudo timedatectl set-timezone Asia/Shanghai
# Restart logging service (may be needed for cron)
sudo systemctl restart rsyslog
# Or
/etc/init.d/rsyslog restart
Open PDF Files from Terminal
# Open PDF without loading file explorer
evince document.pdf
# Alternative PDF viewers
xpdf document.pdf
okular document.pdf
References
- setting timezone from terminal - Ask Ubuntu
- cron - How do you set the timezone for crontab? - Ask Ubuntu
- Ubuntu 命令行打开 PDF 文件 - bigmarco 的专栏
- Linux 文件删除,但是 df 之后磁盘空间没有释放 | cnblogs
- How to lookup CNAME records | nslookup.io
- Linux 命令后台运行
– EOF –