通过学习很多的Linux命令,我觉得它的命令可以这样组成:
命令+参数名+参数值
命令目录
pwd
使用pwd命令找出您所在的当前工作目录(文件夹)的路径。该命令将返回绝对(完整)路径,基本上是所有目录中以正斜杠(/)开头的路径,绝对路径的示例是/root。
演示:
[root@localhost ~]# pwd
/root
cd
要浏览Linux文件和目录,请使用cd命令。它需要完整路径或目录名称,具体取决于您所在的当前工作目录。
演示:
[root@localhost ~]# cd /
[root@localhost /]# pwd
/
ls
ls命令用于查看目录的内容。默认情况下,此命令将显示当前工作目录的内容。如果要查看其他目录的内容,请键入ls,然后键入目录的路径。例如,输入ls /root
查看的内容的文件。
演示:
[root@localhost /]# ls
bin dev file.txt lib media opt root sbin sys usr
boot etc home lib64 mnt proc run srv tmp var
cat
cat是Linux中最常用的命令之一。它用于在标准输出(sdout)上列出文件的内容。要运行此命令,请键入cat,然后输入文件名及其扩展名。例如:cat test.txt
。
[root@localhost /]# cat test.txt
wiqiehd
wdwqrhhfife
ggth
grgrg54efef343498hr
32f43gregregrfwef
eewfregeg
cp
使用cp命令将文件从当前目录复制到另一个目录。
演示:
[root@localhost /]# cp test.txt /root
[root@localhost /]# cd /root
[root@localhost ~]# ls
anaconda-ks.cfg initial-setup-ks.cfg 公共 视频 文档 音乐
file.txt test.txt 模板 图片 下载 桌面
mkdir
使用mkdir命令创建一个新目录-如果键入mkdir Music,它将创建一个名为Music的目录。要在另一个目录中生成新目录,请使用此Linux基本命令mkdir Music / Newfile使用 p选项在两个现有目录之间创建目录。
演示:
[root@localhost ~]# mkdir dir
[root@localhost ~]# ls
anaconda-ks.cfg file.txt test.txt 模板 图片 下载 桌面
dir initial-setup-ks.cfg 公共 视频 文档 音乐
rmdir
如果需要删除目录,请使用rmdir命令。但是,rmdir仅允许您删除空目录。
演示:
[root@localhost ~]# mkdir dir
[root@localhost ~]# ls
anaconda-ks.cfg file.txt test.txt 模板 图片 下载 桌面
dir initial-setup-ks.cfg 公共 视频 文档 音乐
[root@localhost ~]# rmdir dir
[root@localhost ~]# ls
anaconda-ks.cfg initial-setup-ks.cfg 公共 视频 文档 音乐
file.txt test.txt 模板 图片 下载 桌面
rm
该命令用于删除目录以及其中的内容。如果只想删除目录(作为rmdir的替代方法),请使用rm -r
。
不过,使用此命令时要格外小心,并仔细检查您所在的目录。这将删除所有内容,并且没有恢复的选项。
演示:
[root@localhost ~]# mkdir hhh
[root@localhost ~]# ls
anaconda-ks.cfg hhhhh.txt test.txt 视频 下载
file.txt initial-setup-ks.cfg 公共 图片 音乐
hhh name.txt 模板 文档 桌面
[root@localhost ~]# rm -r hhh
rm:是否删除目录 "hhh"?y
[root@localhost ~]#
touch
该触摸命令允许您创建通过Linux命令行新的空白文件。
演示:
[root@localhost ~]# touch hhhhh.txt
[root@localhost ~]# ls
anaconda-ks.cfg hhhhh.txt test.txt 模板 图片 下载 桌面
file.txt initial-setup-ks.cfg 公共 视频 文档 音乐
find
类似于locate命令,使用 find还可以搜索文件和目录。区别在于,您可以使用find命令在给定目录中查找文件。例如,find / home / -name notes.txt命令将在主目录及其子目录中搜索名为notes.txt的文件。
演示:
[root@localhost ~]# find hhhhh.txt
hhhhh.txt
grep
无疑对日常使用有用的另一个基本Linux命令是grep。它使您可以搜索给定文件中的所有文本。
演示:
[root@localhost ~]# grep g test.txt
ggth
grgrg54efef343498hr
32f43gregregrfwef
eewfregeg
sudo
该命令是“ SuperUser Do ”的缩写,使您能够执行需要管理或超级用户权限的任务。
演示:
[root@localhost ~]# sudo grep g test.txt
ggth
grgrg54efef343498hr
32f43gregregrfwef
eewfregeg
df
使用df命令获取有关系统磁盘空间使用情况的报告,以百分比和KB表示。如果要以兆字节为单位查看报告,请输入df -m。
演示:
[root@localhost ~]# df -m
文件系统 1M-块 已用 可用 已用% 挂载点
devtmpfs 895 0 895 0% /dev
tmpfs 910 0 910 0% /dev/shm
tmpfs 910 11 900 2% /run
tmpfs 910 0 910 0% /sys/fs/cgroup
/dev/sda3 199582 5600 193983 3% /
/dev/sda1 1014 180 835 18% /boot
tmpfs 182 1 182 1% /run/user/0
/dev/sr0 4494 4494 0 100% /run/media/root/CentOS 7 x86_64
head
所述head命令用于查看任何文本文件的第一行。默认情况下,它将显示前十行,但是您可以根据自己的喜好更改此数字。例如,如果只想显示前2行,则键入head -n 2 test.txt
。
演示:
[root@localhost ~]# head -n 2 test.txt
wiqiehd
wdwqrhhfife
tail
该命令的功能与head命令类似,但是tail命令将显示文本文件的最后十行,而不是显示第一行。例如,tail -n 2 test.txt
。
演示:
[root@localhost ~]# tail -n 2 test.txt
eewfregeg
diff
diff命令是差异的缩写,diff命令逐行比较两个文件的内容。分析文件后,它将输出不匹配的行。程序员在需要更改程序而不是重写整个源代码时经常使用此命令。
演示:
[root@localhost ~]# diff test.txt hhhhh.txt
1,7d0
< wiqiehd
< wdwqrhhfife
< ggth
< grgrg54efef343498hr
< 32f43gregregrfwef
< eewfregeg
<
chmod
chmod是另一个Linux命令,用于更改文件和目录的读取,写入和执行权限。
文件权限简介:‘r’ 代表可读,‘w’ 代表可写,‘x’ 代表执行权限
演示:
[root@localhost ~]# chmod a+r test.txt
[root@localhost ~]# ll
总用量 16
-rw-------. 1 root root 1849 6月 15 14:39 anaconda-ks.cfg
-rw-r--r--. 1 root root 98 6月 16 15:17 file.txt
-rw-r--r--. 1 root root 0 6月 16 15:32 hhhhh.txt
-rw-r--r--. 1 root root 1880 6月 15 14:41 initial-setup-ks.cfg
-rw-r--r--. 1 root root 74 6月 16 15:31 test.txt
[root@localhost ~]# chmod a+w test.txt
[root@localhost ~]# ll
总用量 16
-rw-------. 1 root root 1849 6月 15 14:39 anaconda-ks.cfg
-rw-r--r--. 1 root root 98 6月 16 15:17 file.txt
-rw-r--r--. 1 root root 0 6月 16 15:32 hhhhh.txt
-rw-r--r--. 1 root root 1880 6月 15 14:41 initial-setup-ks.cfg
-rw-rw-rw-. 1 root root 74 6月 16 15:31 test.txt
chown
在Linux中,所有文件均归特定用户所有。该CHOWN命令使您可以更改或文件的所有权转让给指定的用户名。
演示:
[root@localhost ~]# chown elsa test.txt
[root@localhost ~]# ll
总用量 16
-rw-------. 1 root root 1849 6月 15 14:39 anaconda-ks.cfg
-rw-r--r--. 1 root root 98 6月 16 15:17 file.txt
-rw-r--r--. 1 root root 0 6月 16 15:32 hhhhh.txt
-rw-r--r--. 1 root root 1880 6月 15 14:41 initial-setup-ks.cfg
-rw-rw-rw-. 1 elsa root 74 6月 16 15:31 test.txt
[root@localhost ~]# chown root test.txt
[root@localhost ~]# ll
总用量 16
-rw-------. 1 root root 1849 6月 15 14:39 anaconda-ks.cfg
-rw-r--r--. 1 root root 98 6月 16 15:17 file.txt
-rw-r--r--. 1 root root 0 6月 16 15:32 hhhhh.txt
-rw-r--r--. 1 root root 1880 6月 15 14:41 initial-setup-ks.cfg
-rw-rw-rw-. 1 root root 74 6月 16 15:31 test.txt
ping
使用ping命令检查与服务器的连接状态。
演示:
[root@localhost ~]# ping www.baidu.com
PING www.a.shifen.com (163.177.151.109) 56(84) bytes of data.
64 bytes from 163.177.151.109 (163.177.151.109): icmp_seq=9 ttl=53 time=70.5 ms
64 bytes from 163.177.151.109 (163.177.151.109): icmp_seq=10 ttl=53 time=47.3 ms
64 bytes from 163.177.151.109 (163.177.151.109): icmp_seq=11 ttl=53 time=117 ms
64 bytes from 163.177.151.109 (163.177.151.109): icmp_seq=12 ttl=53 time=50.1 ms
64 bytes from 163.177.151.109 (163.177.151.109): icmp_seq=13 ttl=53 time=62.9 ms
64 bytes from 163.177.151.109 (163.177.151.109): icmp_seq=14 ttl=53 time=104 ms
64 bytes from 163.177.151.109 (163.177.151.109): icmp_seq=15 ttl=53 time=95.7 ms
^C
--- www.a.shifen.com ping statistics ---
15 packets transmitted, 7 received, 53% packet loss, time 14026ms
rtt min/avg/max/mdev = 47.394/78.376/117.178/25.510 ms
top
作为与Windows中的任务管理器等效的终端,top命令将显示正在运行的进程以及每个进程使用多少CPU的列表。监视系统资源的使用非常有用,尤其是知道哪个进程由于消耗太多资源而需要终止时。
演示:
[root@localhost ~]# top
top - 15:45:34 up 53 min, 2 users, load average: 0.00, 0.01, 0.05
Tasks: 219 total, 1 running, 218 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.3 us, 0.1 sy, 0.0 ni, 99.6 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 1863028 total, 75852 free, 1233940 used, 553236 buff/cache
KiB Swap: 4194300 total, 4174324 free, 19976 used. 402880 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
2318 root 20 0 3790280 189644 47736 S 1.3 10.2 0:44.89 gnome-she+
1746 mysql 20 0 2045836 359964 1280 S 0.7 19.3 0:11.90 mysqld
320 root 20 0 0 0 0 S 0.3 0.0 0:01.85 xfsaild/s+
1774 root 20 0 392700 72936 36832 S 0.3 3.9 0:16.77 X
2489 root 20 0 779868 9392 3976 S 0.3 0.5 0:00.45 gsd-color
1 root 20 0 193808 5116 2768 S 0.0 0.3 0:02.06 systemd
2 root 20 0 0 0 0 S 0.0 0.0 0:00.01 kthreadd
4 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kworker/0+
6 root 20 0 0 0 0 S 0.0 0.0 0:00.39 ksoftirqd+
7 root rt 0 0 0 0 S 0.0 0.0 0:00.03 migration+
8 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcu_bh
9 root 20 0 0 0 0 S 0.0 0.0 0:00.96 rcu_sched
10 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 lru-add-d+
11 root rt 0 0 0 0 S 0.0 0.0 0:00.01 watchdog/0
12 root rt 0 0 0 0 S 0.0 0.0 0:00.01 watchdog/1
13 root rt 0 0 0 0 S 0.0 0.0 0:00.02 migration+
14 root 20 0 0 0 0 S 0.0 0.0 0:00.02 ksoftirqd+
history
使用Linux一段时间后,您会很快注意到每天可以运行数百个命令。因此,如果您想查看之前输入的命令,运行历史记录命令特别有用。
[root@localhost ~]# history
1 java -v
2 java
3 ipconfig
4 ip config
5 ifconfig
6 rpm -qa | grep mysql
7 yum localinstall https://repo.mysql.com//mysql80-community-release-el7-1.noarch.rpm
......
man
对某些Linux命令的功能感到困惑?不用担心,您可以使用man命令从Linux的外壳中轻松学习如何使用它们。例如,输入man tail将显示tail命令的手动指令。
[root@localhost ~]# man tail
TAIL(1) FSF TAIL(1)
NAME(名称)
tail - 输出文件的末尾部分
SYNOPSIS(总览)
../src/tail [OPTION]... [FILE]...
DESCRIPTION(描述)
在标准输出上显示每个FILE的最后10行.
如果多于一个FILE,会一个接一个地显示, 并在每个文件显示的首部给出文件名.
如果没有FILE,或者FILE是-,那么就从标准输入上读取.
--retry
即使tail开始时就不能访问
或者在tail运行后不能访问,也仍然不停地尝试打开文件. --
只与-f合用时有用.
-c, --bytes=N
输出最后N个字节
-f, --follow[={name|descriptor}]
当文件增长时,输出后续添加的数据; -f, --follow以及 --fol‐
Manual page tail(1) line 1 (press h for help or q to quit)
echo
此命令用于将一些数据移到文件中。例如,如果要将文本“ Hello,可以键入echo Hello, my name is elsa >> name.txt
演示:
[root@localhost ~]# echo Hello, my name is elsa >> name.txt
[root@localhost ~]# ls
anaconda-ks.cfg hhhhh.txt name.txt 公共 视频 文档 音乐
file.txt initial-setup-ks.cfg test.txt 模板 图片 下载 桌面
[root@localhost ~]# cat txt
cat: txt: 没有那个文件或目录
[root@localhost ~]# cat name.txt
Hello, my name is elsa
hostname
如果您想知道主机/网络的名称,只需键入hostname。
[root@localhost ~]# hostname
localhost.localdomain
useradd、userdel
由于Linux是多用户系统,因此这意味着多个人可以同时与同一个系统进行交互。useradd用于创建新用户,而passwd将密码添加到该用户的帐户。
演示:
[root@localhost ~]# useradd hhh -p 123456
[root@localhost ~]# userdel hhh
su
su命令可以切换Linux中的角色。如果切换到root角色,就需要密码。
演示:
[root@localhost ~]# su elsa
[elsa@localhost root]$ su root
密码:
[root@localhost ~]#
exit
退出当前的角色,返回之前的角色。
演示:
[root@localhost ~]# exit
exit
[elsa@localhost root]$ exit
exit
[root@localhost ~]#
vim
该命令用于编辑文件,如果没有该文件,就自动创建该文件。
进入后,操作界面有三种模式:命令模式(command mode)、插入模式(Insert mode)和底行模式(last line mode)
命令模式
-刚进入文件就是命令模式,通过方向键控制光标位置,
-使用命令"dd"删除当前整行
-使用命令"/字段"进行查找
-按"i"在光标所在字符前开始插入
-按"a"在光标所在字符后开始插入
-按"o"在光标所在行的下面另起一新行插入
-按":"进入底行模式
插入模式
-此时可以对文件内容进行编辑,左下角会显示 "-- 插入 --""
-按"ESC"进入底行模式
底行模式
-退出编辑: :q
-强制退出: :q!
-保存并退出: :wq
## 操作步骤示例 ##
1.保存文件:按"ESC" -> 输入":" -> 输入"wq",回车 //保存并退出编辑
2.取消操作:按"ESC" -> 输入":" -> 输入"q!",回车 //撤销本次修改并退出编辑
演示:
[root@localhost ~]# ls
anaconda-ks.cfg hhhhh.txt name.txt 公共 视频 文档 音乐
file.txt initial-setup-ks.cfg test.txt 模板 图片 下载 桌面
[root@localhost ~]# vim 123.txt
[root@localhost ~]# ls
123.txt hhhhh.txt test.txt 视频 下载
anaconda-ks.cfg initial-setup-ks.cfg 公共 图片 音乐
file.txt name.txt 模板 文档 桌面
yum
该命令可以用来安装插件。
演示:
# 安装httpd
yum install httpd
# 更新httpd
yum update httpd
# 卸载httpd
yum remove httpd
效果:
[root@localhost ~]# yum install httpd
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirrors.bfsu.edu.cn
* extras: mirrors.bfsu.edu.cn
* updates: mirrors.bfsu.edu.cn
正在解决依赖关系
--> 正在检查事务
......
[root@localhost ~]# yum update httpd
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirrors.bfsu.edu.cn
* extras: mirrors.bfsu.edu.cn
* updates: mirrors.bfsu.edu.cn
No packages marked for update
[root@localhost ~]# yum remove httpd
已加载插件:fastestmirror, langpacks
正在解决依赖关系
--> 正在检查事务
---> 软件包 httpd.x86_64.0.2.4.6-97.el7.centos.5 将被 删除
--> 解决依赖关系完成
ifconfig
查看网络状态。
演示:
[root@localhost ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.73.36 netmask 255.255.255.0 broadcast 192.168.73.255
inet6 2408:8459:6a40:636:aa8e:7cd6:8ac9:f945 prefixlen 64 scopeid 0x0<global>
inet6 fe80::2fde:16ed:14a9:8a3d prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:1e:18:b9 txqueuelen 1000 (Ethernet)
RX packets 112376 bytes 153558958 (146.4 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 58305 bytes 6198620 (5.9 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
clear
命令行清屏操作。
演示:
[root@localhost ~]# clear
[root@localhost ~]#
tar
打包文件,将多个文件进行打包。
打包演示:
[root@localhost ~]# ls
123.txt hhhhh.txt test.txt 视频 下载
anaconda-ks.cfg initial-setup-ks.cfg 公共 图片 音乐
file.txt name.txt 模板 文档 桌面
[root@localhost ~]# tar -zcvf c.tar file.txt name.txt
file.txt
name.txt
[root@localhost ~]# ls
123.txt file.txt name.txt 模板 文档 桌面
anaconda-ks.cfg hhhhh.txt test.txt 视频 下载
c.tar initial-setup-ks.cfg 公共 图片 音乐
该命令中的-zcvf
是指
z:调用gzip压缩命令进行压缩;
c:打包文件;
v:显示运行过程;
f:指定文件名;
x:解压文件
解压演示:
[root@localhost ~]# tar -zxvf c.tar
file.txt
name.txt
[root@localhost ~]# ls
123.txt file.txt name.txt 模板 文档 桌面
anaconda-ks.cfg hhhhh.txt test.txt 视频 下载
c.tar initial-setup-ks.cfg 公共 图片 音乐