Linux Command Line Basics
The Linux command line is a core tool for system administration. A command's basic syntax is:
command [options] [arguments] [path]
Square brackets [] indicate optional parts. If the path is omitted, the command operates on the current working directory.
Path Notation
Paths are either absolute or relative:
- Absolute path: Starts from the root directory
/. Example:/home/shinerefers to theshinesubdirectory inside/home. - Relative path: Starts from the current working directory. Example: If the current directory is
/home, thenshine/doccorresponds to the absolute path/home/shine/doc.
Common special symbols:
.represents the current directory...represents the parent directory.~represents the current user's home directory (e.g.,/home/shine).
Getting Command Help
If you are unsure how to use a command, consult its help:
man command_name: View the full manual page.command_name --helpor-h: Quickly view common options (e.g.,ls --help).
Interrupting and Exiting Commands
During command execution, you can interrupt or exit using:
Ctrl+C: Terminate the currently running foreground command.Ctrl+D: Send EOF (End-of-File), often used to exit the current shell or end input.exit,logout: Exit the current shell session.
Common Command Quick Reference
File and Directory Operations
cd [path]: Change working directory.cd /switches to the root directory.cdorcd ~returns to the current user's home directory.
ls [options] [path]: List directory contents.ls -a: Show all files (including hidden ones).ls -l: Display in detailed list format.
mkdir [-p] directory_name: Create a directory. The-poption creates parent directories if they don't exist.rm [options] file_or_directory: Remove files or directories.rm -r: Recursively delete a directory and its contents.rm -f: Force deletion without confirmation.
mv source destination: Move or rename files/directories.cp [options] source destination: Copy files or directories.cp -r: Recursively copy a directory and its contents.
pwd: Print the absolute path of the current working directory.
Viewing and Editing File Content
cat filename: Display the entire file content.less filename: View file content page by page (supports scrolling, searching).echo text: Output text or variable values (e.g.,echo $PATH).nanoorvi/vim: Text editors. Beginners may start withnano;vi/vimis more powerful but has a steeper learning curve.
File Permissions and Attributes
chmod [options] permissions file: Change file permissions (e.g.,chmod 755 script.sh).chown [options] user:group file: Change file owner and group.ln [-s] source_file link_file: Create a link. The-soption creates a soft (symbolic) link.
Compression and Extraction
tar xvf file.tar: Extract a.tarfile.tar xzvf file.tar.gz: Extract a.tar.gzor.tgzfile.tar xjvf file.tar.bz2: Extract a.tar.bz2file.unzip file.zip: Extract a ZIP file (requiresunzippackage).unrar x file.rar: Extract a RAR file (requiresunrarpackage).
System Status Monitoring
df -h: Display disk space usage in a human-readable format.du -sh [directory_or_file]: Check the size of a specified directory or file.toporhtop: Dynamically view process status and system resource usage.free -h: Display memory usage.
Package Management
- RHEL/CentOS/Fedora: Use
yumordnf(newer versions). - Debian/Ubuntu: Use
aptorapt-get.
Other Useful Commands
grep [pattern] file: Search for a pattern in a file.find path -name filename: Find files.sort file: Sort file contents.export VARIABLE=value: Set an environment variable.su [username]: Switch user (default is root).passwd [username]: Change a user's password.whoorwhoami: Show information about currently logged-in users.
Mastering these basic commands will enable you to perform daily operations and system administration efficiently in the Linux terminal. Practice regularly and use man and --help for detailed assistance.