Skip to content

Linux基础

在Superti-Cloud 算力平台租用的实例系统默认为发行版的Linux Ubuntu,因此需要了解并熟悉基础的Linux操作命令对于训练模型很有必要,以下为常用的必备命令使用介绍:

新建文件/文件夹

命令: echo, touch, vi/vim; mkdir

shell
user@hostname:/tmp/test$ echo "test" >1.txt 	# 将输出内容定向到 1.txt,并新建文件
user@hostname:/tmp/test$ touch 2.txt			# 新建一个 2.txt 空文件
user@hostname:/tmp/test$ vi 3.txt				# 编辑3.txt 可直接输入内容并保存
user@hostname:/tmp/test$ ls
1.txt  2.txt  3.txt
user@hostname:/tmp/test$mkdir test001			# 新建文件夹
user@hostname:/tmp/test$ ls
1.txt  2.txt  3.txt  test001
user@hostname:/tmp/test$

查看当前/切换路径

查看命令:pwd

切换命令:cd

shell
user@hostname:/tmp$pwd 				# 查看当前所在目录
/tmp/
user@hostname:/tmp$ mkdir test_001 	# 新建文件夹 test_001
user@hostname:/tmp$ cd test_001/ 	# 切换到 test_001 里
user@hostname:/tmp/test_001$

列出指定目录下文件/文件夹

命令:ls

shell
user@hostname:/tmp/test$ ls		# 列出当前目录下的文件和文件夹
1.txt  2.txt  mydemo
user@hostname:/tmp/test$ ll		# 列出当前目录下的文件和文件夹详细信息, 权限,属主,属组,大小,创建/更新时间
total 16
drwxr-xr-x  3 redhat redhat 4096 Nov 19 10:22 ./	# ./ 表示当前目录
drwxrwxrwt 17 root   root   4096 Nov 19 10:21 ../ 	# ../ 表示上级目录
-rw-r--r--  1 redhat redhat    0 Nov 19 10:21 1.txt
-rw-r--r--  1 redhat redhat   20 Nov 19 10:22 2.txt
drwxr-xr-x  2 redhat redhat 4096 Nov 19 10:21 mydemo/
user@hostname:/tmp/test$

重命名、移动文件/文件夹

命令: mv

shell
user@hostname:/tmp/test$ ls
1.txt  2.txt  3.txt  test001
user@hostname:/tmp/test$ mv test001/ test111
user@hostname:/tmp/test$ ls
1.txt  2.txt  3.txt  test111
user@hostname:/tmp/test$
user@hostname:/tmp/test$ mv 1.txt 2.txt 3.txt test111/		# 将文件1.txt 2.txt 3.txt 移动到 test111/ 目录下
user@hostname:/tmp/test$ ls
test111
user@hostname:/tmp/test$ cd test111/
user@hostname:/tmp/test/test111$ ls
1.txt  2.txt  3.txt
user@hostname:/tmp/test/test111$

拷贝文件/文件夹

命令: cp -r (-r表示递归)

shell
user@hostname:/tmp/test$ ls
test111  test222
user@hostname:/tmp/test$ tree ./test111/
./test111/
├── 1.txt
├── 2.txt
└── 3.txt

1 directory, 3 files
user@hostname:/tmp/test$ cp -r test111/ test222/
user@hostname:/tmp/test$ tree ./test222/
./test222/
└── test111
    ├── 1.txt
    ├── 2.txt
    └── 3.txt

2 directories, 3 files
user@hostname:/tmp/test$

删除文件/文件夹

命令: rm -rf (-r递归,-f强制)

shell
user@hostname:/tmp/test/test222$ ls
test111
user@hostname:/tmp/test/test222$ rm -rf test111/
user@hostname:/tmp/test/test222$

压缩和解压

命令: tar、zip、unzip

shell
user@hostname:/tmp/test$ ls
test111  test222
user@hostname:/tmp/test$ zip -r zz.zip test111/		# 将目录test111/ 压缩为 zz
  adding: test111/ (stored 0%)
  adding: test111/3.txt (stored 0%)
  adding: test111/1.txt (stored 0%)
  adding: test111/2.txt (stored 0%)
user@hostname:/tmp/test$ ls
test111  test222  zz.zip
user@hostname:/tmp/test$ unzip zz.zip  				# 将压缩的 zz.zip 解压到当前文件下
Archive:  zz.zip
replace test111/1.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: A     # 解压当前目录存在该文件,是否覆盖原文件
 extracting: test111/3.txt
 extracting: test111/1.txt
 extracting: test111/2.txt
user@hostname:/tmp/test$
user@hostname:/tmp/test$ tar czf test.tar.gz test111/    # tar 参数c表示压缩,x表示解压,z表示压缩/解压为gz格式压缩包
user@hostname:/tmp/test$ ls
test.tar.gz  test111  test222  zz.zip
user@hostname:/tmp/test$ tar xzf test.tar.gz
user@hostname:/tmp/test$ ls
test.tar.gz  test111  test222  zz.zip

查看/终止进程

查看命令:ps

终止命令:kill

shell
user@hostname:/tmp/test$ ps -elf   # 查看进程
F S UID        PID  PPID  C PRI  NI ADDR SZ WCHAN  STIME TTY          TIME CMD
4 S root         1     0  0  80   0 -  5568 -      Nov19 ?        00:00:06 /sbin/init
5 S root         2     1  0  80   0 -   619 -      Nov19 ?        00:00:00 /init
0 S root         6     2  0  80   0 -   628 -      Nov19 ?        00:00:00 plan9 --control-socket 6 --log-level 4 --serv
4 S root        51     1  0  79  -1 - 18755 -      Nov19 ?        00:00:11 /usr/lib/systemd/systemd-journald
4 S root        91     1  0  80   0 -  6031 -      Nov19 ?        00:00:01 /usr/lib/systemd/systemd-udevd
user@hostname:/tmp/test$ ps -elf | grep init    # 通过 |管道符,grep 正则匹配 特定进程
4 S root         1     0  0  80   0 -  5568 -      Nov19 ?        00:00:06 /sbin/init
5 S root         2     1  0  80   0 -   619 -      Nov19 ?        00:00:00 /init
user@hostname:/tmp/test$ kill -9  6     # -9 表示终止信号, 6 进程PID
user@hostname:/tmp/test$

查看整体进程CPU/内存占用情况

命令: top (top命令存在众多排序查看方式,可以通过网络查询具体使用方式)

shell
user@hostname:/tmp/test$ top
top - 10:03:09 up 1 day,  7:56,  2 users,  load average: 0.11, 0.05, 0.01
Tasks:  72 total,   1 running,  71 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.1 us,  0.1 sy,  0.0 ni, 99.8 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
MiB Mem :   7807.5 total,   3144.4 free,   3600.1 used,   1355.7 buff/cache
MiB Swap:   2048.0 total,   2048.0 free,      0.0 used.   4207.5 avail Mem

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
  571 redhat    20   0   31.4g 221512  54324 S   0.7   2.8  21:15.39 node
14713 root      20   0    2496    124      0 S   0.3   0.0   0:00.09 Relay(14715)
    1 root      20   0   22272  13348   9476 S   0.0   0.2   0:06.32 systemd
    2 root      20   0    2476   1504   1384 S   0.0   0.0   0:00.06 init-systemd(Ub
    6 root      20   0    2512    132    132 S   0.0   0.0   0:00.10 init
   51 root      19  -1   75020  23956  22724 S   0.0   0.3   0:11.40 systemd-journal
   91 root      20   0   24124   6108   4748 S   0.0   0.1   0:01.13 systemd-udevd
  101 root      20   0  152936   2204      4 S   0.0   0.0   0:00.00 snapfuse

查看GPU使用情况

命令: nvidia-smi

shell
user@hostname:/tmp/test$ nvidia-smi
Wed Nov 20 10:06:59 2024
+-----------------------------------------------------------------------------------------+
| NVIDIA-SMI 560.35.02              Driver Version: 560.94         CUDA Version: 12.6     |
|-----------------------------------------+------------------------+----------------------+
| GPU  Name                 Persistence-M | Bus-Id          Disp.A | Volatile Uncorr. ECC |
| Fan  Temp   Perf          Pwr:Usage/Cap |           Memory-Usage | GPU-Util  Compute M. |
|                                         |                        |               MIG M. |
|=========================================+========================+======================|
|   0  NVIDIA GeForce RTX 4070 ...    On  |   00000000:01:00.0  On |                  N/A |
| N/A   49C    P8              3W /  115W |     335MiB /   8188MiB |      0%      Default |
|                                         |                        |                  N/A |
+-----------------------------------------+------------------------+----------------------+

+-----------------------------------------------------------------------------------------+
| Processes:                                                                              |
|  GPU   GI   CI        PID   Type   Process name                              GPU Memory |
|        ID   ID                                                               Usage      |
|=========================================================================================|
|  No running processes found                                                             |
+-----------------------------------------------------------------------------------------+