2021-10-25


一、Linux命令和windows区别?

1、Linux命令全部严格区分大小写;而windows命令大小写都识别。

2、Linux所有的文件都是没有扩展名的,而windows是文件是有扩展名的。







创建一个新用户


[root@localhost mxh]# useradd mxh2        # 创建一个user
[root@localhost mxh]# password mxh2             #12345678
[root@localhost xxx]# groupadd mxhgrp     #创建一个组mxhgrp
[root@localhost xxx]# passwd  mxhgrp         #12345678

 passwd  命令用于更改密码

管理员可以更改任何人的密码;而普通用户只能更改自己的密码;

查找命令使用方法

1、man ls
2、 ls --help



二、使用命令

命令格式

        命令  [-选项]  [参数]     []->指可选参数,不加这两个参数的命令也可以单独执行,例如ls

       例如: ls     -la    /etc  

             ls:命令; -la:选项;/etc:操作对象

文件用户分为三类:所有者、用户组、其他人

1、所有者:只能有一个,而且身份可能是会变换的;比如我的电脑,我就是所有者,卖给别人后,那个卖买的人就是所有者,而我不是所有者了。

2、所属组:我的电脑可以给办公室里的美女使用,多个美女就是一个所属组,而我还是所有者

3、其他人:除了以上两个身份外的用户

目录处理命令
ls -a查看目录下所有文件,包括隐藏的文件
ls -l查看目录下文件的详细信息(包括权限)
ls -lh人性化查看目录下文件的详细信息(包括权限)
ls -ld查看文件详细信息(包括权限)
ls -li查看文件的ID序号

1、ls:主要查看当前目录下有什么文件,并没有定位到那个目录下

[root@localhost ~]# ls /
bin   dev  home  lib64  mnt  proc  run   srv  tmp  var
boot  etc  lib   media  opt  root  sbin  sys  usr

2、cd:用于定位到那个目录下

[root@localhost ~]# ls /
bin   dev  home  lib64  mnt  proc  run   srv  tmp  var
boot  etc  lib   media  opt  root  sbin  sys  usr
[root@localhost ~]# ls /run
auditd.pid   dbus                initramfs       plymouth     tmpfiles.d
blkid        dhclient-ens33.pid  lock            sepermit     tuned
chrony       dmeventd-client     log             setrans      udev
console      dmeventd-server     lvm             sshd.pid     user
crond.pid    ebtables.lock       mount           sudo         utmp
cron.reboot  faillock            netreport       syslogd.pid  vmware
cryptsetup   firewalld           NetworkManager  systemd      xtables.lock
[root@localhost ~]# cd /run
[root@localhost run]# ls
auditd.pid   dbus                initramfs       plymouth     tmpfiles.d
blkid        dhclient-ens33.pid  lock            sepermit     tuned
chrony       dmeventd-client     log             setrans      udev
console      dmeventd-server     lvm             sshd.pid     user
crond.pid    ebtables.lock       mount           sudo         utmp
cron.reboot  faillock            netreport       syslogd.pid  vmware
cryptsetup   firewalld           NetworkManager  systemd      xtables.lock
[root@localhost run]#

3、ls -a:显示当前目录下所有文件,包括隐藏的文件,隐藏的文件是以 .开头的

[root@localhost ~]# ls -a
.  ..  anaconda-ks.cfg  .bash_logout  .bash_profile  .bashrc  .cshrc  .tcshrc

4、ls -l(l->long) :文件以一个详细的信息列出来显示


[root@localhost ~]# ls -l
总用量 4
-rw-------. 1 root root 1216 10月 22 21:38 anaconda-ks.cfg

#1->引用计数; root->所有者(rw权限); root->所属组(无权限); 1216B->文件大小;10.22是文件修改时间

ls -lh(h->human人性化显示)


[root@localhost ~]# ls -lh   
总用量 4.0K
-rw-------. 1 root root 1.2K 10月 22 21:38 anaconda-ks.cfg

文件类型:区分软连接、文件、目录

-:表示一个文件

d开头:表示一个目录

l:开头表示一个软连接


[root@localhost ~]# ls -l /
总用量 20
lrwxrwxrwx.   1 root root    7 10月 22 21:29 bin -> usr/bin  #软连接
dr-xr-xr-x.   5 root root 4096 10月 23 09:23 boot            #目录

[root@localhost ~]# ls -a
.  ..  anaconda-ks.cfg  .bash_logout  .bash_profile  .bashrc  .cshrc  .tcshrc
[root@localhost ~]# ls -l /run     #查看run目录下所有文件权限
总用量 24
-rw-r--r--.  1 root   root      4 10月 23 09:21 auditd.pid
drwxr-xr-x.  2 root   root     80 10月 23 09:22 blkid     #所有者:rw、x、r权限;所属组:x、r权限
drwxr-x---.  2 chrony chrony   80 10月 23 09:23 chrony     #是一个目录
drwxr-xr-x.  2 root   root     40 10月 23 09:21 console
-rw-r--r--.  1 root   root      5 10月 23 09:23 crond.pid   #是一个文件

X执行权限最大;

5、ls -ld :查看文件权限,不是目录下文件的权限;注意与ls -l的区别;


[root@localhost ~]# ls -ld /run
drwxr-xr-x. 26 root root 740 10月 23 09:23 /run  #run这个文件:所有者rw、x、r权限

5、ls -li:查看文件的id序号(最前面的是ID号)


[root@localhost ~]# ls -li /run
总用量 24
15072 -rw-r--r--.  1 root   root      4 10月 23 09:21 auditd.pid
16995 drwxr-xr-x.  2 root   root     80 10月 23 09:22 blkid
24830 drwxr-x---.  2 chrony chrony   80 10月 23 09:23 chrony
15035 drwxr-xr-x.  2 root   root     40 10月 23 09:21 console
26891 -rw-r--r--.  1 root   root      5 10月 23 09:23 crond.pid
28110 ----------.  1 root   root      0 10月 23 09:23 cron.reboot
15034 drwx------.  2 root   root     40 10月 23 09:21 cryptsetup
24213 drwxr-xr-x.  2 root   root     60 10月 23 09:23 dbus
27608 -rw-r--r--.  1 root   root      5 10月 23 11:31 dhclient-ens33.pid
13853 prw-------.  1 root   root      0 10月 23 09:19 dmeventd-client
13852 prw-------.  1 root   root      0 10月 23 09:19 dmeventd-server
28868 -rw-------.  1 root   root      0 10月 23 09:23 ebtables.lock
15024 drwxr-xr-x.  2 root   root     40 10月 23 09:21 faillock
27614 drwxr-x---.  2 root   root     40 10月 23 09:23 firewalld
10562 drwxr-xr-x.  4 root   root    120 10月 23 09:19 initramfs
 1085 drwxr-xr-x.  5 root   root    120 10月 23 10:30 lock
10566 drwxr-xr-x.  3 root   root     60 10月 23 09:19 log

文件处理命令
mkdir

创建文件mkdir /tmp/mxh    在tmp目录下创建mxh文件夹

mkdir -p递归创建多个文件夹
cd定位到某个目录下
cd..返回上一级目录
pwd打印当前所在的完整目录
rmdir删除命令(只能删除空目录)
cp复制命令:将文件复制到其他目录下的操作
cp -r复制命令:将目录复制到其他目录下的操作
cp -p复制文件时保留其属性,修改时间不变,特别是日志
cp进行复制的同时还可以改名字

1、mkdir 创建文件

mkdir +文件位置  这个只能在原有的目录下创建一个文件夹,不能连续创建多个子目录


[root@localhost ~]# mkdir /tmp/mxh
[root@localhost ~]# ls /tmp
ks-script-IAr5pa
mxh                #这个是我创建的文件夹
vmware-root_724-2965906890
vmware-root_7527-3853947382
yum.log

2、mkdir -p 加目录:可连续创建多个子目录


[root@localhost tmp]# mkdir -p /tmp/mxh/mxh1/mxh2  #连续创建两个子目录mxh1、mxh2
[root@localhost tmp]# ls /tmp/mxh
mxh1
[root@localhost tmp]# ls /tmp/mxh/mxh1
mxh2

3、mkdir 同时创建多个并行目录文件夹


[root@localhost ~]# mkdir /tmp/mxh/AK1 /tmp/mxh/AK2  #两个目录中间是用空格隔开
[root@localhost ~]# ls -l /tmp
总用量 4
-rwx------. 1 root root 836 10月 22 21:38 ks-script-IAr5pa
drwxr-xr-x. 5 root root  40 10月 23 13:00 mxh
drwx------. 3 root root  17 10月 23 09:23 systemd-private-4b41272e80d041ceb75a8d4cc2b1d4a6-chronyd.service-KlCaFC
drwx------. 3 root root  17 10月 22 21:41 systemd-private-4d581edd37154b6cb14d94ae0babdd78-chronyd.service-dubJHv
drwx------. 2 root root   6 10月 22 21:41 vmware-root_724-2965906890
drwx------. 2 root root   6 10月 23 09:23 vmware-root_7527-3853947382
-rw-------. 1 root root   0 10月 22 21:28 yum.log
[root@localhost ~]# ls -l /tmp/mxh
总用量 0
drwxr-xr-x. 2 root root  6 10月 23 13:00 AK1     #在/tmp/mxh下创建AK1
drwxr-xr-x. 2 root root  6 10月 23 13:00 AK2     #在/tmp/mxh下创建AK2
drwxr-xr-x. 3 root root 18 10月 23 12:52 mxh1

4、pwd


[root@localhost ~]# cd /tmp/mxh/AK1
[root@localhost AK1]# ls
[root@localhost AK1]# pwd
/tmp/mxh/AK1

5、cd用法,返回上一级命令

[root@localhost tmp]# cd /tmp/mxh/AK1
[root@localhost AK1]# pwd
/tmp/mxh/AK1
[root@localhost AK1]# cd ..
[root@localhost mxh]# cd ..
[root@localhost tmp]# cd ..
[root@localhost /]# cd ..
[root@localhost /]# ls /
bin   dev  home  lib64  mnt  proc  run   srv  tmp  var
boot  etc  lib   media  opt  root  sbin  sys  usr

6、rmdir 删除命令使用(只能删除空目录)


[root@localhost /]# ls /tmp/mxh
AK1  AK2  mxh1
[root@localhost /]# rmdir /tmp/mxh/AK2   #删除AK2
[root@localhost /]# ls /tmp/mxh
AK1  mxh1

7、cp复制命令:#这个是将文件复制到其他目录下的操作

#这个是将文件复制到其他目录下的操作
[root@localhost tmp]# cp /run/auditd.pid /tmp/mxh/test  #将auditd.pid复制到test目录下
[root@localhost tmp]# ls /tmp/mxh/test
auditd.pid

8、将目录复制到其他目录下操作


[root@localhost tmp]# ls /tmp
ks-script-IAr5pa
mxh
systemd-private-4b41272e80d041ceb75a8d4cc2b1d4a6-chronyd.service-KlCaFC
systemd-private-4d581edd37154b6cb14d94ae0babdd78-chronyd.service-dubJHv
test1                         #将test1这个目录复制到麦mxh这个目录下
vmware-root_724-2965906890
vmware-root_7527-3853947382
yum.log
[root@localhost tmp]# cp -r /tmp/test1 /tmp/mxh
[root@localhost tmp]# ls /tmp/mxh
test  test1

9、cp同时复制多个文件到其他目录   cp 目标文件1  目标文件2  。。。。 目的目录

#将/run下的crond.pid、cron.reboot复制到test1目录下
[root@localhost tmp]# cp /run/crond.pid /run/cron.reboot /tmp/mxh/test1
[root@localhost tmp]# ls /tmp/mxh/test1
crond.pid  cron.reboot

10、cp -p :复制文件时保留其属性不变

[root@localhost tmp]# ls /run
auditd.pid   dbus                initramfs       plymouth     tmpfiles.d
blkid        dhclient-ens33.pid  lock            sepermit     tuned
chrony       dmeventd-client     log             setrans      udev
console      dmeventd-server     lvm             sshd.pid     user
crond.pid    ebtables.lock       mount           sudo         utmp
cron.reboot  faillock            netreport       syslogd.pid  vmware
cryptsetup   firewalld           NetworkManager  systemd      xtables.lock
[root@localhost tmp]# ls -l /run/crond.pid
-rw-r--r--. 1 root root 5 10月 23 09:23 /run/crond.pid
[root@localhost tmp]# cp -p /run/crond.pid  /tmp/mxh/test1
cp:是否覆盖"/tmp/mxh/test1/crond.pid"? y
[root@localhost tmp]# ls -l /tmp/mxh/test1
总用量 4
-rw-r--r--. 1 root root 5 10月 23 09:23 crond.pid   时间和修改前不变

11、cp进行复制的同时还可以改名字


[root@localhost tmp]# ls /tmp/mxh
test  test1
[root@localhost tmp]# ls /tmp/mxh/test1
crond.pid  cron.reboot
[root@localhost tmp]# cp -r /tmp/mxh/test1 /tmp/mxh/test/gaiming   #将test1这个目录复制到test目录下,并同时将test1这个目录名称改为gaiming这个名称
[root@localhost tmp]# ls /tmp/mxh/test
auditd.pid  gaiming
[root@localhost tmp]# ls /tmp/mxh/test/gaiming
crond.pid  cron.reboot

mv剪切功能:mv  剪切目标  剪切目的(可直接剪切文件或目录,不需要像cp -r 来复制目录)
mv该文件或文件夹名称

1、mv 剪切功能(可直接剪切文件或目录,不需要像cp -r 来复制目录)


[root@localhost ~]# ls /tmp/mxh/test1  
crond.pid  cron.reboot
[root@localhost ~]# mv /tmp/mxh/test1/crond.pid /tmp/mxh  #将crond.pid剪切到mxh目录下
[root@localhost ~]# ls /tmp/mxh
crond.pid  test1
[root@localhost ~]# ls /tmp/mxh/test1
cron.reboot

2、mv 剪切功能同时改名(可直接剪切文件或目录,不需要像cp -r 来复制目录)


[root@localhost ~]# ls /tmp/mxh
test1
[root@localhost ~]# ls /tmp/mxh/test1
crond.pid  cron.reboot
[root@localhost ~]# cp /tmp/mxh/test1/crond.pid /tmp/mxh/test1/1.pid
[root@localhost ~]# ls /tmp/mxh/test1
1.pid  crond.pid  cron.reboot
[root@localhost ~]# mv /tmp/mxh/test1/1.pid /tmp/mxh/2.pid #将1.pid 剪切并改名为2.pid到mxh目录下
[root@localhost ~]# ls /tmp/mxh/test1
crond.pid  cron.reboot
[root@localhost ~]# ls /tmp/mxh
2.pid  test1

3、mv 直接改文件夹或文件名称

[root@localhost ~]# ls /tmp/mxh
2.pid  test1
[root@localhost ~]# cd /tmp/mxh
[root@localhost mxh]# mv 2.pid 3.pid
[root@localhost mxh]# ls
3.pid  test1
rm删除文件(每次删除会带有问你是否删除)
rm -f强制删除  (不会带有询问)
rm -rf删除目录文件夹

1、rm删除文件


[root@localhost ~]# ls /tmp/mxh
1.pid  test1
[root@localhost ~]# cp /tmp/mxh/1.pid /tmp/mxh/2.pid
[root@localhost ~]# ls /tmp/mxh           #绝对路经下复制
1.pid  2.pid  test1              
[root@localhost ~]# cd /tmp/mxh
[root@localhost mxh]# cp 2.pid 3.pid
[root@localhost mxh]# ls                 #进到路径下复制
1.pid  2.pid  3.pid  test1
[root@localhost mxh]# rm 1.pid            #删除文件
rm:是否删除普通文件 "1.pid"?y
[root@localhost mxh]# ls
2.pid  3.pid  test1
[root@localhost mxh]# cp 2.pid 1.pid
[root@localhost mxh]# ls
1.pid  2.pid  3.pid  test1
[root@localhost mxh]# mv 3.pid 4.pid
[root@localhost mxh]# ls
1.pid  2.pid  4.pid  test1

2、rm -f 强制删除  (不会带有询问)


[root@localhost mxh]# ls
1.pid  2.pid  4.pid  test1
[root@localhost mxh]# cd
[root@localhost ~]# rm -f /tmp/mxh/1.pid /tmp/mxh/2.pid  #删除1.pid 2.pid
[root@localhost ~]# ls /tmp/mxh
4.pid  test1

3、rm -rf 强制删除目录文件夹


[root@localhost mxh]# mkdir test
[root@localhost mxh]# ls
1.pid  2.pid  3.pid  4.pid  test  test1
[root@localhost mxh]# cp 1.pid 2.pid 3.pid 4.pid test
[root@localhost mxh]# ls test
1.pid  2.pid  3.pid  4.pid
[root@localhost mxh]# ls
1.pid  2.pid  3.pid  4.pid  test  test1
[root@localhost mxh]# ls test
1.pid  2.pid  3.pid  4.pid
[root@localhost mxh]# rm test
rm: 无法删除"test": 是一个目录
[root@localhost mxh]# rm -r test
rm:是否进入目录"test"? y
rm:是否删除普通文件 "test/1.pid"?y
rm:是否删除普通文件 "test/2.pid"?y
rm:是否删除普通文件 "test/3.pid"?y
rm:是否删除普通文件 "test/4.pid"?y
rm:是否删除目录 "test"?y
[root@localhost mxh]# ls
1.pid  2.pid  3.pid  4.pid  test1
[root@localhost mxh]# mkdir test
[root@localhost mxh]# cp 1.pid 2.pid 3.pid 4.pid test
[root@localhost mxh]# ls test
1.pid  2.pid  3.pid  4.pid
[root@localhost mxh]# ls
1.pid  2.pid  3.pid  4.pid  test  test1
[root@localhost mxh]# rm -rf test
[root@localhost mxh]# ls
1.pid  2.pid  3.pid  4.pid  test1

文件处理命令

touch新建一个文件(mkdir是创建一个目录,不一样)
cat直接浏览文件的具体内容(适合小文档内容查看)
cat -n显示内容的行号
more查看大文件很多内容的方法
less查看大文件很多内容的方法,比more好用(less只能区分大小写查找,要想找到只能输入的单词完全正确才行,与后面的grep 查找文档内容有区别)
head -n 行数m查看文件的前m行
tail -n 行数m查看文件的最后的m行
tail -f动态显示日志的后10行内容,日志变化显示也跟着变化

1、touch创建文件命令


[root@localhost mxh]# touch 7.pid
[root@localhost mxh]# ls
1.pid  2.pid  3.pid  4.pid  7.pid  test1

2、连续新建文档(适合小文档内容查看)


[root@localhost mxh]# touch 5.pid 6.pid
[root@localhost mxh]# ls
1.pid  2.pid  3.pid  4.pid  5.pid  6.pid  7.pid  test1

3、cat直接浏览文件的具体内容,cat -n 显示内容的行数


[root@localhost mxh]# cat 1.pid
7565

[root@localhost mxh]# cat -n 1.pid
     1  7565

4、more:查看一个文件里有很多内容方法


[root@localhost ~]# more /etc/services
# /etc/services:
# $Id: services,v 1.55 2013/04/14 ovasik Exp $
#
# Network services, Internet style
# IANA services version: last updated 2013-04-10
#
# Note that it is presently the policy of IANA to assign a single well-known
# port number for both TCP and UDP; hence, most entries here have two entries
# even if the protocol doesn't support UDP operations.
# Updated from RFC 1700, ``Assigned Numbers'' (October 1994).  Not all ports
# are included, only the more common ones.
#
# The latest IANA port assignments can be gotten from
#       http://www.iana.org/assignments/port-numbers
# The Well Known Ports are those from 0 through 1023.
# The Registered Ports are those from 1024 through 49151
# The Dynamic and/or Private Ports are those from 49152 through 65535
#
# Each line describes one service, and is of the form:
#
# service-name  port/protocol  [aliases ...]   [# comment]

tcpmux          1/tcp                           # TCP port service multiplexer
--More--(0%)

空格或f  往下翻页

Enter  一行一行的翻页

b        往上一页一页的往回翻页

ctrl +C  退出

5、more的用法

tcpmux          1/udp                           # TCP port service multiplexer
[root@localhost ~]# less /etc/services
[root@localhost ~]#
[root@localhost ~]# less /etc/services
# /etc/services:
# $Id: services,v 1.55 2013/04/14 ovasik Exp $
#
# Network services, Internet style
# IANA services version: last updated 2013-04-10
#
# Note that it is presently the policy of IANA to assign a single well-known
# port number for both TCP and UDP; hence, most entries here have two entries
# even if the protocol doesn't support UDP operations.
# Updated from RFC 1700, ``Assigned Numbers'' (October 1994).  Not all ports
# are included, only the more common ones.

PgDn向上翻页

PgDn向下翻页

q  退出


#
# The latest IANA port assignments can be gotten from
#       http://www.iana.org/assignments/port-numbers
# The Well Known Ports are those from 0 through 1023.
# The Registered Ports are those from 1024 through 49151
# The Dynamic and/or Private Ports are those from 49152 through 65535
#
# Each line describes one service, and is of the form:
#
# service-name  port/protocol  [aliases ...]   [# comment]

tcpmux          1/tcp                           # TCP port service multiplexer
/service       #写入/ +要查找的关键字  就会帮你找出来  

 #写入"/" +要查找的关键字  就会帮你找出来

按键 n(next) 会帮你查找下一个关键字

6、head -n 行数m:查看文件内容的前m行

[root@localhost ~]# head -n 10 /etc/services
# /etc/services:
# $Id: services,v 1.55 2013/04/14 ovasik Exp $
#
# Network services, Internet style
# IANA services version: last updated 2013-04-10
#
# Note that it is presently the policy of IANA to assign a single well-known
# port number for both TCP and UDP; hence, most entries here have two entries
# even if the protocol doesn't support UDP operations.
# Updated from RFC 1700, ``Assigned Numbers'' (October 1994).  Not all ports

7、tail -n 行数m:查看文件内容的后m行(工作中查看日志常用)


[root@localhost ~]# tail -n 5 /etc/services
com-bardac-dw   48556/tcp               # com-bardac-dw
com-bardac-dw   48556/udp               # com-bardac-dw
iqobject        48619/tcp               # iqobject
iqobject        48619/udp               # iqobject
matahari        49000/tcp               # Matahari Broker

8、tail -f   动态显示日志的后10行内容,日志变化显示也跟着变化

链接命令:软链接命令相当于windows的快捷方式

ln(link)ln -s  [源文件]  [目标文件]  生成链接文件  针对的是文件,不是目录  软链接
ln   [源文件]  [目标文件]  生成链接文件  针对的是文件,不是目录,生成硬链接

  

1、ln -s  [源文件]  [目标文件]  生成链接文件  针对的是文件,不是目录  生成软链接


[root@localhost mxh]# ls
1.pid  2.pid  3.pid  4.pid  5.pid  6.pid  7.pid  mxhtest  test1
[root@localhost mxh]# ln -s mxhtest mxhtest.soft  生成软链接
[root@localhost mxh]# ls -l
总用量 16
-rw-r--r--. 1 root root  5 10月 23 22:18 1.pid
-rw-r--r--. 1 root root  5 10月 23 22:18 2.pid
-rw-r--r--. 1 root root  5 10月 23 22:18 3.pid
-rw-r--r--. 1 root root  5 10月 23 22:18 4.pid
-rw-r--r--. 1 root root  0 10月 23 23:17 5.pid
-rw-r--r--. 1 root root  0 10月 23 23:17 6.pid
-rw-r--r--. 1 root root  0 10月 23 23:07 7.pid
drwxr-xr-x. 2 root root  6 10月 24 10:00 mxhtest
lrwxrwxrwx. 1 root root  7 10月 24 10:01 mxhtest.soft -> mxhtest   #软连接
drwxr-xr-x. 2 root root 42 10月 23 21:51 test1

2、ln   [源文件]  [目标文件]  生成链接文件  针对的是文件,不是目录  生成硬链接


[root@localhost mxh]# ln mxhtest mxhtest.hard
[root@localhost mxh]# ls -l
总用量 16
-rw-r--r--. 1 root root  5 10月 23 22:18 1.pid
-rw-r--r--. 1 root root  5 10月 23 22:18 2.pid
-rw-r--r--. 1 root root  5 10月 23 22:18 3.pid
-rw-r--r--. 1 root root  5 10月 23 22:18 4.pid
-rw-r--r--. 1 root root  0 10月 23 23:17 5.pid
-rw-r--r--. 1 root root  0 10月 23 23:17 6.pid
-rw-r--r--. 1 root root  0 10月 23 23:07 7.pid
-rw-r--r--. 2 root root  0 10月 24 10:07 mxhtest
-rw-r--r--. 2 root root  0 10月 24 10:07 mxhtest.hard
lrwxrwxrwx. 1 root root  7 10月 24 10:07 mxhtest.soft -> mxhtest   #注意软链接的特点
drwxr-xr-x. 2 root root 42 10月 23 21:51 test1

权限管理命令

权限: r读 、w写、x执行 

权限对象:所有者、所属组、其他人

只有所有者或超级管理员才能更改权限

r  -> 4         w->2         x->1

chmod权限管理命令chmod 754 文件名称
chmod -R递归修改,就是如果、a/b/c,执行chmod -r 777 /a  ,那么,、b/c的权限也会和/a的权限一样被修改

 1、chmod修改文件目录权限

[root@localhost mxh]# ls -l 1.pid
-rwxr--r--. 1 root root 5 10月 23 22:18 1.pid

假设1.pid 原本的权限是 所有者rwx(4+2+1=7)  ;所属者r(4+0+0=4)  ;其他人 r(4+0+0=4)  1.pid的权限是744;我想要改成rwxrxw--(拆分就是754)

[root@localhost mxh]# ls -l 1.pid
-rwxr-xr--. 1 root root 5 10月 23 22:18 1.pid  #修改后

2、chmod -R递归修改文件目录权限,递归修改,就是如果、a/b/c,执行chmod -R 777 /a  ,那么,、b/c的权限也会和/a的权限一样被修改

[root@localhost mxh]# ls -l
总用量 0
-rw-r--r--. 1 root root  0 10月 24 10:07 mxhtest
lrwxrwxrwx. 1 root root  7 10月 24 10:07 mxhtest.soft -> mxhtest
drwxr-xr-x. 2 root root 42 10月 23 21:51 test1
drwxr-xr-x. 2 root root 32 10月 24 12:55 xxx    #未修改前
[root@localhost mxh]# ls -l xxx
总用量 8
-rw-r--r--. 1 root root 5 10月 23 22:18 2.pid    #未修改前
-rw-r--r--. 1 root root 5 10月 23 22:18 3.pid    #未修改前
[root@localhost mxh]# chmod -R 777 xxx          #递归修改xxx目录后,其底下的两个文件权限也跟着变
[root@localhost mxh]# ls -l
总用量 0
-rw-r--r--. 1 root root  0 10月 24 10:07 mxhtest
lrwxrwxrwx. 1 root root  7 10月 24 10:07 mxhtest.soft -> mxhtest
drwxr-xr-x. 2 root root 42 10月 23 21:51 test1
drwxrwxrwx. 2 root root 32 10月 24 12:55 xxx      #修改后
[root@localhost mxh]# ls -l xxx
总用量 8
-rwxrwxrwx. 1 root root 5 10月 23 22:18 2.pid     #修改后
-rwxrwxrwx. 1 root root 5 10月 23 22:18 3.pid     #修改后

目录权限

        对于目录来说,有以下权限就可以执行相应的操作:

           r -> ls查看

          w->mkdir\rm\rmdir\touch 删除和增加文件

           x-> cd 进入到目录下

有r权一定有x权限,不然查看不了目录的文件,进不去目录

chownchown 用户名 目标文件   更改目标文件的所有者
chgrpchgrp  用户名 目标文件     更改目标文件的所属组
umask -S查看当前创建目录有哪一些权限

1、chown 用户名 目标文件   更改目标文件的所有者


[root@localhost xxx]# ls -l
总用量 0
-rw-r--r--. 1 root root 0 10月 24 17:18 1.pid   #第一个root是所有者(一般是谁创建谁就是所有者)   第二个root是所属者
-rw-r--r--. 1 root root 0 10月 24 17:18 2.pid
[root@localhost xxx]# useradd mxh2
useradd:用户“mxh2”已存在
[root@localhost xxx]# chown mxh2 1.pid   #把所有者的身份从root转移到mxh2上
[root@localhost xxx]# ls -l
总用量 0
-rw-r--r--. 1 mxh2 root 0 10月 24 17:18 1.pid   #1.pid的所有者是mxh2
-rw-r--r--. 1 root root 0 10月 24 17:18 2.pid

2、chgrp  用户名 目标文件     更改目标文件的所属组


[root@localhost xxx]# ls -l
总用量 0
-rw-r--r--. 1 mxh2 root 0 10月 24 17:18 1.pid
-rw-r--r--. 1 root root 0 10月 24 17:18 2.pid
[root@localhost xxx]# chgrp mxhgrp 1.pid   #将1.pid所属者的身份从root变为mxhgrp用户
[root@localhost xxx]# ls -l
总用量 0
-rw-r--r--. 1 mxh2 mxhgrp 0 10月 24 17:18 1.pid   #成功修改权限
-rw-r--r--. 1 root root   0 10月 24 17:18 2.pid

3、chmod更改所有者和所属组的权限

[root@localhost xxx]# ls -l
总用量 0
-rw-r--r--. 1 mxh2 mxhgrp 0 10月 24 17:18 1.pid
-rw-r--r--. 1 root root   0 10月 24 17:18 2.pid
[root@localhost xxx]# ^C
[root@localhost xxx]# chmod 777 1.pid
[root@localhost xxx]# ls -l
总用量 0
-rwxrwxrwx. 1 mxh2 mxhgrp 0 10月 24 17:18 1.pid
-rw-r--r--. 1 root root   0 10月 24 17:18 2.pid

3、umask -S查看当前创建目录有哪一些权限

[root@localhost xxx]# ls
[root@localhost xxx]# umask -S   #针对创建目录查看其权限
u=rwx,g=rx,o=rx    
[root@localhost xxx]# mkdir test
[root@localhost xxx]# touch 1.pid
[root@localhost xxx]# ls -l  
总用量 0
-rw-r--r--. 1 root root 0 10月 24 18:48 1.pid  #为了安全性,一般新建的文件不具有可执行性
drwxr-xr-x. 2 root root 6 10月 24 18:48 test  #与umask -S 的权限一致

umask创建目录时的权限是怎么得来的:

[root@localhost ~]# umask
0022
#第一个0 :特殊权限
#022 对应一下:
#    ----w--w-    然后与777异或取值
#    rwxrwxrwx
----------------------
#    rwxr-xr-x     所以看到创建的目录是rwxr-xr-x

文件搜索命令

findfind会占用服务器大量的系统资源,尽量少用
find 目录范围  -name  文件名 (区分大小写查找)find 目录范围  -name  文件名  (精准查找)或 find 目录范围  -name *文件名* (只要有这个文件名的都找出来)
find 目录范围  -iname  文件名 (不区分大小写查找) 不区分大小写进行查找

1、find 目录范围  -name  文件名 (区分大小写查找) 精准查找和广泛查找(区分大小写查找)

[root@localhost ~]# find /etc -name init
/etc/sysconfig/init
/etc/selinux/targeted/active/modules/100/init
[root@localhost ~]# find /etc -name *init*
/etc/systemd/system/sysinit.target.wants
/etc/inittab
/etc/sysconfig/init
/etc/sysconfig/network-scripts/init.ipv6-global
/etc/init.d
/etc/rc.d/init.d
/etc/selinux/targeted/active/modules/100/init
/etc/selinux/targeted/contexts/initrc_context
/etc/security/namespace.init

2、find 目录范围  -iname  文件名 (不区分大小写查找) 


[root@localhost mxh]# cd xxx
[root@localhost xxx]# touch INIT
[root@localhost xxx]# ls
1.pid  INIT  test                       #在这里创建一个INIt文件

[root@localhost xxx]# find / -name init   #查找不到INIT
/sys/fs/selinux/initial_contexts/init 
/etc/sysconfig/init
/etc/selinux/targeted/active/modules/100/init
/usr/sbin/init
/usr/libexec/os-probes/init

[root@localhost xxx]# find / -iname init     #不区分大小写查找,查到了INIT文件
/sys/fs/selinux/initial_contexts/init
/etc/sysconfig/init
/etc/selinux/targeted/active/modules/100/init
/tmp/mxh/xxx/INIT                                #这里
/usr/sbin/init
/usr/libexec/os-probes/init
find  /目录 -size +n(大于)/-n(小于)/n(等于)根据文件大小来查找
find  /目录 -user/-group  文件名根据用户来查找文件
find /目录 -amin  -n分钟查找在n分钟访问过的文件目录
find /目录  -cmin  -n分钟根据在n分钟内更改过的文件来查找
find /目录  -mmin  -n分钟根据在n分钟内文件内容被修改来查找

1、根据文件大小来查找


[root@localhost ~]# find /tmp -size -1k
/tmp/yum.log
/tmp/mxh/test1/cron.reboot
/tmp/mxh/mxhtest
/tmp/mxh/xxx/1.pid
/tmp/mxh/xxx/INIT

2、根据用户来查找  find  /目录 -user/-group  文件名


[root@localhost ~]# find / -user mxh2 #查找用户mxh2的所有文件
/run/user/1000
/var/spool/mail/mxh2
/home/mxh2
/home/mxh2/.bash_logout
/home/mxh2/.bash_profile
/home/mxh2/.bashrc
/home/mxh2/.lesshst
/home/mxh2/.bash_history

3、find /目录 -amin  -n分钟 查找在n分钟访问过的文件目录


[root@localhost ~]# find /tmp -amin -100
/tmp/mxh/test1

4、find /目录  -cmin  -n分钟  根据在n分钟内更改过的文件来查找


[root@localhost ~]# find /tmp -cmin -200
/tmp/mxh/xxx
/tmp/mxh/xxx/INIT

5、find /目录  -mmin  -n分钟  根据在n分钟内文件内容被修改来查找


[root@localhost ~]# find /tmp -mmin -300
/tmp
/tmp/mxh/xxx
/tmp/mxh/xxx/test
/tmp/mxh/xxx/1.pid
/tmp/mxh/xxx/INIT
/tmp/systemd-private-e90722c3e92448538e73ca7ff7af2bf4-chronyd.service-UmZK4C
/tmp/systemd-private-e90722c3e92448538e73ca7ff7af2bf4-chronyd.service-UmZK4C/tmp
/tmp/vmware-root_713-4290166671
find  /目录 -type f查找文件(f是文件)
find  /目录 -type d查找目录(d是目录)
find  /目录 -type l查找软连接(l是软连接)
连接符:-a 条件同时满足
连接符:-o条件满足其中之一
find /目录 -iname 文件名  -exec ls -l {} \;解读:可以把ls -l 改成其他命令的组合;意思就是找到这些文件后你要操作什么步骤

1、find  /目录 -type f   查找文件(f是文件)


[root@localhost ~]# find /tmp -type f
/tmp/yum.log
/tmp/ks-script-IAr5pa
/tmp/mxh/test1/cron.reboot
/tmp/mxh/test1/crond.pid
/tmp/mxh/mxhtest
/tmp/mxh/xxx/1.pid
/tmp/mxh/xxx/INIT

2、find  /目录 -type d  查找目录(d是目录)


[root@localhost ~]# find /tmp -type d
/tmp
/tmp/.XIM-unix
/tmp/.font-unix
/tmp/.X11-unix
/tmp/.ICE-unix
/tmp/.Test-unix

3、find  /目录 -type l  查找软连接(l是软连接)


[root@localhost ~]# find /tmp -type l
/tmp/mxh/mxhtest.soft

4、连接符:-a  条件同时满足

oot@localhost ~]# find /etc -iname *init* -a -type f  
/etc/inittab
/etc/sysconfig/init
/etc/sysconfig/network-scripts/init.ipv6-global
/etc/selinux/targeted/contexts/initrc_context
/etc/security/namespace.init
[root@localhost ~]# find /etc -iname *init* -a -type l
/etc/init.d
[root@localhost ~]# find /etc -iname *init* -a -type d
/etc/systemd/system/sysinit.target.wants
/etc/rc.d/init.d
/etc/selinux/targeted/active/modules/100/init

5、find /目录 -iname 文件名  -exec ls -l {} \;  {}表示前面找到的内容的一个集合

解读:可以把ls -l 改成其他命令的组合;意思就是找到这些文件后你要操作什么步骤


[root@localhost ~]# find /etc -iname init -a -type f
/etc/sysconfig/init
[root@localhost ~]# find /etc -iname init -a -type f -exec ls -l {} \;
-rw-r--r--. 1 root root 798 10月 13 2020 /etc/sysconfig/init

6、find和cp命令结合使用 {}表示前面找到的内容的一个集合


[root@localhost ~]# find /etc -iname init -a -type f -exec cp {} /tmp/mxh/xxx \;
[root@localhost ~]# ls -l /tmp/mxh/xxx
总用量 4
-rw-r--r--. 1 root root   0 10月 24 18:48 1.pid
-rw-r--r--. 1 root root 798 10月 24 22:44 init
-rw-r--r--. 1 root root   0 10月 24 19:21 INIT
drwxr-xr-x. 2 root root   6 10月 24 18:48 test

7、find . -inum 序号 -exec rm {} \;     {}表示前面找到的内容的一个集合


[root@localhost xxx]# touch 1.pid
[root@localhost xxx]# ls -il
总用量 0
33583818 -rw-r--r--. 1 root root 0 10月 25 13:04 1.pid
[root@localhost xxx]# find . -inum 33583818 -exec rm -rf {} \;
[root@localhost xxx]# ls -il
总用量 0
locate查找命令,首先安装yum install mlocate ;再更新库 updatedb;再进行 locate 文件名 进行查找 (基于数据库检索)
locate -i 文件名不区分大小写进行查找

1、locate 文件名  基于数据库查找 ;但是,存在临时文件的查找不了


[root@localhost ~]# yum install mlocate  #安装
已加载插件:fastestmirror
[root@localhost ~]# updatedb      #更新数据库

[root@localhost ~]# locate mxh    #关键字查找
/home/mxh2
/home/mxh2/.bash_history
/home/mxh2/.bash_logout
/home/mxh2/.bash_profile
/home/mxh2/.bashrc
/home/mxh2/.lesshst
/var/spool/mail/mxh2

 2、临时文件下面的查找不到


[root@localhost ~]# ls /tmp/mxh
mxhtest  mxhtest.soft  test1  xxx
[root@localhost ~]# locate test1
[root@localhost ~]#

3、locate -i 文件名   不区分大小写进行查找


[root@localhost home]# ls -il
总用量 0
33583836 -rw-r--r--. 1 root root  0 10月 25 19:38 MXh1
51042340 drwx------. 2 mxh2 mxh2 99 10月 24 17:30 mxh2
[root@localhost home]# updatedb
[root@localhost home]# locate -i mxh1
/home/MXh1
which 命令快速查找命令所在位置
whereis 命令快速查找命令和帮助文档所在位置
grep -i 关键词 文件目录可以不区分关键词大小写来进行查找文档内容

1、which 命令    快速查找命令所在位置


[root@localhost ~]# which groupadd
/usr/sbin/groupadd
[root@localhost ~]# which ls
alias ls='ls --color=auto'
 /usr/bin/ls

2、whereis 命令  同样能找到命令所在位置,还能知道帮助文件所在位置,whereis路径多出来的部分就是帮助文档的路径  以下:/usr/share/man/man1/ls.1.gz  就是帮助文档位置。


[root@localhost ~]# whereis ls
ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz
[root@localhost ~]# which ls
alias ls='ls --color=auto'
 /usr/bin/ls

3、grep -i 关键词 文件目录  可以不区分关键词大小写来进行查找文档内容

[root@localhost ~]# more /etc/fstab

#
# /etc/fstab
# Created by anaconda on Fri Oct 22 21:28:51 2021
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/centos-root /                       xfs     defaults        0 0
UUID=57a167f9-7601-4c24-93e7-d2916c3a2445 /boot                   xfs     defaults        0
 0
/dev/mapper/centos-swap swap                    swap    defaults        0 0
[root@localhost ~]# grep -i created /etc/fstab
# Created by anaconda on Fri Oct 22 21:28:51 2021  #检索结果直接打出关键词的这一行

[root@localhost ~]# grep -i create /etc/fstab       #不完全输入完也能找出来
# Created by anaconda on Fri Oct 22 21:28:51 2021

帮助命令

man  命令man 命令  查看命令的帮助信息
man  配置文件查看配置文件的帮助内容
命令 --help也是可以查看帮助命令

安装man命令前要提前安装好库,一共一条语句:

、sudo yum install man-pape

1、man 命令  查看命令的帮助信息

[root@localhost ~]# man ls  #就能查看ls这个命令的使用法内容

 2、man  配置文件    查看配置文件的帮助内容

[root@localhost ~]# man services  #就能查看services这个文档的帮助内容

3、


[root@localhost ~]# whereis passwd
passwd: /usr/bin/passwd /etc/passwd /usr/share/man/man1/passwd.1.gz /usr/share/man/man5/passwd.5.gz
# 1.gz  是指命令帮助信息
# 5.gz  是指配置文件帮助信息

3.1 如果直接man passwd   它就会优先显示passwd的命令帮助信息

[root@localhost ~]# man passwd

3.2 想要显示配置文件信息可以这样做

[root@localhost ~]# man 5 passwd  #因为5是配置文件信息,声明一下

4、命令 --help 查看命令帮助

[root@localhost ~]# ls --help

用户管理命令

管理员可以更改任何人的密码;而普通用户只能更改自己的密码;

useradd增加用户
passwd更改密码
who查看有谁在登陆linux

1、who  查看谁登陆服务器


[yangmi@localhost ~]$ who
root     tty1         2021-10-25 19:19       #tty1  表示本地登录
root     pts/0        2021-10-25 19:19 (192.168.32.1)    #pts1  表示远程登陆1  登录时间  登陆ip地址
yangmi   pts/1        2021-10-25 21:41 (192.168.32.1)    #pts2  表示远程登陆2  登录时间  登陆ip地址

2、w  这个命令可以详细查看用户登录信息


[yangmi@localhost ~]$ w
 21:56:38 up  2:41,  3 users,  load average: 0.08, 0.03, 0.05  #
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     tty1                      19:19    2:37m  0.12s  0.12s -bash
root     pts/0    192.168.32.1     19:19   12:30   6.73s  6.73s -bash
yangmi   pts/1    192.168.32.1     21:41    6.00s  1.56s  1.20s w

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值