Linux命令汇总

Linux基本命令的使用

Linux文件系统 和 Windows文件系统有一些区别,Windows是分C盘、D盘、E盘…的,但是在Linux中是有一个最大的目录,称之为根目录,用 / 表示,根目录下面会有很多子目录,这些子目录其实可以理解为windows中C盘、D盘、E盘。
bin 存放二进制可执行文件(ls,cat,mkdir等)
boot 存放用于系统引导时使用的各种文件
dev 存放设备文件
etc 存放系统配置文件
home 存放所有用户文件的根目录
lib 存放跟文件系统中的程序运行所需要的共享库及内核模块
proc 虚拟文件系统,存放当前内存的映射
usr 存放系统应用程序,比较重要的目录/usr/local 管理员软件安装目录
var 存放运行时需要改变数据的文件
mnt 挂载目录
sbin 存储管理级别的二进制执行文件
root 超级用户主目录
opt 额外安装的可选应用程序包安装位置

文件查看

pwd

pwd, 列出当前目录的路径,查看当前所在的路径。
注意,~表示是用户家目录,目前我们登陆的是root用户,root用户的家目录是/root

[root@localhost ~]# pwd 
/root
复制代码

ls

ls 列出当前目录下的所有文件

[root@localhost ~]# ls
anaconda-ks.cfg
复制代码

ll

(ls -l缩写) 列出当前目录下的文件(带文件信息)

[root@localhost ~]# ll 
total 4  
-rw-------. 1 root root 1243 Mar 28 20:59 anaconda-ks.cfg
[root@localhost ~]# ls -l 
total 4 
-rw-------. 1 root root 1243 Mar 28 20:59 anaconda-ks.cfg
复制代码

ll -a 列出当前目录下的所有文件(包括隐藏文件)

[root@localhost ~]# ll -a 
total 28 
dr-xr-x---. 2 root root 135 Mar 28 21:00 . 
dr-xr-xr-x. 17 root root 224 Mar 28 20:58 .. 
-rw-------. 1 root root 1243 Mar 28 20:59 anaconda-ks.cfg 
-rw-------. 1 root root 8 Mar 28 21:00 .bash_history 
-rw-r--r--. 1 root root 18 Dec 29 2013 .bash_logout 
-rw-r--r--. 1 root root 176 Dec 29 2013 .bash_profile 
-rw-r--r--. 1 root root 176 Dec 29 2013 .bashrc 
-rw-r--r--. 1 root root 100 Dec 29 2013 .cshrc 
-rw-r--r--. 1 root root 129 Dec 29 2013 .tcshrc
复制代码

创建、重命名文件\文件夹

touch

touch filename 创建空文件

创建空文件hello.txt

[root@localhost ~]# touch hello.txt 
[root@localhost ~]# ll 
total 4 
-rw-------. 1 root root 1243 Mar 28 20:59 anaconda-ks.cfg 
-rw-r--r--. 1 root root 0 Mar 28 22:21 hello.txt
复制代码

mkdir

mkdir 创建目录
创建目录abc

[root@localhost ~]# mkdir abc 
[root@localhost ~]# ll 
total 4 
drwxr-xr-x. 2 root root 6 Mar 28 22:22 abc 
-rw-------. 1 root root 1243 Mar 28 20:59 anaconda-ks.cfg 
-rw-r--r--. 1 root root 0 Mar 28 22:21 hello.txt
复制代码

mkdir -p 目标目录存在也不报错

当我们在创建目录时不确定这个目录是否已存在的时候,可以使用-p参数, 就算目录已存在也不会报错,如果不指定-p参数会报错,会提示目录已存在
创建目录abc

[root@localhost ~]# mkdir -p abc 
[root@localhost ~]# ll 
total 4 
drwxr-xr-x. 2 root root 6 Mar 28 22:22 abc 
-rw-------. 1 root root 1243 Mar 28 20:59 anaconda-ks.cfg 
-rw-r--r--. 1 root root 0 Mar 28 22:21 hello.txt [root@localhost ~]# mkdir abc 
mkdir: cannot create directory ‘abc’: File exists
复制代码

mv

mv 重命名文件\文件夹
修改目录abc的名称为abx

[root@localhost ~]# ll 
total 4 
drwxr-xr-x. 2 root root 6 Mar 28 22:22 abc 
-rw-------. 1 root root 1243 Mar 28 20:59 anaconda-ks.cfg 
-rw-r--r--. 1 root root 0 Mar 28 22:21 hello.txt 
[root@localhost ~]# mv abc abx 
[root@localhost ~]# ll total 4 
drwxr-xr-x. 2 root root 6 Mar 28 22:22 abx 
-rw-------. 1 root root 1243 Mar 28 20:59 anaconda-ks.cfg 
-rw-r--r--. 1 root root 0 Mar 28 22:21 hello.txt
复制代码

链接文件

linux有两种链接:硬链接、符号(软)链接 软链接功能类似类似于windows的快捷方式,主要用于节省磁盘空间。

ln

首先看硬链接:硬链接相当于对原始文件的一个复制,不能对目录使用硬链接。
命令如下:
ln hello.txt hlink

[root@localhost ~]# ll 
total 4 
drwxr-xr-x. 2 root root 6 Mar 28 22:22 abx 
-rw-------. 1 root root 1243 Mar 28 20:59 anaconda-ks.cfg 
-rw-r--r--. 1 root root 0 Mar 28 22:21 hello.txt   
[root@localhost ~]# ln hello.txt hlink 
[root@localhost ~]# ll total 4 
drwxr-xr-x. 2 root root 6 Mar 28 22:22 abx 
-rw-------. 1 root root 1243 Mar 28 20:59 anaconda-ks.cfg 
-rw-r--r--. 2 root root 0 Mar 28 22:21 hello.txt 
-rw-r--r--. 2 root root 0 Mar 28 22:21 hlink
复制代码

ln -s

再看符号(软)链接:
如果想使用软连接,需要添加-s,相当于快捷方式,不能删除原文件
命令如下:
ln -s hello.txt vlink

[root@localhost ~]# ll 
total 4 
drwxr-xr-x. 2 root root 6 Mar 28 22:22 abx 
-rw-------. 1 root root 1243 Mar 28 20:59 anaconda-ks.cfg 
-rw-r--r--. 2 root root 0 Mar 28 22:21 hello.txt 
-rw-r--r--. 2 root root 0 Mar 28 22:21 hlink   
[root@localhost ~]# ln -s hello.txt vlink   
[root@localhost ~]# ll 
total 4 
drwxr-xr-x. 2 root root 6 Mar 28 22:22 abx 
-rw-------. 1 root root 1243 Mar 28 20:59 anaconda-ks.cfg 
-rw-r--r--. 2 root root 0 Mar 28 22:21 hello.txt 
-rw-r--r--. 2 root root 0 Mar 28 22:21 hlink lrwxrwxrwx. 1 root root 9 Mar 28 22:33 vlink -> hello.txt
复制代码

切换目录

cd

cd . 当前目录
一个.表示当前目录

[root@localhost ~]# pwd 
/root 
[root@localhost ~]# cd . 
[root@localhost ~]# pwd 
/root
复制代码

cd .. 去上一级目录
两个.. 表示上一级目录

[root@localhost ~]# pwd 
/root 
[root@localhost ~]# cd .. 
[root@localhost /]# pwd 
/
复制代码

cd / 去根目录
通过在cd后面指定目录,可以切换到指定目录

[root@localhost /]# cd / 
[root@localhost /]# pwd 
/ 
[root@localhost ~]# cd /bin/ 
[root@localhost bin]# pwd 
/bin
复制代码

cd ~ 去当前用户主(家)目录

[root@localhost bin]# cd ~ 
[root@localhost ~]# pwd 
/root
复制代码

cd xxx/xxx 直接跳转到某个目录

[root@localhost ~]# cd abx/test/ 
[root@localhost test]# pwd 
/root/abx/test
复制代码

删除文件\文件夹(目录)

rm命令可以删除文件或者目录,也可以将某个目录及其下属的所有文件及其子目录均删除掉
对于链接文件,只是删除整个链接文件,而原有文件保持不变。

rm

rm 删除文件
删除文件,但是会有提示确认对话,输入y确认删除!

[root@localhost test]# touch abc.txt 
[root@localhost test]# ll 
total 0 
-rw-r--r--. 1 root root 0 Mar 29 13:53 abc.txt 
[root@localhost test]# rm abc.txt 
rm: remove regular empty file ‘abc.txt’? y       
[root@localhost test]# ll 
total 0
复制代码

rm -r 删除目录,需要确认
删除目录需要指定r参数,否则会提示不能删除
r是给rm加入递归(recursion)特性,也就是目标为文件夹时删除文件夹下所有数据
使用rm -r 在删除目录的时候也会有提示确认对话,输入y确认删除

[root@localhost abx]# ll 
total 0 
drwxr-xr-x. 2 root root 6 Mar 29 14:01 test 
[root@localhost abx]# rm test 
rm: cannot remove ‘test’: Is a directory 
[root@localhost abx]# rm -r 
test rm: remove directory ‘test’? y 
[root@localhost abx]# ll total 0
复制代码

rm -f 强制删除

f给rm加入强制(force)特性,也就是遇到删除时不需要询问即可直接删除
注意:这个操作还是比较危险的,建议慎用,因为删除之后就找不到了
Linux系统中没有回收站

[root@localhost abx]# touch a.txt 
[root@localhost abx]# ll 
total 0 
-rw-r--r--. 1 root root 0 Mar 29 14:03 a.txt 
[root@localhost abx]# rm -f a.txt 
[root@localhost abx]# ll 
total 0
复制代码

rm -rf 递归删除目录及其文件
Linux中最危险的操作,最具破坏性
rf参数可以强制递归删除任何数据,并且没有任何提示,慎用!慎用!慎用!

[root@localhost ~]# ll 
drwxr-xr-x. 2 root root 6 Mar 29 14:03 abx 
-rw-------. 1 root root 1243 Mar 28 20:59 anaconda-ks.cfg 
[root@localhost ~]# mkdir -p abx/test/aaa 
[root@localhost ~]# cd abx/test/aaa/ 
[root@localhost aaa]# touch a.txt 
[root@localhost aaa]# cd ~ 
[root@localhost ~]# rm -rf abx 
[root@localhost ~]# ll 
-rw-------. 1 root root 1243 Mar 28 20:59 anaconda-ks.cfg
复制代码

复制\粘贴\剪切

cp

cp 复制&粘贴文件
复制hello.txt文件,复制后的文件名为hello-bak.txt

[root@localhost ~]# ll 
-rw-r--r--. 1 root root 0 Mar 28 22:21 hello.txt
[root@localhost ~]# cp hello.txt hello-bak.txt
[root@localhost ~]# ll 
-rw-r--r--. 1 root root 0 Mar 29 14:20 hello-bak.txt 
-rw-r--r--. 1 root root 0 Mar 28 22:21 hello.txt
复制代码

cp -r

cp -r 复制&粘贴文件或目录
复制目录,需要指定r参数

[root@localhost ~]# mkdir abc 
[root@localhost ~]# ll 
drwxr-xr-x. 2 root root 6 Mar 29 14:21 abc
[root@localhost ~]# cp abc xyz 【错误用法,复制目录必须指定-r参数】 cp: omitting directory ‘abc’ 
[root@localhost ~]# cp -r abc xyz 
[root@localhost ~]# ll 
drwxr-xr-x. 2 root root 6 Mar 29 14:21 abc 
drwxr-xr-x. 2 root root 6 Mar 29 14:21 xyz
复制代码

mv

mv 移动(剪切)文件或目录
将目录xyz移动到目录abc下面

[root@localhost ~]# ll 
drwxr-xr-x. 2 root root 6 Mar 29 14:21 abc 
drwxr-xr-x. 2 root root 6 Mar 29 14:21 xyz 
[root@localhost ~]# ll abc/ 
total 0 
[root@localhost ~]# mv xyz abc 
[root@localhost ~]# ll abc/ 
drwxr-xr-x. 2 root root 6 Mar 29 14:21 xyz
复制代码

远程复制

scp

scp命令用于在网络中不同主机之间复制文件或目录。scp是有Security的文件copy,基于ssh登录,如果没有配置免密码登陆,需要输入主机密码。

scp jdk-8u191-linux-x64.tar.gz root@121.4.40.241:/home/software
复制代码

提示:
显示进度在scp后添加-v
复制目录在scp后添加-r
静默复制模式在scp后添加-q

scp -rq /root/abc root@192.168.182.130:/
复制代码

文件属性

第一段:权限
其余字符每3个一组(rwx),读(r)、写(w)、执行(x)
第一组:文件所有者的权限是读、写和执行
第二组:与文件所有者同一组的用户的权限
第三组:不与文件所有者同组的其他用户的权限
也可用数字表示为:r=4,w=2,x=1,如:权限6可以表示为r+w=6
第二段:目录/链接个数
对于目录文件,表示它的第一级子目录的个数
注意:此处的值要减2才等于该目录下的子目录的实际个数(目录下默认包含.和…这两个目录)
对于其他文件,默认是1
第三段:所属用户
第四段:所属组
第五段:文件大小(字节)
第六段:最后修改时间
第七段:文件\文件夹名称

chmod 分配权限

常见用法:
chmod u+x xxx.txt 给当前所有者添加执行权限【x表示是执行权限】
针对hello.txt文件,给当前用户添加执行权限

[root@localhost ~]# ll 
-rw-r--r--. 1 root root 0 Mar 28 22:21 hello.txt 
[root@localhost ~]# chmod u+x hello.txt 
[root@localhost ~]# ll 
-rwxr--r--. 1 root root 0 Mar 28 22:21 hello.txt
复制代码

chmod 777 xxx.txt 添加rwxrwxrwx权限
给hello.txt添加777权限

[root@localhost ~]# chmod 777 hello.txt 
[root@localhost ~]# ll 
-rwxrwxrwx. 1 root root 0 Mar 28 22:21 hello.txt
复制代码

chmod -R 777 xxx 给指定目录递归添加rwxrwxrwx权限
给abc目录及其子目录中的所有内容添加777权限

[root@localhost ~]# ll 
drwxr-xr-x. 3 root root 17 Mar 29 14:24 abc 
[root@localhost ~]# cd abc/ 
[root@localhost abc]# ll 
total 0 
drwxr-xr-x. 2 root root 6 Mar 29 14:21 xyz 
[root@localhost abc]# cd .. 
[root@localhost ~]# chmod -R 777 abc 
[root@localhost ~]# ll 
drwxrwxrwx. 3 root root 17 Mar 29 14:24 abc 
[root@localhost ~]# cd abc/ 
[root@localhost abc]# ll 
total 0 
drwxrwxrwx. 2 root root 6 Mar 29 14:21 xyz
复制代码

内容查看

cat

cat 显示文本内容,类似windows中的type(顺序输出)

[root@localhost ~]# cat anaconda-ks.cfg 
#version=DEVEL 
#System authorization information 
auth --enableshadow --passalgo=sha512 
......
复制代码

cat -b 显示行号输出

[root@localhost ~]# cat -b anaconda-ks.cfg 
1 #version=DEVEL 
2 # System authorization information 
3 auth --enableshadow --passalgo=sha512 
4 # Use CDROM installation media 
........
复制代码

more

分屏显示 more
用一次显示一屏,没有显示完时最后一行显示进度。回车显示下一行,按b显示上一页,空格显示下一页,q退出

[root@localhost ~]# more anaconda-ks.cfg 
#version=DEVEL 
#System authorization information 
auth --enableshadow --passalgo=sha512 
#Use CDROM installation media 
cdrom 
#Use graphical install graphical 
#Run the Setup Agent on first boot 
firstboot --enable 
ignoredisk --only-use=sda 
#Keyboard layouts 
keyboard --vckeymap=us --xlayouts='us' 
#System language 
lang en_US.UTF-8 
#Network information 
network --bootproto=dhcp --device=ens33 --ipv6=auto --activate 
network --hostname=localhost.localdomain 
#Root password 
rootpw --iscrypted $6$sx2fOWF8AMiHgQTV$ExkpEX6Sq1EfZVHaP4RxfYls3B0o dX2ouFfaTX2S0TttzWz7tX3L3cWRFeb1M4qfGUA2FGzrkylhlGfp4psze. 
--More--(48%)
复制代码

压缩、解压

tar

参数:
-z 是否同时具有 gzip 的属性?亦即是否需要用 gzip 压缩?
-c 创建一个压缩文件的参数指令(create 的意思);
-x 解开一个压缩文件的参数指令!
f 使用档案名字,这个参数是最后一个参数,后面只能接档案名!
注意:特别注意,在参数的下达中, c/x 仅能存在一个!不可同时存在!
压缩:
tar -zcvf 打包及压缩(gzip方式) 将abc目录的内容打包压缩为abc.tar.gz

[root@localhost ~]# ll 
drwxrwxrwx. 3 root root 17 Mar 29 14:24 abc 
[root@localhost ~]# tar -zcvf abc.tar.gz abc
[root@localhost ~]# ll 
drwxrwxrwx. 3 root root 17 Mar 29 14:24 abc 
-rw-r--r--. 1 root root 130 Mar 29 15:24 abc.tar.gz
复制代码

解压:
tar -zxvf 解压缩

输出及显示

echo

echo:将内容输出到设备,类似java里面的 system.out.println()
常见用法:
echo “hello\t\t world!” 不解析转义字符
echo -e “hello\t\t world!” 解析转义字符
echo $PATH 输出环境变量
注意:在打印变量信息的时候,使用echo ${PATH} 也可以,效果是一样的

[root@localhost ~]# echo "hello\t\t world!" 
hello\t\t world! 
[root@localhost ~]# echo -e "hello\t\t world!" 
hello world! 
[root@localhost ~]# echo $PATH 
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin 
[root@localhost ~]# echo ${PATH} 
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
复制代码

软件安装和卸载

第一种:压缩包安装方式,直接解压,配置相应的环境变量即可使用
第二种:在线安装,使用yum
yum集成了连接网络,软件安装,删除,更新等功能,yum在配置好repo后,机器只要连网,就能智能化安装软件,使用yum 安装的好处在于可以自动安装软件需要的依赖包
安装

yum

yum install -y 安装
yum install xxx 如果安装的软件有询问会卡在询问页,如果希望安装过程自己很清楚的可以使用此命令;
yum –y install xxx 使用这个就不会老询问你要不要yes他会自动的同意,也就不需要你老确认。
升级
yum update 不跟则更新全部
查找和显示
yum info 显示包信息
yum list 不跟则显示已安装或可安装包
删除程序
yum remove
清除缓存
yum clean all 清除所有缓存(包含文件、旧软件)
在这里大家先了解yum的基本语法,等后面具体使用到的时候我们再演示。

查看操作历史

history

history保留了最近执行的命令记录,默认可以保留1000。
历史清单从0开始编号到最大值。
常见用法:
history N 显示最近N条命令
history -c 清除所有的历史记录
history -w xxx.txt 保存历史记录到文本xxx.txt

[root@localhost ~]# history 10
142 ll 
143 tar -zxvf abc.tar.gz 
144 ll 
145 cd . 
146 ll 
147 echo "hello\t\t world!" 
148 echo -e "hello\t\t world!" 
149 env 
150 echo $PATH 
151 history 10 
[root@localhost ~]# history -w his.txt 
[root@localhost ~]# ll -rw-------. 1 root root 1395 Mar 29 15:41 his.txt 
[root@localhost ~]#
more his.txt 
ip addr 
ls / 
pwd 
ls 
ll 
..........
复制代码

磁盘使用情况

df

使用df命令查看硬盘使用情况
查看磁盘使用情况

查看内存使用情况

free

free 查看内存和交换空间的使用情况
常见用法:
free -m:显示内存单位为MB
free -h:根据值的大小,显示易于识别的单位

Linux高级命令的使用

vi文件编辑利器的使用

如果想编辑文件,需要按键盘上的i键,进入编辑模式。
文件编辑好了以后我们想要保存,此时需要按键盘左上角的esc键,退出编辑模式,此时即进入了命令模式(命令模式其实就是我们最开始进入的那个不可编辑的模式)
在命令模式下按shift和: ,最后输入wq
这里的w是write的缩写,表示写入的意思
q是quit的缩写,表示退出的意思
快速查找:
在命令模式下,输入/,然后再输入你想要查询的字符串,最后按回车键就可以进行查询了,再按n 代表查找下一个。
跳到某一行:
然后按shift和:,在这里输入具体的行号然后按回车即可。
显示行号:
按shift和: 然后输入 set nu 这个时候就可以看到文件中显示了行号。
快速复制粘贴:
连按yy,这样就把这一行内容复制上了,然后按p就会把刚才复制的内容粘贴到下一行,按一次p粘贴一行,一直按到你喊停为止。
最后按shift和: 输入wq保存退出即可
快速删除:
进入命令模式,把光标定位到想要删除的那一行内容上面,连按dd,就可以删除当前行的内容。
先连按999,然后再连按dd,这样就可以清空光标所在行之后的所有内容。
快速跳到文件首行和末行:
大写G快速跳到末行,输入小写gg快速跳到首行。
使用vi编辑器遇到的问题:
当你有时候修改一个文件,修改到一半的时候,你出去玩去了,或者你把这命令行直接关闭了,这是因为,没有正确关闭文件导致出现了临时文件,显示如下:

[root@localhost ~]# vi hello.txt
E325: ATTENTION
Found a swap file by the name ".hello.txt.swp"
owned by: root dated: Sun Mar 29 18:14:30 2020
file name: ~root/hello.txt
modified: YES
user name: root host name: localhost.localdomain
process ID: 2158
While opening file "hello.txt"
dated: Sun Mar 29 18:02:43 2020
(1) Another program may be editing the same file. If this is the case,
be careful not to end up with two different instances of the same
file when making changes. Quit, or continue with caution.
(2) An edit session for this file crashed.
If this is the case, use ":recover" or "vim -r hello.txt"
to recover the changes (see ":help recovery").
If you did this already, delete the swap file ".hello.txt.swp"
to avoid this message.
"hello.txt" 6L, 78C
Press ENTER or type command to continue
复制代码

默认和这个原始文件在一个目录下面,只不过它是一个隐藏文件,通过ll命令看不到,这个隐藏文件的后缀名为.swp,我们通过ll -a就可以看到了,找到以后 使用rm删除掉即可。

[root@localhost ~]# ll
total 8
-rw-------. 1 root root 1243 Mar 28 20:59 anaconda-ks.cfg
-rw-r--r--. 1 root root 78 Mar 29 18:02 hello.txt
[root@localhost ~]# ll -a
total 44
dr-xr-x---. 2 root root 174 Mar 29 18:16 .
dr-xr-xr-x. 17 root root 224 Mar 28 20:58 ..
-rw-------. 1 root root 1243 Mar 28 20:59 anaconda-ks.cfg
-rw-------. 1 root root 188 Mar 29 18:14 .bash_history
-rw-r--r--. 1 root root 18 Dec 29 2013 .bash_logout
-rw-r--r--. 1 root root 176 Dec 29 2013 .bash_profile
-rw-r--r--. 1 root root 176 Dec 29 2013 .bashrc
-rw-r--r--. 1 root root 100 Dec 29 2013 .cshrc
-rw-r--r--. 1 root root 78 Mar 29 18:02 hello.txt
-rw-r--r--. 1 root root 12288 Mar 29 18:14 .hello.txt.swp
-rw-r--r--. 1 root root 129 Dec 29 2013 .tcshrc
[root@localhost ~]# rm -rf .hello.txt.swp
复制代码

文件内容统计相关命令

wc

-c是表示获取文件内容的字节数量
-m表示获取字符数量
-l表示是行数
-L是获取最长的一行内容的长度
-w 表示文件中单词的个数,默认使用空白符切割

uniq

检查重复的行列

[root@localhost ~]# uniq hello.txt 
hello world!
复制代码

再看一下-c参数,这个参数表示在输出行的前面加上数据在文件中重复出现的次数

[root@localhost ~]# uniq -c hello.txt 
6 hello world!
复制代码

还有一个-u参数,表示返回文件中不重复的行,针对hello.txt这个文件返回的是空,因为这个文件中的几行内容都是重复的。

[root@localhost ~]# uniq -u hello.txt
复制代码

我们在这个文件中随便添加一行内容,再执行一下看看结果

[root@localhost ~]# cat hello.txt 
hello world! 
hello world! 
hello world! 
hello world! 
hello world! 
hello world! 
abc 
[root@localhost ~]# uniq -u hello.txt 
abc
复制代码

接下来来演示一个坑,在工作中很容易掉进去。
我重新创建一个文件,test.txt。我想要对这个文件中的内容进行去重,最后返回hello和abc

[root@localhost ~]# cat test.txt 
hello 
hello 
abc 
hello 
hello
复制代码

在这执行uniq test.txt 【对连续相同的数据行进行去重】

[root@localhost ~]# uniq test.txt 
hello 
abc 
hello
复制代码

最终显示的结果并不是我想要的
目前使用uniq test.txt这个命令最终获取的结果有问题是因为文件中的数据没有排序,相同的数据不是连续在一起的,uniq只能对连续在一起的重复内容进行去重,那我们想要利用这个特性的话就需要先对数据进行排序,再去重,这个时候就需要用到一个新朋友了,管道命令,管道命令很简单,就是一个 |通过管道可以把前面一个命令的输出结果传递给后面一个命令这样就可以获取到我们需要的结果了。

[root@localhost ~]# sort test.txt | uniq 
abc 
hello
复制代码

sort

sort命令是对数据进行排序的,它后面也支持很多个参数,在这里只讲三个

[root@localhost ~]# cat num.txt 
3 
2 
9 
10 
1
复制代码

首先使用sort命令直接操作这个文件,我们可以看到这个排序是有问题的。

[root@localhost ~]# sort num.txt 
1 
10 
2 
3 
9
复制代码

sort -n 按照数值大小进行排序

[root@localhost ~]# sort -n num.txt 
1 
2 
3 
9 
10
复制代码

这个时候是正序排序,能不能倒序排序呢?当然可以,倒序需要使用-r,单纯使用-r是不行的,需要让n和r同时上场。

[root@localhost ~]# sort -r num.txt 
9 
3 
2 
10 
1 
[root@localhost ~]# sort -nr num.txt 或者 sort -n -r num.txt
10 
9 
3 
2 
1
复制代码

这个文件是比较简单的,实际中会复杂一些,我们来看一个复杂一点的文件 在这里发现使用-n参数是没有用的,不过看文件中的第一列内容其实也是正确的,按照字母的字典顺序排序的。但是我们是希望按照第二列中的数字进行排序的?

[root@localhost ~]# sort -n num2.txt 
aa 9 
ax 1 
bc 2 
dd 7 
xc 15
复制代码

凡事不要慌,我们再来学习一个参数-k 在-n的基础上增加-k 这个参数后面需要指定一个数字,这个数字表示是文件中的第几列,编号从1开始

[root@localhost ~]# sort -k 2 -n num2.txt 
ax 1 
bc 2 
dd 7 
aa 9 
xc 15
复制代码

head

head表示获取前N条数据,默认返回前10条,后面可以通过指定数字来控制返回的数据条数

[root@localhost ~]# cat num.txt 
3 
2 
9 
10 
1 
[root@localhost ~]# head -3 num.txt 
3 
2 
9
复制代码

这样是没有什么意义的,我们想取前几条数据其实就是想取topN,这样直接获取的数据是没有排序的,所以可以把sort和head命令放在一块使用

[root@localhost ~]# sort -nr num.txt 
10 
9 
3 
2 
1 
[root@localhost ~]# sort -nr num.txt | head -3
10
9
3
复制代码

其实我们前面学习的这些命令都可以处理管道传输过来的数据。
例如 cat和sort命令

[root@localhost ~]# cat num.txt | sort -nr 
10 
9 
3 
2 
1
复制代码

日期相关

date

[root@localhost ~]# date 
Sun Mar 29 20:48:15 CST 2026
复制代码

通过%Y,%m,%d这些参数可以对日期进行格式化
date +"%Y-%m-%d %H:%M:%S"
注意:date后面的这些参数中间如果没有空格,可以省略双引号。

[root@localhost ~]# date +"%Y-%m-%d %H:%M:%S" 
2026-03-29 21:06:45
复制代码

这里面的%s表示获取自1970-01-01 00:00:00以来的秒数
date +%s

[root@localhost ~]# date +%s 
1585487600
复制代码

如果想要获取毫秒数呢?不好意思,date命令不支持
注意了,虽然date命令没有直接支持获取毫秒数,但是从秒换算为毫秒也很简单啊,最直接粗暴的方式就是在秒数后面加3个0
date +%s"000"

[root@localhost ~]# date +%s"000" 
1585487796000
复制代码

date命令 提供的有–date这个参数,可以指定时间

[root@localhost ~]# date --date="2026-01-01 00:00:00" 
Wed Jan 1 00:00:00 CST 2026
复制代码

接着在后面指定%s,这个时候获取的时间戳就是指定时间的时间戳了。

[root@localhost ~]# date --date="2026-01-01 00:00:00" +%s 
1577808000
复制代码

在工作中还有一个比较常见的需求,就是获取昨天的日期,这个需求也需要使用–date参数实现
date --date=“1 days ago”

[root@localhost ~]# date --date="1 days ago" 
Sat Mar 28 21:36:37 CST 2026
复制代码

再对返回的结果进行格式化,只获取年月日
date --date=“1 days ago” +%Y-%m-%d

[root@localhost ~]# date --date="1 days ago" +%Y-%m-%d 
2026-03-28
复制代码

假设我想知道今年2月份有多少天,应该怎么做呢?
先指定今年的3月1日,然后再获取前一天的时间,对返回的数据进行格式化,只返回day即可。
date --date=“2026-03-01 1 days ago” +%d

[root@localhost ~]# date --date="2026-03-01 1 days ago" +%d 
29
复制代码

进程相关

ps

ps命令是用来显示进程相关信息的,他的一个典型应用就是在后面跟e和f参数,显示系统内的所有进程
ps -ef 显示所有进程

netstat

netstat也是显示进程相关信息的,只不过可以比ps命令额外显示端口相关的信息。
但是这个命令默认是没有安装的,最方便的方式就是使用yum来在线安装 yum install -y net-tools
netstat的常见用法是 netstat -anp

[root@localhost ~]# netstat -anp | grep 22 
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 
tcp 0 52 192.168.182.132:22 192.168.182.1:51472 ESTABLISH
tcp6 0 0 :::22 :::* LISTEN
复制代码

kill

kill PID:杀掉进程,自杀。
使用kill -9 PID ,可以实现强制杀进程,它杀。

grep

grep 常用于查找文件里符合条件的字符串
我们先演示一个基本的操作,这样可以实现一个简单的过滤查找操作

[root@localhost ~]# cat hello.txt 
hello world!
hello world! 
hello world! 
hello world! 
hello world! 
hello world! 
abc 
[root@localhost ~]# grep abc hello.txt 
abc
复制代码

grep后面跟的这个字符串是可以支持正则表达式的,我们可以把这个调整一下
查询hello.txt中以字母a开头的内容,这样写也是可以的

[root@localhost ~]# grep ^a hello.txt 
abc
复制代码

有时候在查询的时候,我们也忘记需要查询的字符串是大写还是小写了,这个时候可以使用忽略大小写功能。

[root@localhost ~]# grep ABC hello.txt 
[root@localhost ~]# grep -i ABC hello.txt 
abc
复制代码

还有一些场景需要查询出来对应字符串所在的行号,方便我们快速在文件中定位字符串所在的位置,这个也很简单,通过-n参数就可以实现

[root@localhost ~]# grep -i ABC -n hello.txt 
7:abc
复制代码
ps -ef | grep java
复制代码

显示出这个信息其实说明没有找到java进程信息,下面返回的这一行表示是grep进程本身,这样容易给我们造成错觉,想把它去掉,怎么去掉呢?

[root@localhost ~]# ps -ef | grep java 
root 2497 2247 0 22:34 pts/1 00:00:00 grep --color=auto java
复制代码

很简单,使用grep加上-v参数再做一次过滤即可,表示忽略包含指定字符串的数据。

[root@localhost ~]# ps -ef | grep java | grep -v grep
复制代码

sed

用于自动编译一个或者多个文件,简化对文件的反复操作。

注意了,sed 默认不会直接修改源文件数据,而是会将数据复制到缓冲区中,修改也仅限于缓冲区中的数据,最终把缓冲区内的数据输出到控制台。

a参数表示向指定行的下面添加数据, a其实是append的意思。
sed '1a\haha' hello.txt 此操作会将数据添加到第一行下面(也就是第二行的位置)

[root@localhost ~]# cat hello.txt 
hello world!
hello world! 
hello world!
hello world! 
hello world! 
hello world! 
abc 
[root@localhost ~]# sed '2a\haha' hello.txt 
hello world! 
hello world! 
haha 
hello world! 
hello world! 
hello world! 
hello world! 
abc
复制代码

i是insert的意思,可以实现在某一行前插入数据。

[root@localhost ~]# sed '1i\haha' hello.txt 
haha 
hello world! 
hello world! 
hello world! 
hello world! 
hello world! 
hello world! 
abc
复制代码

在这里我们可以通过一个特殊参数 $ ,在这里表示是最后一行的意思。

[root@localhost ~]# sed '$i\haha' hello.txt 
hello world! 
hello world! 
hello world! 
hello world! 
hello world! 
hello world! 
haha 
abc
复制代码

d参数代表删除某一行。

[root@localhost ~]# sed '7d' hello.txt 
hello world! 
hello world! 
hello world! 
hello world! 
hello world! 
hello world!
复制代码

当然了这里也可以使用$参数,删除最后一行,更加便捷

[root@localhost ~]# sed '$d' hello.txt 
hello world!
hello world! 
hello world! 
hello world! 
hello world! 
hello world!
复制代码

以上是增删操作,下面讲下sed的替换操作。
sed后面的参数格式为[address]s/pattern/replacement/flags
s 表示替换操作,pattern 指的是需要替换的内容,replacement 指的是要替换的新内容,flags有多种用法,我们挑两种说一下:
第一种就是flags可以表示为1~512之间的任意一个数字,表示指定要替换的字符串在这一行中出现第几次时才进行替换
第二种就是flags可以直接表示为g,这样的意思就是对每一行数据中所有匹配到的内容全部进行替换
如果flags位置的值为空,则只会在第一次匹配成功时做替换操作

注意了,咱们前面所讲的sed命令的所有操作,在执行之后都不会修改源文件中的内容,这样只能作为测试,如果需要修改源文件的话,其实也很简单,只需要增加一个 -i 参数即可,我们来演示一下

[root@localhost ~]# sed -i '2s/l/a/g' hello.txt 
[root@localhost ~]# cat hello.txt 
hello world! 
heaao worad! 
hello world! 
hello world! 
hello world! 
hello world! 
abc
复制代码

awk

awk是一个强大的文本分析工具,相对于grep的查找,sed的编辑,awk在其对数据分析并生成报告时,显得尤为强大,简单来说awk就是把文件逐行的读入,以空白字符为默认分隔符将每行内容切片,切开的部分再进行各种分析处理。

awk的基本格式:awk [option] programe file
可以在option里面指定字段分隔符,通过-F 参数

[root@localhost ~]# cat hello.txt 
hello world! 
heaao worad! 
hello world! 
hello world! 
hello world! 
hello world! 
abc 
[root@localhost ~]# awk '{print $1}' hello.txt 
hello 
heaao 
hello 
hello 
hello 
hello 
abc 
[root@localhost ~]# awk '{print $2}' hello.txt 
world! 
worad! 
world! 
world!
world! 
world!
[root@localhost ~]# awk '{print $0}' hello.txt 
hello world! 
heaao worad!
hello world! 
hello world! 
hello world!
hello world!
abc
复制代码

linux中还有一个文件 /etc/password 里面存储的是用户信息
但是这个文件中的字段之间是使用:分割的,这个时候想要操作某列字段的话就需要我们手工指定字段分隔符了。
awk -F: ‘{print $1}’ /etc/passwd

[root@localhost ~]# awk -F: '{print $1}' /etc/passwd 
root 
bin 
daemon 
adm 
lp 
sync 
shutdown 
halt
复制代码

awk ‘/world/ {print $1}’ hello.txt 这种写法表示对每次读取到的那一行数据进行匹配

[root@localhost ~]# cat hello.txt 
hello world! 
heaao worad! 
hello world! 
hello world! 
hello world! 
hello world! 
abc 
[root@localhost ~]# awk '/world/ {print $0}' hello.txt 
hello world! 
hello world! 
hello world! 
hello world! 
hello world! 
[root@localhost ~]# awk '/abc/ {print $0}' hello.txt 
abc
复制代码

awk '($1 ~ /world/) {print $1}' hello.txt 在这里面可以通过$来指定具体是哪一列,需要把具体的对比逻辑放到小括号里面

[root@localhost ~]# awk '($1 ~ /world/) {print $0}' hello.txt 
[root@localhost ~]# awk '($2 ~ /world/) {print $0}' hello.txt 
hello world! 
hello world! 
hello world! 
hello world! 
hello world! 
[root@localhost ~]# awk '($2 ~ /wor[a-z]d/) {print $1}' hello.txt 
hello world! 
heaao worad! 
hello world! 
hello world! 
hello world! 
hello world!
复制代码


作者:Java小陈
链接:https://juejin.cn/post/6907573077421490190
来源:掘金
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值