Linux下常用命令
关机重启命令
shutdown
命令
例:立即关机
shutdown -h now
例:一分钟后关机
shutdown -h 1
例:立即重启
shutdown -r now
文件目录相关
ls
指令
语法:ls [选项] [目录]
功能: 列出文件及其信息
-h
选项
配合 -l
选项人类易读方式显示文件大小 (with -l, print sizes in human readable format)
# 默认是用字节数显示文件大小 [root@hcss-ecs-d3d5 test]# ls -al total 32 drwxr-xr-x 3 root root 4096 Feb 26 19:06 . dr-xr-x---. 7 root root 4096 Feb 26 19:06 .. -rwxr-xr-x 1 root root 8968 Feb 26 19:06 a.out drwxr-xr-x 2 root root 4096 Feb 26 18:59 directory
对比 ls -al
#以易读的形式列出文件大小 [root@hcss-ecs-d3d5 test]# ls -alh total 32K drwxr-xr-x 3 root root 4.0K Feb 26 19:06 . dr-xr-x---. 7 root root 4.0K Feb 26 19:06 .. -rwxr-xr-x 1 root root 8.8K Feb 26 19:06 a.out drwxr-xr-x 2 root root 4.0K Feb 26 18:59 directory -rw-r--r-- 1 root root 85 Feb 26 19:05 test.cpp -rw-r--r-- 1 root root 41 Feb 26 19:06 test.hpp -rw-r--r-- 1 root root 0 Feb 26 19:04 test.sh
-t
选项
以修改时间排序, 新的优先(sort by modification time, newest first)
# 可以看到最新创建的排在最上面 [root@hcss-ecs-d3d5 test]# ls -tl total 4 -rw-r--r-- 1 root root 0 Feb 26 19:04 test.sh -rw-r--r-- 1 root root 0 Feb 26 19:04 test.hpp -rw-r--r-- 1 root root 0 Feb 26 19:03 test.cpp drwxr-xr-x 2 root root 4096 Feb 26 18:59 directory
-d
(--directory
)选项
列出目录本身,而不是它们的内容(list directories themselves, not their contents)
#查看当前目录 [root@hcss-ecs-d3d5 directory]# pwd /root/test/directory #使用-d选项查看该目录, 并使用-i选项inode号 #结果显示一个点号, 就代表当前目录(相对路径) [root@hcss-ecs-d3d5 directory]# ls -di 656719 . #验证其与打印的信息就是directory这个目录的 #返回到该目录的父目录, 再在该父目录下打印该目录下所有文件的inode号, 结果与前者相同, 所以是同一个文件. [root@hcss-ecs-d3d5 directory]# cd .. [root@hcss-ecs-d3d5 test]# ls -i 656719 directory [root@hcss-ecs-d3d5 test]#
-n
选项
把所属用户名和组名用 uid
和 gid
代替
[root@hcss-ecs-d3d5 test]# ls -n total 24 # 当前用户为root,root用户uid为0,所在组root的gid也为0 -rwxr-xr-x 1 0 0 8968 Feb 26 19:06 a.out drwxr-xr-x 2 0 0 4096 Feb 26 18:59 directory -rw-r--r-- 1 0 0 85 Feb 26 19:05 test.cpp -rw-r--r-- 1 0 0 41 Feb 26 19:06 test.hpp -rw-r--r-- 1 0 0 0 Feb 26 19:04 test.sh [root@hcss-ecs-d3d5 test]#
-F
选项
加上特殊符号来表示文件类型
/
表示目录@
表示符号链接*
表示普通可执行文件
[root@hcss-ecs-d3d5 test]# ls -F a.out* directory/ opt@ test.cpp test.hpp test.sh [root@hcss-ecs-d3d5 test]# ls a.out directory opt test.cpp test.hpp test.sh [root@hcss-ecs-d3d5 test]#
-r
对目录进行反向排序:
[root@hcss-ecs-d3d5 test]# ls a.out directory opt test.cpp test.hpp test.sh [root@hcss-ecs-d3d5 test]# ls -r test.sh test.hpp test.cpp opt directory a.out [root@hcss-ecs-d3d5 test]#
-s
选项
打印每个文件的分配大小(实际占用大小?)
[root@hcss-ecs-d3d5 test]# ls -s total 24 12 a.out 4 directory 0 opt 4 test.cpp 4 test.hpp 0 test.sh [root@hcss-ecs-d3d5 test]#
-R
选项
列出子目录下的所有文件
[root@hcss-ecs-d3d5 test]# ls -R .: a.out dir_1 opt test.cpp test.hpp test.sh ./dir_1: dir_1_1 dir_1_2 dir_1_3 dir_1_4 ./dir_1/dir_1_1: ./dir_1/dir_1_2: ./dir_1/dir_1_3: ./dir_1/dir_1_4: [root@hcss-ecs-d3d5 test]#
-1
选项
每行输出一个文件:
对比 ls -al
, ls -1
只输出了文件名, 而没有详细信息.
[root@hcss-ecs-d3d5 test]# ls -1 a.out dir_1 opt test.cpp test.hpp test.sh [root@hcss-ecs-d3d5 test]# ls -l total 24 -rwxr-xr-x 1 root root 8968 Feb 26 19:06 a.out drwxr-xr-x 6 root root 4096 Feb 27 12:49 dir_1 lrwxrwxrwx 1 root root 4 Feb 27 12:43 opt -> /opt -rw-r--r-- 1 root root 85 Feb 26 19:05 test.cpp -rw-r--r-- 1 root root 41 Feb 26 19:06 test.hpp -rw-r--r-- 1 root root 0 Feb 26 19:04 test.sh [root@hcss-ecs-d3d5 test]#
pwd
指令
作用: 查看当前所在的目录(绝对路径)
语法:
pwd
例:
[root@hcss-ecs-d3d5 dir_1]# pwd /root/test/dir_1 [root@hcss-ecs-d3d5 dir_1]#
cd
指令
作用: 切换当前工作目录(Change the current directory. )
语法:
cd [目录]
例:
# 使用相对路径, 返回父级目录 [root@hcss-ecs-d3d5 test]# cd .. # 返回上次访问的目录 [root@hcss-ecs-d3d5 ~]# cd - /root/test # 使用相对路径进入当前目录下的一个一个一个子目录 [root@hcss-ecs-d3d5 test]# cd ./dir_1/ # 使用绝对路径进入一个一个一个目录 [root@hcss-ecs-d3d5 dir_1]# cd /root/test [root@hcss-ecs-d3d5 test]#
touch
指令
mkdir
指令
作用:创建目录
语法:
mkdir -[选项] [目录名]
常用选项: -p
(-parents
), 此时若路径中一些目录沿不存在, 加上此选项后会自动创建.
例:
# 此时目录中并不存在 dir_1_5 [root@hcss-ecs-d3d5 dir_1]# ls -al total 24 drwxr-xr-x 6 root root 4096 Feb 27 12:49 . drwxr-xr-x 3 root root 4096 Feb 27 13:23 .. drwxr-xr-x 2 root root 4096 Feb 27 12:49 dir_1_1 drwxr-xr-x 2 root root 4096 Feb 27 12:49 dir_1_2 drwxr-xr-x 2 root root 4096 Feb 27 12:49 dir_1_3 drwxr-xr-x 2 root root 4096 Feb 27 12:49 dir_1_4 # 不带选项 -p会报No such file or directory错误 [root@hcss-ecs-d3d5 dir_1]# mkdir dir_1_5/dir_1_5_1 mkdir: cannot create directory ‘dir_1_5/dir_1_5_1’: No such file or directory # 带上选项 -p 之后会自动创建缺失目录 [root@hcss-ecs-d3d5 dir_1]# mkdir -p dir_1_5/dir_1_5_1 [root@hcss-ecs-d3d5 dir_1]# ls -al total 28 drwxr-xr-x 7 root root 4096 Feb 27 23:05 . drwxr-xr-x 3 root root 4096 Feb 27 13:23 .. drwxr-xr-x 2 root root 4096 Feb 27 12:49 dir_1_1 drwxr-xr-x 2 root root 4096 Feb 27 12:49 dir_1_2 drwxr-xr-x 2 root root 4096 Feb 27 12:49 dir_1_3 drwxr-xr-x 2 root root 4096 Feb 27 12:49 dir_1_4 drwxr-xr-x 3 root root 4096 Feb 27 23:05 dir_1_5 [root@hcss-ecs-d3d5 dir_1]# ls dir_1_5/ dir_1_5_1 [root@hcss-ecs-d3d5 dir_1]#
rmkdir
指令
作用: rmdir
指令与 mkdir
对应, 其作用为删除目录
语法:
rmdir -[选项] [要删除的目录]
常用选项: -p
如果删除了该目录下所有的文件和目录, 则把该目录也删除
例:
[root@hcss-ecs-d3d5 dir_1]# ls dir_1_1 dir_1_2 dir_1_3 dir_1_4 dir_1_5 [root@hcss-ecs-d3d5 dir_1]# ls dir_1_5/ test # 删除dir_1_5目录下的所有文件片目录后, 也会将该目录删除 # 如果当前就在该目录中, 则该目录不会被删除. [root@hcss-ecs-d3d5 dir_1]# rmdir -p dir_1_5/test/ [root@hcss-ecs-d3d5 dir_1]# ls dir_1_1 dir_1_2 dir_1_3 dir_1_4 [root@hcss-ecs-d3d5 dir_1]#
rm
指令
作用: 删除目录和文件
语法:
rm -[选项] [要删除的文件或目录]
常用选项:
-
-r
: 递归删除 -
-f
: 强制删除 -
-i
: 删除时逐一询问是否确认删除
例:
[root@hcss-ecs-d3d5 test]# ls dir_1 dir.zip opt test.cpp test.hpp test.sh # 强制删除当前目录下的所有文件, 逐一写yes即可删除, 键入y亦可删除 [root@hcss-ecs-d3d5 test]# rm -rfi ./* rm: descend into directory ‘./dir_1’? yes rm: remove directory ‘./dir_1/dir_1_4’? yes rm: remove directory ‘./dir_1/dir_1_2’? yes rm: remove directory ‘./dir_1/dir_1_1’? yes rm: remove directory ‘./dir_1/dir_1_3’? yes rm: remove directory ‘./dir_1’? yes rm: remove regular file ‘./dir.zip’? yes rm: remove symbolic link ‘./opt’? yes rm: remove regular file ‘./test.cpp’? yes rm: remove regular file ‘./test.hpp’? yes rm: remove regular empty file ‘./test.sh’? yes # 已经删除成功 [root@hcss-ecs-d3d5 test]# ls [root@hcss-ecs-d3d5 test]# ls -al total 8 drwxr-xr-x 2 root root 4096 Feb 27 23:16 . dr-xr-x---. 8 root root 4096 Feb 27 13:23 .. [root@hcss-ecs-d3d5 test]#
man
指令
cp
指令
mv
指令
cat
指令
more
指令
less
指令
head
指令
tail
指令
时间日期相关指令
find
指令
作用: 查找文件
常用选项:
-name
按名称查找
例:
# 在根目录下, 按名称查找stdio.h文件 [root@hcss-ecs-d3d5 ~]# find / -name "stdio.h" /usr/include/stdio.h /usr/include/c++/4.8.2/tr1/stdio.h /usr/include/bits/stdio.h [root@hcss-ecs-d3d5 ~]#
grep
指令
作用: 过滤文本中的行
常用选项:
-i
忽略大小写
-n
添加行号
-v
反向选择
# Text_Print_1_To_10000.txt, 该文本文件中的内容是Hello World [1 ~ 1000] # 列出所有带有114的行 # 加上-n选项会显示该行在文本中的行数 [root@hcss-ecs-d3d5 Temp]# cat Text_Print_1_To_10000.txt | grep -n 114 114:Hello World [114] 1114:Hello World [1114] 1140:Hello World [1140] 1141:Hello World [1141] 1142:Hello World [1142] 1143:Hello World [1143] 1144:Hello World [1144] 1145:Hello World [1145] 1146:Hello World [1146] 1147:Hello World [1147] 1148:Hello World [1148] 1149:Hello World [1149] 2114:Hello World [2114] 3114:Hello World [3114] 4114:Hello World [4114] 5114:Hello World [5114] 6114:Hello World [6114] 7114:Hello World [7114] 8114:Hello World [8114] 9114:Hello World [9114] [root@hcss-ecs-d3d5 Temp]# # 打印出所有不带9的行 [root@hcss-ecs-d3d5 Temp]# cat Text_Print_1_To_10000.txt | grep -v 9 Hello World [1] Hello World [2] Hello World [3] Hello World [4] Hello World [5] Hello World [6] Hello World [7] Hello World [8] Hello World [10] Hello World [11] Hello World [12] ......
bc
指令(计算器)
作用: 一个轻量级的计算器
例:
[root@hcss-ecs-d3d5 ~]# bc bc 1.06.95 Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc. This is free software with ABSOLUTELY NO WARRANTY. For details type `warranty'. 1 + 1 # 输入 2 # 结果 2.5 - 0.3 2.2 4 * 9 36 9 / 4 2
alias
指令
给命令起别名
which
指令
作用:查找命令所在路径
whereis
作用: 类似于 find
的搜索指令, 用于近似查找.
sort
指令
作用: 按照ASCII码, 为指定的文件按行排序
[root@hcss-ecs-d3d5 Temp]# cat Text_Print_a_To_z_line.txt dddd oooo pppp hhhh dddd xxxx nnnn cccc llll yyyy [root@hcss-ecs-d3d5 Temp]# sort Text_Print_a_To_z_line.txt cccc dddd dddd hhhh llll nnnn oooo pppp xxxx yyyy [root@hcss-ecs-d3d5 Temp]#
uniq
指令
作用: 去除重复的行, 前提是必须排好序
# 也可以用sort -u来实现去重 [root@hcss-ecs-d3d5 Temp]# sort TestUniq.txt | uniq 00000 11111 22222 33333 44444 [root@hcss-ecs-d3d5 Temp]# cat TestUniq.txt 11111 00000 11111 22222 22222 33333 22222 44444 00000 11111 [root@hcss-ecs-d3d5 Temp]#
快捷键
小细节
Linux 下相对根目录的 ../目录还是根目录
命令执行结果如下:
atri@Debian:/$ pwd / atri@Debian:/$ cd ../ atri@Debian:/$ pwd /
virtio
接口:virtual I/O
一般使用租来的云服务器查看磁盘信息时会显示这种磁盘
/dev/vd[a-p] atri@Debian:/$ lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT vda 254:0 0 40G 0 disk └─vda1 254:1 0 40G 0 part / atri@Debian:/$
压缩命令
zip
指令
作用: 压缩, 解压缩文件或目录
语法:
zip -[选项] [目标文件] [源文件] unzip [源文件] -[选项] [目标文件]
常用选项:
zip -r
将目录下的文件和子目录一并处理
uzip -d
选项解压到哪个目录中去
例:
[root@hcss-ecs-d3d5 test]# ls a.out dir_1 opt test.cpp test.hpp test.sh # 把dir_1目录下的文件全部压缩到dir.zip中, 如果不带 -r 选项, 压缩的就是一个空目录dir_1 [root@hcss-ecs-d3d5 test]# zip -r dir.zip dir_1/ adding: dir_1/ (stored 0%) adding: dir_1/dir_1_4/ (stored 0%) adding: dir_1/dir_1_2/ (stored 0%) adding: dir_1/dir_1_1/ (stored 0%) adding: dir_1/dir_1_3/ (stored 0%) # 解压到父级目录中 [root@hcss-ecs-d3d5 test]# unzip dir.zip -d ../ Archive: dir.zip creating: ../dir_1/ creating: ../dir_1/dir_1_4/ creating: ../dir_1/dir_1_2/ creating: ../dir_1/dir_1_1/ creating: ../dir_1/dir_1_3/ [root@hcss-ecs-d3d5 test]#
tar
指令
功能: 打包, 压缩文件
例:
打包并压缩文件
[root@hcss-ecs-d3d5 documents]# ls codes # -z: 使用gzip压缩 # -c: 打包 # -v: 显示过程 # -f: 指定压缩后压缩包的文件名, 这里为code.tar.gz [root@hcss-ecs-d3d5 documents]# tar -zcvf codes.tar.gz codes/ codes/ codes/test.c codes/main.c codes/test.h [root@hcss-ecs-d3d5 documents]# ls codes codes.tar.gz
解包并解压
[root@hcss-ecs-d3d5 temp]# ls codes.tar.gz [root@hcss-ecs-d3d5 temp]# mkdir this [root@hcss-ecs-d3d5 temp]# ls codes.tar.gz this # -x 解压 # -C 指定要解压到的目录 [root@hcss-ecs-d3d5 temp]# tar -xvf codes.tar.gz -C ./this/ codes/ codes/test.c codes/main.c codes/test.h [root@hcss-ecs-d3d5 temp]# tree ./ ./ ├── codes.tar.gz └── this └── codes ├── main.c ├── test.c └── test.h 2 directories, 4 files [root@hcss-ecs-d3d5 temp]#
7z
指令
压缩文件
7z a -p密码 -mhe=on 压缩文件名.7z 要压缩的文件或目录
参数说明:
a
:添加文件到压缩包。-p密码
:设置压缩包的密码,将密码
替换为你的实际密码。-mhe=on
:启用加密文件名功能。压缩文件名.7z
:生成的压缩文件名。要压缩的文件或目录
:要压缩的文件或目录路径。
查看系统信息
uname
指令
作用: 打印出系统相关信息( uname - print system information)
语法:
uname -[选项]
常用选项:
-a
(--all
)输出所有信息.
[root@hcss-ecs-d3d5 ~]# uname Linux [root@hcss-ecs-d3d5 ~]# uname -a Linux hcss-ecs-d3d5 3.10.0-1160.119.1.el7.x86_64 #1 SMP Tue Jun 4 14:43:51 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux [root@hcss-ecs-d3d5 ~]#
lscpu
指令
作用: 查看 cpu
信息
例:
[root@hcss-ecs-d3d5 Temp]# lscpu Architecture: x86_64 CPU op-mode(s): 32-bit, 64-bit Byte Order: Little Endian CPU(s): 2 On-line CPU(s) list: 0,1 Thread(s) per core: 2 Core(s) per socket: 1 Socket(s): 1 NUMA node(s): 1 Vendor ID: AuthenticAMD CPU family: 25 Model: 1 Model name: General Purpose Processor Stepping: 1 CPU MHz: 2449.840 BogoMIPS: 4899.68 Hypervisor vendor: KVM Virtualization type: full L1d cache: 32K L1i cache: 32K L2 cache: 512K L3 cache: 32768K NUMA node0 CPU(s): 0,1 Flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx mmxext fxsr_opt pdpe1gb rdtscp lm constant_tsc art rep_good nopl nonstop_tsc extd_apicid eagerfpu pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm cmp_legacy cr8_legacy abm sse4a misalignsse 3dnowprefetch osvw topoext perfctr_core invpcid_single ssbd ibrs ibpb stibp vmmcall fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid rdseed adx smap clflushopt clwb sha_ni xsaveopt xsavec xgetbv1 clzero xsaveerptr arat umip vaes vpclmulqdq spec_ctrl intel_stibp arch_capabilities
lsmem
指令
作用: 查看内存信息
例:
[root@hcss-ecs-d3d5 Temp]# lsmem RANGE SIZE STATE REMOVABLE BLOCK 0x0000000000000000-0x0000000007ffffff 128M online no 0 0x0000000008000000-0x000000000fffffff 128M online yes 1 0x0000000010000000-0x0000000017ffffff 128M online no 2 0x0000000018000000-0x000000001fffffff 128M online yes 3 0x0000000020000000-0x000000004fffffff 768M online no 4-9 0x0000000050000000-0x0000000057ffffff 128M online yes 10 0x0000000058000000-0x000000007fffffff 640M online no 11-15 Memory block size: 128M Total online memory: 2G Total offline memory: 0B [root@hcss-ecs-d3d5 Temp]#
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架