Blog / Linux/ Linux Command Line Basics: Quick Reference and Operations Guide

Linux Command Line Basics: Quick Reference and Operations Guide

Linux 命令行基础:常用命令速查与操作指南

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/shine refers to the shine subdirectory inside /home.
  • Relative path: Starts from the current working directory. Example: If the current directory is /home, then shine/doc corresponds 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 --help or -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.
    • cd or cd ~ 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 -p option 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).
  • nano or vi/vim: Text editors. Beginners may start with nano; vi/vim is 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 -s option creates a soft (symbolic) link.

Compression and Extraction

  • tar xvf file.tar: Extract a .tar file.
  • tar xzvf file.tar.gz: Extract a .tar.gz or .tgz file.
  • tar xjvf file.tar.bz2: Extract a .tar.bz2 file.
  • unzip file.zip: Extract a ZIP file (requires unzip package).
  • unrar x file.rar: Extract a RAR file (requires unrar package).

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.
  • top or htop: Dynamically view process status and system resource usage.
  • free -h: Display memory usage.

Package Management

  • RHEL/CentOS/Fedora: Use yum or dnf (newer versions).
  • Debian/Ubuntu: Use apt or apt-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.
  • who or whoami: 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.

Post a Comment

Your email will not be published. Required fields are marked with *.