Essential Linux Commands and Practical Tips
This article introduces several frequently used Linux commands and techniques that can significantly improve your terminal efficiency.
1. Execute Multiple Commands Sequentially
In Linux, you can use a semicolon ; to separate multiple commands. The system will execute them in order.
wget https://example.com/abc.zip; unzip abc.zip
The example above downloads a file and then extracts it. Note: If the first command fails, the second will still run. To stop execution if a command fails, use && instead.
2. Moving Files and Directories
Use the mv command to move items. Common patterns:
- Move all files from current directory to parent:
mv * ../(excludes hidden files) - Move all files from a subdirectory to current directory:
mv abc/* . - Move files between subdirectories:
mv abc/* xyz/
Permission Note: If you encounter a permission error, prepend sudo to run the command with administrator privileges.
3. Compressing Files and Directories
Use zip to compress the current directory's contents into test.zip:
zip -r ./test.zip ./*
The -r flag enables recursive processing (includes subdirectories). ./* matches all non-hidden files in the current directory.
4. Checking Directory Size
To view the total size of a directory (including subdirectories), use the du (disk usage) command:
du -sh .
Flag explanation:
-s: Display only the total size.
-h: Show sizes in human-readable format (e.g., K, M, G).
.: Refers to the current directory.
Tip: These commands work on most Linux distributions (Ubuntu, CentOS) and macOS terminal. Always verify paths before executing to avoid accidental data loss.