Guide to Basic Linux Commands for Beginners
Linux is a powerful open-source operating system widely used in server administration, programming, and web development. To work efficiently with Linux, you need to master its basic commands. In this article, we’ll explore fundamental Linux commands with detailed explanations to help you apply them effortlessly.
1. Command ls /etc/php/
- Meaning: This is actually a command in Unix-like systems (including Linux), but you might have meant dir in Windows or a similar command. In Linux, to list the contents of the /etc/php/ directory, you use:
ls -l /etc/php/
2. Command mv file_name_old file_name_new
- Meaning: Move or rename a file/directory.
- Explanation:
- mv stands for "move".
- Example: mv old.txt new.txt will rename the file from old.txt to new.txt. If a path is specified, the file will be moved to that location.
3. Command cp -a path/folder path/new_folder
- Meaning: Copy an entire directory while preserving its structure and permissions.
- Explanation:
- -a (archive) ensures all attributes, permissions, and symbolic links are copied.
- Example: cp -a /home/user/sites /backup/sites copies the sites directory to /backup.
4. Creating and Managing ZIP Files
- Install ZIP:
sudo apt update
sudo apt install zip unzip -y
- Create a ZIP File:
zip -r sites.zip /path/to/sites
- -r (recursive) compresses the entire sites directory into sites.zip.
- Split ZIP File:
split -b 900M sites.zip sites.zip.part
- Splits sites.zip into smaller parts, each 900MB, with names like sites.zip.partaa, sites.zip.partab, etc.
- Recombine ZIP File:
cat sites.zip.part* > sites.zip
- Combines all parts into a single sites.zip file.
- Extract ZIP:
unzip sites.zip -d /desired/path/
- Extracts sites.zip to the specified directory.
5. Command sudo ufw allow 'Nginx Full'
- Meaning: Open ports for Nginx in the UFW firewall.
- Explanation:
- UFW (Uncomplicated Firewall) manages firewall rules.
- This command allows traffic through ports 80 (HTTP) and 443 (HTTPS) for Nginx.
6. Installing and Managing SSL with Certbot
- Update Packages:
sudo apt update
- Install Certbot:
sudo apt install certbot python3-certbot-nginx -y
- Generate SSL Certificate:
sudo certbot --nginx -d example.com -d www.example.com
- Automatically configures SSL for Nginx on the specified domains.
- Test Renewal:
sudo certbot renew --dry-run
- Simulates certificate renewal without applying it.
7. Download Files with curl
- Example
curl -O https://test4kynang.intergreat.com/test4kynang.sql
- Explanation: Downloads the test4kynang.sql file to the current directory.
8. Managing Ownership and Permissions
- Identify Web Server User:
ps aux | grep apache2 # Or grep nginx
- Checks processes to identify the user (typically www-data).
- Change Ownership:
sudo chown -R www-data:www-data /path/to/drupal/sites/default/files
- Assigns ownership to www-data (web server user) for the Drupal directory.
- Set Permissions:
sudo chmod -R 755 /path/to/drupal/sites/default/files
- Permissions 755: full access for the owner, read/execute for others.
9. Automated Bash Script
- Example:
#!/bin/bash set -e cd /home/iotdev/vn-ielts-test/web wget https://github.com/vrana/adminer/releases/download/v4.8.1/adminer-4.8.1-en.php echo "Download completed"
- Automatically changes directory and downloads the adminer file.
10. Renaming Files and Content
- Rename File Extensions
for file in *.php; do mv "$file" "${file%.php}.html"; done
- Converts all .php files to .html.
- Replace Content:
find . -type f -exec sed -i '' 's/\.php/\.html/g' {} +
- Replaces .php with .html in file contents.
11. Using the Find function in nano
sudo nano /path/to/file
Press Ctrl + W (Where Is) to activate search mode.
Enter the string you want to find (e.g., hello) and press Enter.
nano will move the cursor to the first occurrence of the string found.
To find the next occurrence, press Alt + W (or Ctrl + W then Enter if there are no changes).
12. Using the Replace function in nano
To replace one instance at a time:
Press Ctrl + \ (backslash key) to enter replace mode.
Enter the string you want to find (e.g., hello) and press Enter.
Enter the replacement string (e.g., world) and press Enter.
nano will ask if you want to replace at the current position:
- Press Y (Yes) to replace.
- Press N (No) to skip and move to the next occurrence.
- Press A (All) to replace all occurrences of the string in the file without confirming each one.
Example:
- Original file: hello world hello universe
- Press Ctrl + \, enter hello, then world2.
- Choose A → File becomes: world2 world world2 universe.
13. Replacing in bulk with sed (if you need it faster)
If you want to replace all occurrences of a string without opening nano, you can use the sed command from the terminal:
sudo sed -i 's/hello/world/g' /path/to/file
- -i: modifies the file directly.
- s/hello/world/g: replaces all instances of hello with world.
After that, you can open the file with sudo nano to check the result.