Linux 基本命令操作


文件和目录操作

  1. 列出目录内容

    1
    ls

    输出示例:

    1
    file1.txt  file2.txt  directory1  directory2
  2. 切换到指定目录

    1
    cd directory1

    输出示例:

    1
    (无输出,仅切换目录)
  3. 显示当前工作目录

    1
    pwd

    输出示例:

    1
    /home/user/directory1
  4. 创建新目录

    1
    mkdir new_directory

    输出示例:

    1
    (无输出,仅创建目录)
  5. 删除文件

    1
    rm file1.txt

    输出示例:

    1
    (无输出,仅删除文件)

文件内容查看和编辑

  1. 显示文件内容

    1
    cat file2.txt

    输出示例:

    1
    This is the content of file2.txt.
  2. 分页显示文件内容

    1
    more file2.txt

    输出示例:

    1
    2
    This is the content of file2.txt.
    --More-- (按空格键翻页)
  3. 编辑文件

    1
    nano file2.txt

    输出示例:

    1
    (进入 nano 编辑界面)

文件权限和所有权

  1. 更改文件权限

    1
    chmod 755 file2.txt

    输出示例:

    1
    (无输出,仅更改权限)
  2. 更改文件所有者

    1
    sudo chown newuser file2.txt

    输出示例:

    1
    (无输出,仅更改所有者)

系统管理

  1. 显示当前运行的进程

    1
    ps

    输出示例:

    1
    2
    3
     PID TTY          TIME CMD
    1056 pts/0 00:00:00 bash
    1082 pts/0 00:00:00 ps
  2. 实时显示系统资源使用情况

    1
    top

    输出示例:

    1
    2
    3
    4
    5
    top - 10:45:14 up 2 days,  4:22,  1 user,  load average: 0.00, 0.01, 0.05
    Tasks: 118 total, 1 running, 117 sleeping, 0 stopped, 0 zombie
    %Cpu(s): 0.7 us, 0.3 sy, 0.0 ni, 99.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
    KiB Mem : 2048576 total, 192652 free, 1298964 used, 556960 buff/cache
    KiB Swap: 1048572 total, 1048572 free, 0 used. 652632 avail Mem
  3. 清除journalctl系统日志

    1
    journalctl --flush --rotate --vacuum-time=1s
  4. 显示磁盘使用情况

    1
    df -h

    输出示例:

    1
    2
    3
    Filesystem      Size  Used Avail Use% Mounted on
    /dev/sda1 20G 9.3G 9.1G 51% /
    tmpfs 996M 1.2M 995M 1% /run
  5. 关机或重启系统

    1
    sudo shutdown -h now

    输出示例:

    1
    (系统会显示关机通知并在几秒钟后关机)

网络相关

  1. 检测网络连接

    1
    ping google.com

    输出示例:

    1
    2
    PING google.com (172.217.164.110) 56(84) bytes of data.
    64 bytes from lga25s61-in-f14.1e100.net (172.217.164.110): icmp_seq=1 ttl=54 time=10.8 ms
  2. 设置 terminal proxy

    1
    2
    export http_proxy="http://192.168.50.1:58080"
    export https_proxy="http://192.168.50.1:58080"
  3. 使用 proxychains proxy

    A. 安装 proxychains

    1
    apt install proxychains

    B. 修改配置文件

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    nano /etc/proxychains.conf
    # Quiet mode (no output from library)
    quiet_mode

    [ProxyList]
    # add proxy here ...
    # meanwile
    # defaults set to "tor"
    #socks4 127.0.0.1 9050
    http 192.168.50.1 58080

    C. 使用

    1
    proxychains apt update

软件安装相关

  1. apt 安装软件

    1
    2
    apt update
    apt install stress
  2. apt 卸载软件

    1
    apt remove stress
  3. apt 搜索软件

    1
    apt search stress
  4. dpkg 查看已安装软件信息

    1
    dpkg -l
  5. 编译安装

    A. 安装编译组件

    1
    apt install -y build-essential

    B. 下载并解压 source code

    1
    2
    wget https://jaist.dl.sourceforge.net/project/smartmontools/smartmontools/7.4/smartmontools-7.4.tar.gz
    tar xvf smartmontools-7.4.tar.gz

    C. 编译

    1
    2
    3
    cd smartmontools-7.4/
    ./configure
    make && make install

    D. 检查是否成功编译安装

    1
    smartctl -v