15 个常用高效率 Linux 命令汇总
发布时间:
本文字数:1,340 字 阅读完需:约 3 分钟
![15 个常用高效率 Linux 命令汇总]
本文提供了 Linux 中最常用的 15 个命令的常用用法。
这并不全面,但可以让你快速了解一些常见的 Linux 命令,从而一只脚跨入 Linux 的大门。
如果你觉得还有其他常用的 Linux 命令,可以在评论区补充。
1 grep
在文件中搜索给定字符串:
$ grep -i -n -w -C 3 "the" filename
-i:不区分大小写
-n:打印行号
-w:只输出完整单词(比如只考虑 the,不考虑 they)
-C:指定显示匹配项前后的行数
根据特定模式匹配,在一系列文件中搜索:
$ grep "this" demo_*
demo_file:this line is the 1st lower case line in this file.
demo_file1:Two lines above this line is empty.
demo_file2:And this is the last line.
使用 demo_*
匹配到 demo_file
, demo_file1
, demo_file2
三个文件。
匹配文件中的正则表达式:
$ grep "want.*go" demo_file
They want to go shopping.
I want to go swimming.
grep 还支持使用正则表达式来搜索字符串,比如上面的语句就表示搜索以 want
开头,以 go
结尾的模式。
常用正则表达式可以查看这篇文章。
[程序员阿德:超简单正则表达式入门教程29 赞同 · 2 评论文章![]
递归搜索所有文件:
$ grep -r "the" *
反向匹配,即只显示没有匹配上的行:
$ grep -v "the" filename
2 find
使用文件名查找文件:
$ find -name test.cpp
使用文件名查找文件(不区分大小写):
$ find -iname test.cpp
指定查找路径:
$ find dirname/ -iname test.cpp
反向匹配,即查找不是指定名称的文件和目录:
$ find -not -iname test.cpp
根据文件类型查找文件:
需要使用 -type
选项,只查找普通文件(f),只查找所有目录(d)。
$ find . -type f
查找最大的 5 个文件:
$ find . -type f -exec ls -s {} \; sort -n -r head -5
想查找最小的文件,将 -r
去掉即可。
3 ps
查看当前正在运行的进程信息:
$ ps -ef more
以树状结构查看当前正在运行的进程信息:
$ ps -efH more
-H
:代表进程层次结构
4 free
显示系统中空闲内存、已用内存、交换内存等内存信息。默认输出以 KB 为单位显示。
$ free
total used free shared buff/cache available
Mem: 8208948 6610388 1369208 17720 229352 1464828
Swap: 25165824 1199792 23966032
如果认为字节显示不方便,可以通过指定参数选择其他单位:-g 表示GB,-k 表示KB,-m 表示MB。
查看总内存(包括交换内存):
$ free -t
total used free shared buff/cache available
Mem: 8208948 6425468 1554128 17720 229352 1649748
Swap: 25165824 1202472 23963352
Total: 33374772 7627940 25517480
5 df
显示文件系统磁盘空间使用情况,默认情况下 df -k
以字节为单位显示输出。
$ df -k
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda1 29530400 3233104 24797232 12% /
/dev/sda2 120367992 50171596 64082060 44% /home
df -h
以我们更方便阅读的形式显示输出,即大小将以 GB 显示。
$ df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 29G 3.1G 24G 12% /
/dev/sda2 115G 48G 62G 44% /home
使用 -T 选项显示文件系统的类型。
$ df -T
Filesystem Type 1K-blocks Used Available Use% Mounted on
/dev/sda1 ext4 29530400 3233120 24797216 12% /
/dev/sda2 ext4 120367992 50171596 64082060 44% /home
6 tar
创建一个 tar 压缩包:
$ tar cvf archive_name.tar dirname/
-c:创建一个新的压缩包
-v:压缩时详细列出文件名(可选)
-f:指定压缩包名
从现有的 tar 压缩包中提取:
$ tar xvf archive_name.tar
查看 tar 压缩包中的文件:
$ tar tvf archive_name.tar
7 kill
使用 kill 命令终止进程。首先使用 ps -ef 命令获取进程 id,然后使用 kill -9 杀死正在运行的 Linux 进程。
$ ps -ef grep vim
ramesh 7243 7222 9 22:43 pts/2 00:00:00 vim
$ kill -9 7243
8 rm
$ rm -i filename.txt
-i:在删除文件之前获得确认(非常有用的一个参数,防止误删)
递归删除目录下的所有文件和目录:
$ rm -rf dirname/
-r:递归删除
-f:强制删除
9 cp
将 file1 复制到 file2,并保留模式、所有权和时间戳。
$ cp -p file1 file2
将 file1 复制到 file2,如果 file2 存在,则在覆盖前提示确认。
$ cp -i file1 file2
10 mv
移动文件到新路径:
$ mv -vi filename dirname/
将 file1 重命名为 file2,如果 file2 存在,则在覆盖前提示确认:
$ mv -vi file1 file2
-i:覆盖前提示确认
-f:不提示直接覆盖
-v:显示文件重命名过程
$ mv -v 1.txt 01/
renamed '1.txt' -> '01/1.txt'
11 cat
你可以在不打开文件的情况下,查看文件内容,甚至可以同时查看多个文件内容:
$ cat file1 file2
查看文件内容时,显示行号:
$ cat -n filename
查看文件前两行:
cat -n filename head -n 2
查看文件后两行:
cat -n filename tail -n 2
三种方法显示文件10行到50行内容:
cat filename head -n 50 tail -n +10
cat filename tail -n +10 head -n 41
sed -n '100,500p' filename
12 tail
查看文件最后 n 行(默认 n 为 10):
tail -n N filename
实时查看文件内容:
$ tail -f filename
这对于查看不断增长的日志文件很有用。可以使用 CTRL-C 终止该命令。
13 less
less 在查看巨大的日志文件时非常高效,因为它不需要在打开时加载完整的文件。
less filename
14 ifconfig
使用 ifconfig 命令查看或配置 Linux 系统上的网络接口。
查看所有网络接口以及状态:
$ ifconfig -a
启动或停止特定网络接口。
$ ifconfig eth0 up
$ ifconfig eth0 down
15 shutdown
关闭系统并立即关闭电源:
shutdown -h now
10 分钟后关闭系统:
$ shutdown -h +10
重启系统:
$ shutdown -r now