【自己练习】linux常见命令——(六)

菜鸟教程命令大全    http://www.runoob.com/linux/linux-command-manual.html

命令大全:      http://man.linuxde.net/

 

tab :自动补全

Ctrl+l 清屏 (类似于clear命令)

Ctrl+U 清空当前输入

 

ctrl+c  关闭

man command 帮助

        man :manual : 手工,帮助 ,帮助命令,好比windows help


          命令: man ls

 

 

 

文件命名规则:

 

1)除了/都是合法的字符

 

2)有些字符最好不要用,像空格、制表符号、退格符和字符@#$%-等符号。(linux下面命令和参数之间用空格分割)

 

3)避免使用.作为普通文件的第一个字符  (linux下面以.开头的文件表示隐藏文件)

 

4)大小写敏感。(windows下面不区分大小写)

 

 

 

命令存在目录与权限关系:  (可以用which  ls查看ls存在的目录)

  • 只有管理员可以运行的命令所在目录:

/sbin

/usr/sbin/

  

  • 所有用户可以执行的命令存在目录:

/bin

/usr/bin

 

 

bin-binary  二进制文件,linux所有的文件都是二进制文件,是一个可执行的二进制文件

use -user 

/sbin    super binary

 

例如:查看chmod的路径与运行权限:

[root@iz2ze46xi6pjjj69ailg9lz dir1]# which chmod
/usr/bin/chmod
[root@iz2ze46xi6pjjj69ailg9lz dir1]# ll /usr/bin/chmod
-rwxr-xr-x. 1 root root 58584 Nov  6  2016 /usr/bin/chmod

 

   也可以通过chmod改变其运行权限

 

 

 

 

命令格式:  

   命令 -选项 参数     (选项代表以什么形式展示,参数代表针对谁操作)

  例子:   ls -la /etc

说明:1)当有多个选项的时候可以写在一起

  2)两个特殊的目录.和..分别代表当前目录和当前目录的父目录。~代表用户工作目录,/代表根目录

 

 -----------------------------------基本管理命令--------------------------

1.who与whoami,whois与w与id与groups

[root@VM_0_12_centos ~]# who    #查看当前在线用户
qlq      pts/0        2018-03-28 19:47 (218.26.179.150)
root     pts/1        2018-03-28 19:46 (218.26.179.150)
[root@VM_0_12_centos ~]# w     #查看活动用户
 19:49:07 up 8 days, 10:30,  2 users,  load average: 0.00, 0.02, 0.05
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
qlq      pts/0    218.26.179.150   19:47    1:23   0.01s  0.01s -bash
root     pts/1    218.26.179.150   19:46    3.00s  0.01s  0.00s w
[root@VM_0_12_centos ~]# whoami  #查看我是谁
root
[root@VM_0_12_centos ~]# id  #查看当前用户的组信息
uid=0(root) gid=0(root) groups=0(root)

 whois查询域名信息

[root@VM_0_12_centos ~]# whois qiaoliqiang.cn
Domain Name: qiaoliqiang.cn
ROID: 20170902s10001s95633482-cn
Domain Status: ok
Registrant ID: hc2862611690203
Registrant: 乔利强
Registrant Contact Email: 954318444253555555508@qq.com
Sponsoring Registrar: 阿里云计算有限公司(万网)
Name Server: dns31.hichina.com
Name Server: dns32.hichina.com
Registration Time: 2017-09-02 08:22:50
Expiration Time: 2018-09-02 08:22:50
DNSSEC: unsigned

 

查看一个用户属于哪个组:
[root@iz2ze46xi6pjjj69ailg9lz ~]# groups qlq #查看qlq属于哪个组
qlq : qlq mygroup
或者:
[root@iz2ze46xi6pjjj69ailg9lz ~]# id qlq #查看qlq用户的UID和组信息
uid=1000(qlq) gid=1000(qlq) groups=1000(qlq),888(mygroup)
groups和id如果不加后面的参数默认查询当前用户的组信息。

 

 

 

2:显示“$”标识表示是普通用户, 显示“#”标识表示是超级管理员
  例如: 进入linux系统,控制台出现:itcast@ubuntu:~$
      ~用户的根目录
3:切换用户与设置密码

  •   切换用户(使用 su 命令切换用户):

    当从普通用户切换到root用户(超级管理员)或其他用户时,需要输入目标用户的密码。
    当从root用户切换到普通用户时,不需要输入密码。

[root@VM_0_12_centos ~]# su qlq
[qlq@VM_0_12_centos root]$ su root
Password:
[root@VM_0_12_centos ~]#

 

 

直接su环境变量以及目录不会改变,su - username会改变用户的环境变量与工作目录:

[root@localhost ~]# su - hadoop
[hadoop@localhost ~]$ pwd
/home/hadoop

 

 

 

  • 修改用户密码  passwd username   修改用户密码(只有root有权限)
[root@VM_0_12_centos qlq]# passwd qlq
Changing password for user qlq.
New password:
BAD PASSWORD: The password is shorter than 7 characters
Retype new password:
passwd: all authentication tokens updated successfully.

   

  •  修改组密码    gpasswd grpname(修改组的密码)

 

1.注销、关机、重启

  - 注销 :logout :登出 、exit    (只是退出账号)

     - 关机 :(需要成为rwwwt用户或者被rwwwt授予权限)

        shutdown - h  时间

           - h :关机

           - 时间 :

             1. now :马上

             2. 12.30 :指定具体时间

             3. 3  :几分钟以后

          sudo : superuser do :由超级用户来执行该命令

            要配置sudo 命令 : 授权 哪些用户能执行哪些命令

             由超级用户配置  sudo

              /etc/sudoers

            sudo shutdown -h now    当前账号:itcast 

     - 重启 :

    reboot : 相当于重启服务器

          shutdown -r  时间

                 -r :restart

 

 
---------------------------------------文件操作命令--------------------------------------------

   

1. ls   : 查看目录内容  (ll是ls -l的别名 , alias ll可以查看别名)

英文名称:list

命令所在目录:

语法: ls 选项[-ald] 文件或目录

    -a   all,显示索引文件,包括隐藏文件。

    -l   long,显示详细信息

    -d   查看目录属性

    -i   查看inode(inode用于内核操作,每个文件都有一个inode)

例如:

ls -l

-rw-r--r--  1    root   root       14744393    Jan 27 13:50         exam.sql

1-此文件的硬连接数 所有者 所属组 文件大小 创建时间或最后修改时间 文件或目录名称

 

解释:
(1)-rw-r--r--代表文件的类型与权限
首位代表文件类型:  d  目录

         -  二进制文件

         l  软连接文件,link  

  接下来每三个是一组,依次代表所有者、所属组、其他人对此文件权限,代表文件的权限依次是:r-read(4)   w-write(2)  x-execute(1)  -:代表没权限(0)

  所有者:u  user

     g   group

     o others

     a    所有人    

 

 

2. mkdir :创建目录

          Desktop:

             - java

               - JEE : aa.txt  bb.txt 

               - Android

3. cd  切换目录

         cd ..  回到上级目录

         cd ./java  进入当前目录的子目录

          cd ../xxx  进入上一级目录的子目录

    cd ~  进入用户自己的工作目录

4. touch  :创建一个空白的普通文件

        touch aa.txt

 

5. echo  :把内容重定向到指定的文件中 ,有则打开,无则创建

                echo "content">bb.txt    (有时候我们不希望错误信息打印出来可以重定向到/dev/null  会自动回收这些信息)

  

/dev/null 是 Unix/Linux 里的【无底洞
任何的 output 送去了【无底洞】就再也没了。相信我,真的没了!
那麼有人问,在什麼情况下要把 output 送去这无底洞呢?
这里没有标准答案,
不过一般呢,要是你不想看到 output 或者output 太多太大了,有可能把硬碟给挤爆了的时候,程序的设计就会考虑把 output 送到 /dev/null 了。

6. cat、more、less  :查看文件内容

     (1) cat :查看文件内容(适用于查看内容比较少的文件,只能显示最后一页的文件)

cat   test.txt

 

 

 

  (2)more: 分页显示文件内容

more ./catalina.out

 

    空格或者f显示下一页

    Enter显示下一行

    q或者Q退出more的浏览状态

 

  (3)less 分页显示文本文件内容

                1:空格 向下翻一页
                2:PageDown 向下翻一页
                3:PageUp 向上翻一页

     4.Enter显示下一行

                5:q 离开

 

参考:http://blog.csdn.net/xyw_blog/article/details/16861681

 


  7.     head,tail,sed分别显示文件开头和结尾内容和按指定行显示内容

  • head:查看文件制定前多少行

例如查看文件前20行:

 

  head -n 20 xxx.txt  或者   head -20 xxx.txt

 

  •  tail:制定最后多少行与实时查看

查看文件前20行

  tail -n 20 xxx.txt   或者  tail -20 xxx.txt

 

实时查看日志问:

tail -f xxx.log

 

  • sed:查看指定的行数

查看文件中间一段,你可以使用sed命令,如: 

sed -n '8,9p' catalina.2018-03-15.log

这样你就可以只查看文件的第5行到第10行。  

 

    

  8. cp、mv、rm    复制、移动、删除文件

  •     cp      :复制

    复制文件:将bj.txt复制到当前目录/java/jee目录下

                   cp bj.txt ./java/jee    

 

 

    复制文件加-R参数:将/etc目录整体复制到/test目录下

[root@iz2ze46xi6pjjj69ailg9lz test]# cp -R /etc /test
[root@iz2ze46xi6pjjj69ailg9lz test]# ls /test/
etc  test1  test.sh

 

 

  •  mv :     移动、重命名(移动)

(1)移动:  将当前目录下的test.txt移动到当前目录下mydir,名字也是test.txt

[root@iz2ze46xi6pjjj69ailg9lz ~]# mv ./test.txt ./mydir/
[root@iz2ze46xi6pjjj69ailg9lz ~]# ls mydir/
test.txt

 

 

(2)移动并且重命名:将当前目录下的exam1.31.sql 移动打 mydir目录下且重命名为exam.sql

[root@iz2ze46xi6pjjj69ailg9lz ~]# mv ./exam1.31.sql ./mydir/exam.sql
[root@iz2ze46xi6pjjj69ailg9lz ~]# ls ./mydir/
exam.sql  test.txt

 

 

(3)重命名:   假如移动的文件存放在同一个目录中 ,则是重命名

               mv bb.txt cc.txt

 

 

  •  rm :删除文件或者文件夹
删除文件:直接 rm ww.text

删除目录:   rm -rf  ww

 

               -f : 假如要删除的文件不存在,也不提示

               -i : 删除前提示 ,默认不删除,要删除,输入y

               -d : 删除空白目录

               -r :递归删除

 

   9. wc :word count :统计字符数

  wc [选项]  [文件名称]

    -c 统计毕byte数

    -m  统计characters数

    -l    统计行数

    -w   words数量

[root@VM_0_12_centos ~]# wc -c /etc/services
670293 /etc/services
[root@VM_0_12_centos ~]# wc -m /etc/services
670281 /etc/services
[root@VM_0_12_centos ~]# wc -l /etc/services
11176 /etc/services
[root@VM_0_12_centos ~]# wc /etc/services
 11176  61033 670293 /etc/services

 

 

 

   10. ln :创建连接文件

  硬连接相当与源文件的一个复制,只是会同步更新,删除原文件不会影响硬连接;软连接相当于windows的快捷方式,访问软连接实际是访问源文件,删除源文件则软连接相当于没有用了。

 

(1)硬连接:类似于cp   只是两个文件会同步(文件内容更新同步,删除的时候不会影响硬连接,硬链接是有着相同 inode 号仅文件名不同的文件(每个文件都有一个inode,i节点用于内核操作))

        - 默认创建的是硬连接,好比复制 ,但是两个文件会同步

            命令:ln ./java/android/aa.txt  aaa

 

  例如创建一个硬连接并查看其inode发现硬连接与源文件有着相同的inode,因此可以实现同步

[root@iz2ze46xi6pjjj69ailg9lz ~]# ln test ./test.li
[root@iz2ze46xi6pjjj69ailg9lz ~]# ls -li|grep test
132542 -rw-r--r--  2 root root        40 Sep  1  2017 test
132542 -rw-r--r--  2 root root        40 Sep  1  2017 test.li

 

 

    注意:

      硬链接不允许跨分区.(也就是假如windows允许硬链接,不允许将C盘的东西硬链接到D盘,实际windows不存在硬链接)

(2)软连接:

         - s :创建的是软连接变为指向(类似于windows的快捷方式,执行快捷方式实际是执行原文件)

       

  软连接的文件类型是l,权限是所有人可以读写执行,其实操作软连接的时候还是会去操作其源文件。类似于windows 的快捷方式,我们点击快捷方式的时候还是会去执行源文件,因此快捷方式也很小,执行的时候实际是去执行源文件。源文件删除之后,软连接没有任何意义。文件类型是l(link),箭头指向源文件。可以跨分区实现软连接。

 

例如:

qq.sh

num=3
#!/bin/bash
case $num in
1)
    echo "num=1";;
2)
    echo "num=2";;
3)
    echo "num=3";;
4)
    echo "num=4";;
*)
    echo "defaul";;
esac

 

赋予可运行权限:

chmod +x qq.sh

 

创建一个目录ww,并在ww目录创建一个可运行文件qq.sh的硬连接并运行:

[rwwwt@iz2ze46xi6pjjj ~]# mkdir ww
[rwwwt@iz2ze46xi6pjjj ~]# cd ww
[rwwwt@iz2ze46xi6pjjj ww]# ls
[rwwwt@iz2ze46xi6pjjj ww]# ln ../qq.sh  qq
[rwwwt@iz2ze46xi6pjjj ww]# ls
qq
[rwwwt@iz2ze46xi6pjjj69ailg9lz ww]# ./qq
num=3

 11. pwd   :查看当前目录的绝对路径

  英文名称:  print working directory

  12. 管道命令 |    (前一个命令的结果作为后一个命令的输入)

       命令: ls -la | wc

  13. 重定向(添加文本内容)

        - > :覆盖模式

         命令:  echo "ww">aaa

         - >>:追加模式

         命令: echo "ww">>aaa

 

 

 

 

--------------------------------------------文件权限相关命令--------------------------

1.    chmod  改变文件或目录

  change the permissions mode of a file 

语法:

  chmod [{ugo}{+-=}{rwx}] [文件或者目录]

      [mode=421] [文件或者目录]

 

      

      用户类型:u g o a 分别表示所属用户,所属组用户,其他用户,所有用户

      +-=:分别表示   增加、删除、将权限设为。。。。

      rwx-:分别代表读、写、可运行、没权限

 

权限的数字表示法:(每三个一组,r为4,w为2,x为1,-为0)

  rwxr-xr--  对应是数字权限:  754  

  641   rw-r----x                            

 

例如:

(1)创建一个文件:

touch test.txt

 

(2)查看文件权限:

[root@iz2ze46xi6pjjj69ailg9lz ~]# ll | grep test.t
-rw-r--r--  1 root  root          0 Mar 27 21:06 test.txt

 

在Linux中第一个字符代表这个文件是目录、文件或链接文件等等。

  • 当为[ d ]则是目录
  • 当为[ - ]则是文件;
  • 若是[ l ]则表示为链接文档(link file);
  • 若是[ b ]则表示为装置文件里面的可供储存的接口设备(可随机存取装置);
  • 若是[ c ]则表示为装置文件里面的串行端口设备,例如键盘、鼠标(一次性读取装置)。

  接下来的字符中,以三个为一组,且均为『rwx』 的三个参数的组合。其中,[ r ]代表可读(read)、[ w ]代表可写(write)、[ x ]代表可执行(execute)。 要注意的是,这三个权限的位置不会改变,如果没有权限,就会出现减号[ - ]而已。

  r=读取属性  //值=4
  w=写入属性  //值=2
  x=执行属性  //值=1

   -   没有权限      //值=0

每个文件的属性由左边第一部分的10个字符来确定(如下图)。

 

 

(3)将文件的所有权限都收回:

[root@iz2ze46xi6pjjj69ailg9lz ~]# chmod a=- test.txt
[root@iz2ze46xi6pjjj69ailg9lz ~]# ll | grep test.t
----------  1 root  root          0 Mar 27 21:06 test.txt

 

 

(4)赋予该文件属主读写权限,属组权限为读,其他用户为读

[root@iz2ze46xi6pjjj69ailg9lz ~]# chmod u+rw,g+r,o+r test.txt
[root@iz2ze46xi6pjjj69ailg9lz ~]# ll | grep test.t
-rw-r--r--  1 root  root          0 Mar 27 21:06 test.txt

 

 

参考:http://www.cnblogs.com/qlqwjy/p/7464509.html

 

 

(5) 利用数字表示权限以及设置权限

[root@iz2ze46xi6pjjj69ailg9lz ~]# ll |grep Exam.war
-rw-r--r--  1 root  root  213608341 Jan 27 22:19 Exam.war

 

 上面的权限用数字法表示为:644

现在我们修改这个文件的权限为:所属主不变,所属组增加写权限,其他人增加可执行权限。

权限应为:rw- rw- r-x   对应的数字是:665 

[root@iz2ze46xi6pjjj69ailg9lz ~]# chmod 665 Exam.war   
[root@iz2ze46xi6pjjj69ailg9lz ~]# ll |grep Exam.war
-rw-rw-r-x  1 root  root  213608341 Jan 27 22:19 Exam.war

 

 

 权限总结:  (删除一个文件需要对当前文件所在的目录有写权限)

 

 

2.chown  改变文件或者目录的所有者:

   change file ownership 

语法:chown   [用户] [文件或者目录]  

如下:将mbedtls-2.6.0目录的所属主改为root

[root@iz2ze46xi6pjjj69ailg9lz ~]# ll
drwxrwxr-x 11 17608 17608      4096 Aug 22  2017 mbedtls-2.6.0

[root@iz2ze46xi6pjjj69ailg9lz ~]# chown root mbedtls-2.6.0
[root@iz2ze46xi6pjjj69ailg9lz ~]# ll
drwxrwxr-x 11 root 17608      4096 Aug 22  2017 mbedtls-2.6.0

 

 

3.chgrp   改变文件或目录的所属组

  change file group ownership

语法: chgrp [组名] [文件或目录]

 

例如:将mbedtls-2.6.0目录的所属组改为root

[root@iz2ze46xi6pjjj69ailg9lz ~]# chgrp root mbedtls-2.6.0
[root@iz2ze46xi6pjjj69ailg9lz ~]# ll|grep mbedtls-2.6.0
drwxrwxr-x 11 root root      4096 Aug 22  2017 mbedtls-2.6.0
-rw-r--r--  1 root root   1958070 Mar 14 23:53 mbedtls-2.6.0-gpl.tgz

 

 

4.umask  显示、设置创建一个文件或者目录的默认权限:

语法: umask [-S]

  -S以rwx形式显示文件或者目录的缺省权限

 

例如;

[root@iz2ze46xi6pjjj69ailg9lz ~]# umask
0022
[root@iz2ze46xi6pjjj69ailg9lz ~]# umask -S
u=rwx,g=rx,o=rx

 

 

解释:

(1)0022是以数字形式显示缺省权限

0:权限特殊位

022:用户权限位,权限掩码值(类似于子网掩码)

  777 - 022 = 755

 

也就是一个文件或者目录的缺省权限值是755(rwxr-xr-x)

验证:

  创建一个目录并验证其缺省权限:(确实是755)

[root@iz2ze46xi6pjjj69ailg9lz dir1]# mkdir test
[root@iz2ze46xi6pjjj69ailg9lz dir1]# ll
total 4
drwxr-xr-x 2 root root 4096 Mar 28 12:28 test

 

 

 

  创建一个文件并且验证其缺省权限:

[root@iz2ze46xi6pjjj69ailg9lz dir1]# touch xxx.txt
[root@iz2ze46xi6pjjj69ailg9lz dir1]# ls |grep xxx
xxx.txt
[root@iz2ze46xi6pjjj69ailg9lz dir1]# ll |grep xxx
-rw-r--r-- 1 root root    0 Mar 28 12:29 xxx.txt

 

  发现创建的文件缺省权限是:rw-r--r--(644),原因;

    linux将创建文件的可执行权限x去掉了,可以是防止病毒,文件不能执行也就没啥用,因此文件的缺省权限是在755的基础上-111,也就是644

 

(2)修改缺省权限(一般是不需要改变的,默认的linux的所有权限值是755)

  现在加入我们非要改变文件的缺省权限是750(rwxr-x---),设置的时候需要使用掩码值:  777-750=027

[root@iz2ze46xi6pjjj69ailg9lz dir1]# umask 027
[root@iz2ze46xi6pjjj69ailg9lz dir1]# umask
0027
[root@iz2ze46xi6pjjj69ailg9lz dir1]# umask -S
u=rwx,g=rx,o=

 

 

进行测试:(文件也是默认将x权限去掉)

[root@iz2ze46xi6pjjj69ailg9lz dir1]# ll | grep ttt
drwxr-x--- 2 root root 4096 Mar 28 12:35 tttt
[root@iz2ze46xi6pjjj69ailg9lz dir1]# touch xxx.txt
[root@iz2ze46xi6pjjj69ailg9lz dir1]# ll | grep xxx.
-rw-r--r-- 1 root root    0 Mar 28 12:36 xxx.txt

 

关于linux下的s、t、i、a权限参考:http://www.cnblogs.com/qlqwjy/p/8665871.html

 

 

 

------------------------------------文件与命令搜索常用命令------------------------------------------

1.which ,whereis  搜索命令所在绝对路径(显示系统命令所在目录)

  语法    which [命令名称]

[root@iz2ze46xi6pjjj69ailg9lz dir1]# which ls
alias ls='ls --color=auto'
        /usr/bin/ls

 

   which可以提供这个命令的别名信息

 

  语法  whereis [命令名称]

[root@iz2ze46xi6pjjj69ailg9lz dir1]# whereis chmod
chmod: /usr/bin/chmod /usr/share/man/man1/chmod.1.gz

 

    whereis可以提供这个命令的帮助信息

 

2.find    文件搜索命令:

语法: find [搜索路径] [搜寻关键字]    注意:搜索路径最好不要从根目录查找,太占内存。占用系统的资源越少越好。

连接符:

  -a   and   逻辑与

  -o   or  逻辑或

 

 

  •   -name  根据文件名进行查找

例如:

(1)在/root目录下查找xxx.txt文件

[root@iz2ze46xi6pjjj69ailg9lz ~]# find /root -name xxx.txt
/root/dir1/xxx.txt

 

 

也可以使用通配符: *   任意n个字符         ?任意单个字符

(2)在/root目录下查找xxx.txt文件(使用*通配符)

[root@iz2ze46xi6pjjj69ailg9lz ~]# find /root -name xxx*
/root/dir1/xxx.txt
[root@iz2ze46xi6pjjj69ailg9lz ~]# find /root -name *xxx*
/root/dir1/xxx.txt

 

 

(3)在/root目录下查找xxx.txt文件(使用?通配符)

[root@iz2ze46xi6pjjj69ailg9lz ~]# find /root -name xxx.tx?
/root/dir1/xxx.txt

 

 

  • -size  根据文件大小查找  block:数据块  512字节=0.5KB  (数据块的概念可以理解为一个屋子放的是大沙发还是椅子,而坐的人就是数据,沙发和椅子就是数据块)   单位是KB

  100MB=?block

  100MB=102400KB=204800block

例如:/root目录下查找大于200M的文件  (小于用-,等于不用符号)

[root@iz2ze46xi6pjjj69ailg9lz ~]# find /root -size +409600
/root/tomcat项目/exam电厂培训管理系统/Exam.war
/root/Exam.war

 

 

 

在/root目录下查找文件大于80M小于100M的

find /root -size +163840 -a -size -204800

 

 

  • -user  查找指定所有者的文件

例如:在/root/dir1/目录下查找所有者是root的文件

[root@iz2ze46xi6pjjj69ailg9lz ~]# find /root/dir1/ -user root
/root/dir1/
/root/dir1/tttt
/root/dir1/xxx.txt
/root/dir1/test

 

 

  • 按时间查询文件

1.按天查找       ctime   atime  mtime 

2.按分钟查找   cmin   amin    mmin

c-change改变(表示文件的属性被修改,比如说所有者,所属组,权限)

a-acess访问  (被看过、浏览过)

m-modify   (表示文件的内容被修改过)

-之内,+超过,不带符号表示时间值

 

例如:

查找120分钟内文件被改过的文件:

[root@iz2ze46xi6pjjj69ailg9lz ~]# find /root/ -mmin -120
/root/
/root/dir1
/root/dir1/tttt

 

 

  •  -type       根据文件类型查找

f    file:文件

d   directory   目录

l  link    软连接

 

例如:查找所有的软连接:

find /root/ -type l

 

  • 连接操作符:  (重要)

(1)第一组:   连接符:

  -a   and   逻辑与

  -o   or  逻辑或

查看120分钟之内被修改过的文件:

find /root/ -amin -120 -a -type f

 

 

(2)-exec  (也可以用-ok代替,-ok有询问功能)    查出文件并对结果执行一些额外操作

  语法:find ..... -exec  命令 {} \;

             {}表示find查询的结果

              \转义符,符号命令本身使用的含义

            ; 结果

 

 例如:查询一个以xxx开头的文件并显示其详细信息

[root@iz2ze46xi6pjjj69ailg9lz ~]# find /root -name xxx* -exec ls -l {} \;
-rw-r--r-- 1 root root 221 Mar 28 13:02 /root/dir1/xxx.txt

 

  

用-ok的时候有询问过程:

[root@iz2ze46xi6pjjj69ailg9lz ~]# find /root -name xxx* -ok ls -l {} \;
< ls ... /root/dir1/xxx.txt > ? y
-rw-r--r-- 1 root root 221 Mar 28 13:02 /root/dir1/xxx.txt

 

 

 

 

例如:查询一个以xxx开头的文件并删除此文件

[root@iz2ze46xi6pjjj69ailg9lz ~]# find /root -name *xx.txt -ok rm -rf {} \;
< rm ... /root/dir1/xxx.txt > ? y

 

 

 

  •  -inum     通过inode节点查找文件(通过inode与上面的-exec结合可以实现操作一些稀奇古怪的文件)

例如:通过inode查询文件并显示详细信息:

[root@iz2ze46xi6pjjj69ailg9lz test]# find -inum 264464 -exec ls -l {} \;
-rw-r--r-- 1 root root 0 Mar 28 13:46 ./xxx.txt

 

 

例如:通过inode为264464的文件并删除文件:

find -inum 264464 -exec rm {} \;

 

3. locate     文件查找命令:

  语法:   locate  [文件或目录关键字]

 

locate是从文件的数据库或者文件定期更新的数据库目录查找。所以有时候我们新建的文件用locate查找不到,因为数据库没有这个记录,需要结合下面这个命令使用。

 

updatedb  :update the slocate database   更新整个目录文件的数据库

 

更新完之后会对刚新建的文件也添加到数据库。 

例如:

 

[root@VM_0_12_centos ~]# touch xxx.txt   #建立文件
[root@VM_0_12_centos ~]# ls
anaconda-ks.cfg  xxx.txt
[root@VM_0_12_centos ~]# pwd
/root
[root@VM_0_12_centos ~]# locate xxx.txt
[root@VM_0_12_centos ~]# updatedb    #更新数据库目录
[root@VM_0_12_centos ~]# locate xxx.txt
/root/xxx.txt

 

 4.grep   在文件中搜寻字符串匹配的行并输出

语法:

  grep  [指定字符串]  [源文件]

例如:

[root@iz2ze46xi6pjjj69ailg9lz ~]# grep ftp /etc/services
ftp-data        20/tcp
ftp-data        20/udp
# 21 is registered to ftp, but also used by fsp
ftp             21/tcp
ftp             21/udp          fsp fspd
tftp            69/tcp
tftp            69/udp
sftp            115/tcp

 

 

如果需要显示关键字在文件中的行号可以加-n:

[root@iz2ze46xi6pjjj69ailg9lz ~]# grep ftp -n /etc/services
41:ftp-data        20/tcp
42:ftp-data        20/udp
43:# 21 is registered to ftp, but also used by fsp
44:ftp             21/tcp
45:ftp             21/udp          fsp fspd
75:tftp            69/tcp
76:tftp            69/udp

 

如果需要过滤掉以#开头的行

[root@iz2ze46xi6pjjj69ailg9lz ~]# grep -v '^#' /etc/services  | more

 

 

 

5.帮助命令  man

  manual

语法:

 man [命令或配置文件]

例如:查看命令帮助

man ls

 

 

查看配置文件的帮助信息:

man /etc/services

 

 

帮助分为很多种:默认查的是第一种命令帮助,如果需要查配置文件的帮助可以用 man 5 pwaswd

       1   Executable programs or shell commands
       2   System calls (functions provided by the kernel)
       3   Library calls (functions within program libraries)
       4   Special files (usually found in /dev)
       5   File formats and conventions eg /etc/passwd

 

 

6.info   获得帮助信息(与man的显示方式不同)

例如:

[root@iz2ze46xi6pjjj69ailg9lz ~]# info ls

 

7.whatis 提取命令最基本的用法,获取命令用法

[root@iz2ze46xi6pjjj69ailg9lz ~]# whatis ls
ls (1)               - list directory contents

 

8.命令 --help  列出命令的主要选项

[root@iz2ze46xi6pjjj69ailg9lz ~]# umask --help
-bash: umask: --: invalid option
umask: usage: umask [-p] [-S] [mode]

 

9.apropos 命令  查看包含字符串的命令的基本信息

[root@iz2ze46xi6pjjj69ailg9lz ~]# apropos fstab
fstab (5)            - static information about the filesystems
systemd-fstab-generator (8) - Unit generator for /etc/fstab

 

10.  help 查看shell内置命令的帮助

例如:当我们查询cd的帮助信息

man cd   列出的是shell内置的命令的名字,不会查出帮助信息,需要使用help来查看

NAME
       bash,  :,  .,  [, alias, bg, bind, break, builtin, caller, cd, command,
       compgen, complete, compopt,  continue,  declare,  dirs,  disown,  echo,
       enable,  eval,  exec, exit, export, false, fc, fg, getopts, hash, help,
       history, jobs, kill, let, local, logout, mapfile, popd, printf,  pushd,
       pwd,  read, readonly, return, set, shift, shopt, source, suspend, test,
       times, trap, true, type, typeset, ulimit, umask, unalias, unset, wait -
       bash built-in commands, see bash(1)

 

查看shell内置命令帮助信息:

[root@iz2ze46xi6pjjj69ailg9lz ~]# help cd
cd: cd [-L|[-P [-e]]] [dir]
    Change the shell working directory.

    Change the current directory to DIR.  The default DIR is the value of the
    HOME shell variable.

    The variable CDPATH defines the search path for the directory containing
    DIR.  Alternative directory names in CDPATH are separated by a colon (:).
    A null directory name is the same as the current directory.  If DIR begin
    with a slash (/), then CDPATH is not used.

 

11.file  filename   查看一个文件的类型

[root@iz2ze46xi6pjjj69ailg9lz jj]# file test.tar
test.tar: POSIX tar archive (GNU)
[root@iz2ze46xi6pjjj69ailg9lz jj]# file testxt
testxt: empty
[root@iz2ze46xi6pjjj69ailg9lz jj]# file test
test: directory

 

 

 

 

 ---------------------------------------打包压缩相关命令------------------------------------

4. 打包、压缩文件  

    windows : zip、rar   

    linux :gz ,bzip,zip     (zip是windows和linux通用的文件格式)

 

   1.gzip 、bzip2 压缩与解压  (压缩成.gz的文件或者解压.gz的文件)
  • gzip  只能压缩文件,不能压缩目录,且压缩不保留源文件: 压缩文件的时候名字后缀自动加.gz,大小变为原来的1/6

(1)测试只能压缩文件且不保留源文件:

[root@iz2ze46xi6pjjj69ailg9lz jj]# gzip test/   #打包文件夹失败
gzip: test/ is a directory -- ignored
[root@iz2ze46xi6pjjj69ailg9lz jj]# touch testxt
[root@iz2ze46xi6pjjj69ailg9lz jj]# ls
test  testxt
[root@iz2ze46xi6pjjj69ailg9lz jj]# gzip testxt  #压缩文件
[root@iz2ze46xi6pjjj69ailg9lz jj]# ls
test  testxt.gz

   

(2)解压文件 gzip -d xxx.gz  或者  gunzip xxx.gz

[root@iz2ze46xi6pjjj69ailg9lz jj]# gzip testxt
[root@iz2ze46xi6pjjj69ailg9lz jj]# ls
test  testxt.gz
[root@iz2ze46xi6pjjj69ailg9lz jj]# gzip -d testxt.gz   #解压文件
[root@iz2ze46xi6pjjj69ailg9lz jj]# ls
test  testxt
[root@iz2ze46xi6pjjj69ailg9lz jj]# gzip testxt
[root@iz2ze46xi6pjjj69ailg9lz jj]# gunzip testxt.gz    #解压文件
[root@iz2ze46xi6pjjj69ailg9lz jj]# ls
test  testxt
[root@iz2ze46xi6pjjj69ailg9lz jj]#

 

 

     2.tar :打包、拆包命令

        tar - cxzjvf  <打包后的文件>   <欲打包的目录>

           - c :创建的一个归档文件 ,即打包文件夹

           - x :拆包

           - z :以gzip 格式压缩,默认压缩倍数 6倍  (0-9) (打包的同时进行压缩,如果不写只是打包)

           - j :以bzip2格式压缩

           - v :显示打包或者拆包的文件信息(linux很多参数为-v就是显示详细信息)

           - f : 后面紧接一个 归档文件

 

例如:

  • 打包与解包  (只打包不压缩,也就是不加-z参数)

(1)打包文件(打包后的文件是二进制文件)

[root@iz2ze46xi6pjjj69ailg9lz jj]# tar -cvf test.tar ./test  #打包
./test/
[root@iz2ze46xi6pjjj69ailg9lz jj]# ll
total 16
drwxr-xr-x 2 root root  4096 Mar 28 15:21 test
-rw-r--r-- 1 root root 10240 Mar 28 15:32 test.tar

 

 

用file  文件名称  来查看一个文件的类型

[root@iz2ze46xi6pjjj69ailg9lz jj]# file test.tar
test.tar: POSIX tar archive (GNU)

 

 

(2)解包:

[root@iz2ze46xi6pjjj69ailg9lz jj]# ls
test.tar  testxt
[root@iz2ze46xi6pjjj69ailg9lz jj]# tar -xvf test.tar  #解包
./test/
[root@iz2ze46xi6pjjj69ailg9lz jj]# ls
test  test.tar  testxt

 

 

 
  • 打包的同时压缩以及拆包的同时解压缩  (加上-z参数)
(1)打包及压缩   
[root@iz2ze46xi6pjjj69ailg9lz jj]# ls
test  testxt
[root@iz2ze46xi6pjjj69ailg9lz jj]# tar -zcvf test.tar.gz test   #打包及压缩
test/
[root@iz2ze46xi6pjjj69ailg9lz jj]# ls
test  test.tar.gz  testxt
[root@iz2ze46xi6pjjj69ailg9lz jj]# file test.tar.gz   #查看文件类型
test.tar.gz: gzip compressed data, from Unix, last modified: Wed Mar 28 15:44:55
 2018

 

 

(2)拆包及解压缩

[root@iz2ze46xi6pjjj69ailg9lz jj]# ls
test.tar.gz  testxt
[root@iz2ze46xi6pjjj69ailg9lz jj]# tar -zxvf test.tar.gz    #拆包及解压缩
test/
[root@iz2ze46xi6pjjj69ailg9lz jj]# ls
test  test.tar.gz  testxt

 

 

3.zip,unzip压缩解压文件目录(重要)

  zip可以压缩目录,同时会保留文件,zip格式也是linux和windows都能识别的文件格式

  • 压缩文件与解压缩文件

(1)压缩文件  (源文件会保留)

[root@VM_0_12_centos test]# ls
test  test.txt
[root@VM_0_12_centos test]# zip test.txt.zip test.txt #压缩文件
  adding: test.txt (stored 0%)
[root@VM_0_12_centos test]# ls
test  test.txt  test.txt.zip
[root@VM_0_12_centos test]# file test.txt.zip     #查看文件类型
test.txt.zip: Zip archive data, at least v1.0 to extract

 

(2)解压缩:

[root@VM_0_12_centos test]# ls
test  test.txt.zip
[root@VM_0_12_centos test]# unzip test.txt.zip    #解压缩
Archive:  test.txt.zip
 extracting: test.txt
[root@VM_0_12_centos test]# ls
test  test.txt  test.txt.zip

 

 

  • 压缩目录与解压缩目录

(1)压缩目录

[root@VM_0_12_centos test]# zip test.zip ./test  #压缩文件夹
  adding: test/ (stored 0%)
[root@VM_0_12_centos test]# ls
test  test.txt  test.zip

 

(2)解压缩

[root@VM_0_12_centos test]# ls
test.txt  test.zip
[root@VM_0_12_centos test]# unzip ./test.zip  #解压缩
Archive:  ./test.zip
   creating: test/
[root@VM_0_12_centos test]# ll
total 8
drwxr-xr-x 2 root root 4096 Mar 28 15:53 test
-rw-r--r-- 1 root root    0 Mar 28 15:53 test.txt
-rw-r--r-- 1 root root  160 Mar 28 15:56 test.zip

 

 

4:bzip2 压缩文件命令(类似于gzip,加上-k会保留原文件) 
         bzip2 压缩(解压)文件,压缩文件后缀为bz2 ,类似于gzip,只能压缩文件,压缩目录需要将目录打包成.tar文件

(1)压缩文件

[root@VM_0_12_centos test]# ls
test  test.txt
[root@VM_0_12_centos test]# bzip2 test  #压缩目录失败
bzip2: Input file test is a directory.
[root@VM_0_12_centos test]# bzip2 test.txt   #压缩文件
[root@VM_0_12_centos test]# ls
test  test.txt.bz2
[root@VM_0_12_centos test]# file test.txt.bz2
test.txt.bz2: bzip2 compressed data, block size = 900k

 

 

加上-k保留源文件

[root@VM_0_12_centos test]# ls
test  test.txt
[root@VM_0_12_centos test]# bzip2 -k test.txt   #压缩并保留源文件
[root@VM_0_12_centos test]# ll
total 8
drwxr-xr-x 2 root root 4096 Mar 28 15:53 test
-rw-r--r-- 1 root root    0 Mar 28 15:53 test.txt
-rw-r--r-- 1 root root   14 Mar 28 15:53 test.txt.bz2

 

    

       

(2)解压缩

[root@VM_0_12_centos test]# ls
test  test.txt.bz2
[root@VM_0_12_centos test]# bzip2 -d test.txt.bz2   #解压缩
[root@VM_0_12_centos test]# ls
test  test.txt

 

----------------------------------------- 5.网络通信相关指令:---------------------------------

1.write   向当前登录linux的另一个用户发送消息,以Ctral+D为结束(通信用户必须在线)

  语法: write 用户名

例如:

root登录窗口:

[root@VM_0_12_centos test]# who    #查看当前在线用户
root     pts/0        2018-03-28 15:52 (218.26.179.150)
qlq      pts/1        2018-03-28 16:28 (218.26.179.150)
[root@VM_0_12_centos test]# whoami  #查看当前是谁登录
root
[root@VM_0_12_centos test]# write qlq  #向qlq用户发送消息
WHO

 

 

qlq窗口:

[qlq@VM_0_12_centos ~]$
Message from root@VM_0_12_centos on pts/0 at 16:29 ...
WHO

 

 

2.  wall    网络广播:向所有用户广播消息

语法:

  wall [message] [文件名]

 

例如:

root用户发送广播

[root@VM_0_12_centos ~]# wall this is test
[root@VM_0_12_centos ~]#
Broadcast message from root@VM_0_12_centos (pts/1) (Wed Mar 28 16:34:12 2018):

this is test

 

 

qlq查看广播:

[qlq@VM_0_12_centos ~]$
Broadcast message from root@VM_0_12_centos (pts/1) (Wed Mar 28 16:34:12 2018):

this is test

 

 

3.ping  测试网络连通性

[root@VM_0_12_centos ~]# ping www.qiaoliqiang.cn  #查看网络连通性
PING www.qiaoliqiang.cn (47.94.208.124) 56(84) bytes of data.
64 bytes from 47.94.208.124: icmp_seq=1 ttl=48 time=51.3 ms
64 bytes from 47.94.208.124: icmp_seq=2 ttl=48 time=50.7 ms
64 bytes from 47.94.208.124: icmp_seq=3 ttl=48 time=50.7 ms
64 bytes from 47.94.208.124: icmp_seq=4 ttl=48 time=50.6 ms
64 bytes from 47.94.208.124: icmp_seq=5 ttl=48 time=50.7 ms
64 bytes from 47.94.208.124: icmp_seq=6 ttl=48 time=50.6 ms
64 bytes from 47.94.208.124: icmp_seq=7 ttl=48 time=50.6 ms

--- www.qiaoliqiang.cn ping statistics ---
7 packets transmitted, 7 received, 0% packet loss, time 6008ms
rtt min/avg/max/mdev = 50.640/50.782/51.318/0.327 ms

 

  分析丢包率,有的时候会发现连接不上或者数据传输断断续续。

 

-c  次数,可以指定ping的次数。

-s  字节数  指定ping发送的数据包大小,不能超过60004好像是

 

例如:

[root@VM_0_12_centos ~]# ping -c 4 -s 60000 tx.qiaoliqiang.cn
PING tx.qiaoliqiang.cn (118.126.115.126) 60000(60028) bytes of data.
60008 bytes from 118.126.115.126: icmp_seq=1 ttl=63 time=1.45 ms
60008 bytes from 118.126.115.126: icmp_seq=2 ttl=63 time=1.40 ms
60008 bytes from 118.126.115.126: icmp_seq=3 ttl=63 time=1.34 ms
60008 bytes from 118.126.115.126: icmp_seq=4 ttl=63 time=1.20 ms

--- tx.qiaoliqiang.cn ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 12018ms
rtt min/avg/max/mdev = 1.200/1.349/1.451/0.098 ms

 

 

4.ifconfig   查看网络配置信息(可以加-a参数)

语法:

  ifconfig [选项][-a] [网卡设备标识]

 

[root@VM_0_12_centos ~]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.27.0.12  netmask 255.255.240.0  broadcast 172.27.15.255
        ether 52:54:00:da:e2:37  txqueuelen 1000  (Ethernet)
        RX packets 4815859  bytes 483225664 (460.8 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 4815011  bytes 644353238 (614.5 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        loop  txqueuelen 1  (Local Loopback)
        RX packets 1213  bytes 168885 (164.9 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 1213  bytes 168885 (164.9 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

 

  eth0是实际网卡,lo是回环网卡。

 

例如只查看实际网卡的信息:

  可以查看ipv4地址,子网掩码,与广播地址

[root@VM_0_12_centos ~]# ifconfig eth0
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.27.0.12  netmask 255.255.240.0  broadcast 172.27.15.255
        ether 52:54:00:da:e2:37  txqueuelen 1000  (Ethernet)
        RX packets 4819055  bytes 483516557 (461.1 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 4818178  bytes 644770324 (614.9 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

 

 

修改自己的ip地址:

 ifconfig eth0 192.168.15.122 netmask 255.255.255.0

 

另一种方式就是setup打开伪图形界面进行修改。 

 

 

 

 

5.traceroute  hostname   查看到一个主机的路由(类似于windows的tracert)

例如:

[root@VM_0_12_centos ~]# traceroute qiaoliqiang.cn  #查看到qiaoliqiang.cn的路由
traceroute to qiaoliqiang.cn (47.94.208.124), 30 hops max, 60 byte packets
 1  100.99.91.130 (100.99.91.130)  4.844 ms  5.627 ms  6.453 ms
 2  * 100.99.115.230 (100.99.115.230)  0.801 ms *
 3  10.200.159.157 (10.200.159.157)  0.984 ms  0.627 ms  0.921 ms
 4  10.196.0.30 (10.196.0.30)  2.273 ms 10.196.0.34 (10.196.0.34)  2.363 ms  2.5
54 ms
 5  10.196.0.78 (10.196.0.78)  2.238 ms  2.319 ms 10.196.0.86 (10.196.0.86)  1.8
96 ms
 6  182.150.16.101 (182.150.16.101)  11.021 ms  10.771 ms 182.150.16.105 (182.15
0.16.105)  10.060 ms
 7  182.140.229.205 (182.140.229.205)  2.963 ms 182.140.229.177 (182.140.229.177
)  2.684 ms 125.64.29.201 (125.64.29.201)  2.988 ms
 8  182.140.220.229 (182.140.220.229)  10.651 ms 182.140.220.9 (182.140.220.9)
8.763 ms 182.140.220.229 (182.140.220.229)  10.565 ms
 9  * 202.97.36.137 (202.97.36.137)  45.664 ms 202.97.36.45 (202.97.36.45)  60.0
38 ms
10  36.110.244.26 (36.110.244.26)  57.669 ms  57.562 ms *
11  * * *
12  * 180.149.141.146 (180.149.141.146)  52.405 ms *
13  * * *
14  101.200.109.129 (101.200.109.129)  49.570 ms * 123.56.34.22 (123.56.34.22)

 

  总结:我们可以通过路由IP查看所经过的地址,利用IP查看IP所在的地址。

  

 

6.  curl ifconfig.me   查看自己的公网IP

[root@VM_0_12_centos ~]# curl ifconfig.me
118.126.115.126

 

7.实际上我们在编辑的时候是编辑/etc/sysconfig/network-scripts/ifcfg-eth0 文件

DEVICE='eth0'
NM_CONTROLLED='yes'
ONBOOT='yes'
BOOTPROTO='dhcp'
PERSISTENT_DHCLIENT=yes
RES_OPTIONS='rotate timeout:1'

 

 8.linux配置虚拟域名:

/etc/hosts 

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

 

 

 

 

 

 -----------------------------------linux 系统命令(重要)---------------------------------

 

    0.date查看日期与指定格式显示日期:

NAME
       date - print or set the system date and time

SYNOPSIS
       date [OPTION]... [+FORMAT]
       date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]

 

      FORMAT controls the output.  Interpreted sequences are:

      %%     a literal %

      %a     locale's abbreviated weekday name (e.g., Sun)

      %A     locale's full weekday name (e.g., Sunday)

      %b     locale's abbreviated month name (e.g., Jan)

      %B     locale's full month name (e.g., January)

      %c     locale's date and time (e.g., Thu Mar  3 23:05:25 2005)

      %C     century; like %Y, except omit last two digits (e.g., 20)

      %d     day of month (e.g., 01)

      %D     date; same as %m/%d/%y

      %e     day of month, space padded; same as %_d

      %F     full date; same as %Y-%m-%d

      %g     last two digits of year of ISO week number (see %G)

      %G     year of ISO week number (see %V); normally useful only with %V

      %h     same as %b

      %H     hour (00..23)

      %I     hour (01..12)

      %j     day of year (001..366)

      %k     hour, space padded ( 0..23); same as %_H

      %l     hour, space padded ( 1..12); same as %_I

      %m     month (01..12)

      %M     minute (00..59)

      %n     a newline

      %N     nanoseconds (000000000..999999999)

      %p     locale's equivalent of either AM or PM; blank if not known

      %P     like %p, but lower case

      %r     locale's 12-hour clock time (e.g., 11:11:04 PM)

      %R     24-hour hour and minute; same as %H:%M

      %s     seconds since 1970-01-01 00:00:00 UTC

      %S     second (00..60)

      %t     a tab

      %T     time; same as %H:%M:%S

      %u     day of week (1..7); 1 is Monday

      %U     week number of year, with Sunday as first day of week (00..53)

      %V     ISO week number, with Monday as first day of week (01..53)

      %w     day of week (0..6); 0 is Sunday

      %W     week number of year, with Monday as first day of week (00..53)

      %x     locale's date representation (e.g., 12/31/99)

      %X     locale's time representation (e.g., 23:13:48)

      %y     last two digits of year (00..99)

      %Y     year

      %z     +hhmm numeric time zone (e.g., -0400)

      %:z    +hh:mm numeric time zone (e.g., -04:00)

      %::z   +hh:mm:ss numeric time zone (e.g., -04:00:00)

 

例如:

[root@iz2ze46xi6pjjj69ailg9lz ~]# date +%F  #指定日期显示格式
2018-03-31
[root@iz2ze46xi6pjjj69ailg9lz ~]# date +%M
16
[root@iz2ze46xi6pjjj69ailg9lz ~]# date +%D
03/31/18

 

 

 

     1.stat   :查看文件的详细信息

         stat bj.txt

     2.who与whoami

  •          who : 查看在线的用户
[root@VM_0_12_centos test]# who
root     pts/0        2018-03-28 15:52 (218.26.179.150)
qlq      pts/1        2018-03-28 16:09 (218.26.179.150)

 

  •          whoami :查看当前自己的用户
[root@VM_0_12_centos test]# whoami
root

 

     3.hostname : 显示主机名

         hostname

    hostname test  将主机名修改为test,重开一个窗口登录会变为test

     4.uname  :显示系统信息

          -a :显示完整的系统信息 (如果带x86_64代表64位系统,如果不是就是32位系统)

       

     5.top :显示当前耗时的进程的信息 ,每3秒刷新一次

           cltr+c 中断

     6.ps :显示当前进程的快照

    ps -ef   查看所有进程。

    ps -ef | grep xxx 查看进程并过滤条件

  • -A 列出所有的行程
  • -w 显示加宽可以显示较多的资讯
  • -au 显示较详细的资讯
  • -aux 显示所有包含其他使用者的行程
  • au(x) 输出格式 :

例如:查看redis的进程信息

ps -ef |grep redis

  

 3639为进程ID

 

     7.du :显示文件的大小信息

[root@iz2ze46xi6pjjj69ailg9lz ~]# du Exam.war
208608  Exam.war

 

     8.df :磁盘使用情况  disk free

     9.ifconfig :查看或者配置网卡信息 ,好比windows 的ipconfig

        ipv4:32位  2-32次方     ipv6  128位     是 ipv4  2-96次方倍数

         设置虚拟机ip地址:

           1. 设置vmware的连接方式

              - 共享宿主机的ip地址,在网上邻居找不到

              - 桥接方式,需要单独设置ip,可以在网上邻居查找

           2. 图形界面设置ip地址

               edit connection --》ipv4--》manual(手工设置)--》add  (ip地址,子网掩码)

          3.命令方式设置ip地址

              静态设置ip:

              sudo ifconfig eth0 192.168.15.122 netmask 255.255.255.0

11.clear  : 清除屏幕  windows: cls     也可以用Ctrl+l快捷键,Ctrl+u清除光标前面输入的字符

12.history    查看执行过的命令记录

  !命令行号  可以执行对应行号的命令

13.kill  :杀死进程

        kill  pid

 

例如杀死PID为3639的进程:(有时候需要root权限)

kill -9 3639

  

14.netstat  :网络连接详细信息(带LISTENING的是standalone进程,一直在监听端口)

        

C:\Users\WWW>netstat -ano | grep 8005
  TCP    127.0.0.1:8005         0.0.0.0:0              LISTENING       2190932

C:\Users\WWW>jps
2148724
2190932 Bootstrap

 

  15. useradd  添加用户

        - 查看用户信息

            sudo cat /etc/passwd

          itcast:x:1000:1000:UbuntuA,,,:/home/itcast:/bin/bash

           - itcast:用户名

           - x :密码 :已经加密 ,密码存放在  /etc/shadow

           - 1000: 账号id ,userId

           - 1000: 组id ,group id

          - UbuntuA,,,  :账号描述

          - /home/itcast :该账号存放文件的默认位置  ~

           - /bin/bash:该用户的shell脚本的解析方式  ,sh 、bash、rbash

 

     - 创建用户并设置密码

           sudo useradd lijun -d  /home/lijun   -s /bin/bash

              -d :指定该用户的home 路径

              - s :该用户的shell解析方式

      -d  -s 也可以不指定,linux会默认在/home下面创建对应的用户名目录作为用户的根目录

例如:

[root@VM_0_12_centos test]# useradd qlq   #创建用户qlq
[root@VM_0_12_centos test]# passwd qlq  #修改qlq用户的密码
Changing password for user qlq.
New password:
BAD PASSWORD: The password is a palindrome
Retype new password:
passwd: all authentication tokens updated successfully.

 

       

也可以手动创建目录并执行该用户的默认工作空间:

    步骤:

             1. 创建 /home/lijun 目录

             2. 执行 useradd 命令(指明目录为上面新建的目录)

             3. 用 passwd 设置密码

             4. su 切换用户

 

具体的用户和用户组管理参考:    http://www.runoob.com/linux/linux-user-manage.html

 

16.别名以及定义别名:

  • 定义别名alias 别名名称=命令名称  (命名名称如果加选项需要用引号包起来)

  alias copy=cp

  alias xrm="rm -r"

例如:

[root@VM_0_12_centos ~]# alias drm="rm -r"
[root@VM_0_12_centos ~]# ls
anaconda-ks.cfg  test
[root@VM_0_12_centos ~]# drm test/
rm: descend into directory ‘test/’? y

 

 

  • 查询别名:
[root@VM_0_12_centos ~]# alias
alias cp='cp -i'
alias drm='rm -r'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'

 

 

  • 删除别名
[root@VM_0_12_centos ~]# unalias drm

 

 

17.输入输出重定向:

同标准IO一样,shell对于每个进程预先定义3个文件描述(0,1,2).分别对应于

0: (STDIN)  标准输入

1.(STDOUT)    标准输出

2:(STDERR) 标准错误输出

 

  • >或>>  输出重定向:

        - > :覆盖模式

         命令:  echo "ww">aaa

[root@VM_0_12_centos ~]# ls ./ > ./test.txt
[root@VM_0_12_centos ~]# cat ./test.txt
anaconda-ks.cfg
test.txt

 

 

         - >>:追加模式

[root@VM_0_12_centos ~]# ls ./ >> ./test.txt
[root@VM_0_12_centos ~]# cat ./test.txt
anaconda-ks.cfg
test.txt
anaconda-ks.cfg
test.txt

 

有时候我们不希望一些提示信息输出在屏幕,可以将结果重定向到一个文件,如果我们不希望看到,同时又不想保存到文件,可以重定向到/dev/null,/dev/null是unix,linux的无底洞。

[root@VM_0_12_centos ~]# echo "test" > /dev/null
[root@VM_0_12_centos ~]# ll /dev/null
crw-rw-rw- 1 root root 1, 3 Mar 20 09:18 /dev/null
[root@VM_0_12_centos ~]# cat /dev/null
[root@VM_0_12_centos ~]#

 

 

 

  • <    输入重定向,信息不是从键盘读取,是从资源文件读取

例如:广播一个文件中的信息

[root@VM_0_12_centos ~]# wall < ./test.txt
[root@VM_0_12_centos ~]#
Broadcast message from root@VM_0_12_centos (Wed Mar 28 17:18:39 2018):

anaconda-ks.cfg
test.txt
anaconda-ks.cfg
test.txt

 

 

  • 错误信息重定向到日志中(重要)
[root@VM_0_12_centos ~]# XX 2> ./cw.err
[root@VM_0_12_centos ~]# cat ./cw.err
bash: XX: command not found

  这个错误重定向可以用于当我们在夜间复制文件报错的时候将错误信息重定向到一个错误日志文件中,当正确执行的时候不会执行错误重定向

 

18.管道: 将一个命令的输出作为另一个命令的输入  |

  使用方法:

    命令 | 命令2 | 命令3...

例如:

[root@VM_0_12_centos ~]# ls | grep cw | more
cw.err

 

  解释:相当远ls查询出来的结果1放到第二个grep的cw后面作为输入  grep cw 结果1

     将上面的grep cw 结果 出来的结果2放到more 后面作为输入  more 结果2

 

19.  ;  &&  ||操作符

;  让命令顺序执行

&&   逻辑与

||   逻辑或

 

  • ;  多个命令顺序执行:
[root@VM_0_12_centos ~]# ls;pwd;cd /;pwd
anaconda-ks.cfg  cw.err  xxx
/root
/
[root@VM_0_12_centos /]#

 

结合错误重定向出错后重定向到日志,看是哪一步出错了:(让命令顺序执行,出错之后将错误记录下来)

[root@VM_0_12_centos ~]# ls;pwd;d / 2> ./bb.err;pwd
anaconda-ks.cfg  xxx
/root
/root
[root@VM_0_12_centos ~]# cat ./bb.err
-bash: d: command not found

 

  • &&    逻辑与  (前面的命令执行成功后面的命令才会执行)

comm1  &&  comm2

 成功              执行

 失败            不执行

 

第一个命令执行成功,第二个命令执行:

mkdir xx && rm -rf xx

 

第一个命令失败,第二个命令也不会执行:

[root@VM_0_12_centos ~]# ls
anaconda-ks.cfg
[root@VM_0_12_centos ~]# rE -rf xxx && mkdir xxx
-bash: rE: command not found

 

多个命令的时候前面成功后面才会执行:

[root@VM_0_12_centos ~]# ls && pwd && ls  && pwd
anaconda-ks.cfg
/root
anaconda-ks.cfg
/root

 

前面执行失败之后后面的不会执行;

[root@VM_0_12_centos ~]# ls && pwd && lX  && pwd
anaconda-ks.cfg
/root
-bash: lX: command not found

 

  • ||  逻辑或  (前面的执行成功不会执行后面的,否则会执行后面的,多个命令中执行一个)

例子:

  第一个成功不会执行后面的

[root@VM_0_12_centos ~]# ls || pwd || cd /
anaconda-ks.cfg

 

  第一个失败执行第二个:

[root@VM_0_12_centos ~]# lX || pwd || cd /
-bash: lX: command not found
/root

 

20.命令替换符:   `命令`   (注意符号是键盘上1左边的符号,不是单引号)

    将一个命令的输出作为另一个命令的参数

 语法:  命令1 `命令2`

例如:

[root@VM_0_12_centos ~]# which touch
/usr/bin/touch
[root@VM_0_12_centos ~]# ls /usr/bin/touch
/usr/bin/touch
[root@VM_0_12_centos ~]# ls -l `which touch`  #命令替换符
-rwxr-xr-x. 1 root root 62432 Nov 20  2015 /usr/bin/touch

 21.与`等价的命令替换符为$(command)

[root@iz2ze46xi6pjjj69ailg9lz ~]# ll $(which touch)
-rwsr-xr-x. 1 root root 62488 Nov  6  2016 /usr/bin/touch

 

22.seq命令用于产生从某个数到另外一个数之间的所有整数。

 语法:

seq [选项]... 尾数
seq [选项]... 首数 尾数
seq [选项]... 首数 增量 尾数

 

选项

-f, --format=格式        使用printf 样式的浮点格式
-s, --separator=字符串   使用指定字符串分隔数字(默认使用:\n)
-w, --equal-width        在列前添加0 使得宽度相同

-f选项:指定格式

#seq -f"%3g" 9 11
9
10
11

 

%后面指定数字的位数 默认是%g%3g那么数字位数不足部分是空格。

#sed -f"%03g" 9 11
#seq -f"str%03g" 9 11
str009
str010
str011 

    这样的话数字位数不足部分是0,%前面制定字符串。

-w选项:指定输出数字同宽

seq -w 98 101
098
099
100
101

    不能和-f一起用,输出是同宽的。

-s选项:指定分隔符(默认是回车)

seq -s" " -f"str%03g" 9 11
str009 str010 str011

 

要指定/t做为分隔符号:

seq -s"`echo -e "/t"`" 9 11

 

指定\n作为分隔符号:

seq -s"`echo -e "\n"`" 9 11
19293949596979899910911

 23  {}的用法:

  ①大括号拓展。(通配(globbing))将对大括号中的文件名做扩展。在大括号中,不允许有空白,除非这个空白被引用或转义。第一种:对大括号中的以逗号分割的文件列表进行拓展。如 touch {a,b}.txt 结果为a.txt b.txt。第二种:对大括号中以点点(..)分割的顺序文件列表起拓展作用,如:touch {a..d}.txt 结果为a.txt b.txt c.txt d.txt

[root@iz2ze46xi6pjjj69ailg9lz ttt]# touch test{0..5}.txt
[root@iz2ze46xi6pjjj69ailg9lz ttt]# ls
test0.txt  test1.txt  test2.txt  test3.txt  test4.txt  test5.txt
[root@iz2ze46xi6pjjj69ailg9lz ttt]# echo str{b..d}
strb strc strd
[root@iz2ze46xi6pjjj69ailg9lz ttt]# echo str{b,d}
strb strd
 

 

   ②代码块,又被称为内部组,这个结构事实上创建了一个匿名函数 。与小括号中的命令不同,大括号内的命令不会新开一个子shell运行,即脚本余下部分仍可使用括号内变量。括号内的命令间用分号隔开,最后一个也必须有分号。{}的第一个命令和左括号之间必须要有一个空格。{}也可以用于多行注释,作为函数包起来只是不调用即可。 

24.    awk 域分割符 '命令'  (单引号)

  不写的话默认以空格分割。

例1:检测系统中UID为0的用户

原来在/etc/passwd中的信息:

[root@VM_0_12_centos sshDemo]# grep root /etc/passwd
root:x:0:0:root:/root:/bin/bash

 

awk检测:

[root@VM_0_12_centos sshDemo]# awk -F: '$3==0 {print $1}' /etc/passwd
root

 

解释:  以:分割,$3表示取第三个位0的,然后打印第一位,针对/etc/passwd文件进行操作

 

例2:检测系统中密码为空的用户

1.系统中源文件格式

[root@VM_0_12_centos sshDemo]# tail -2 /etc/shadow
qlq11:$1$OaDtFguj$RDXrwYOWXC.KZsz1lukic/:17621:0:99999:7:::
apache:!!:17621::::::

 

  发现aoache的账户是锁着的,没有密码

2.检测:

[root@VM_0_12_centos sshDemo]# awk -F: 'length($2)==0 {print $1}' /etc/shadow

 

检测: 以:分割,如果第二位的长度为0就满足条件,满足条件的打印第一位,针对/etc/shadow文件

 

3解锁apache账户再次检测:.

[root@VM_0_12_centos sshDemo]# passwd -uf apache
Unlocking password for user apache.
passwd: Success
[root@VM_0_12_centos sshDemo]# awk -F: 'length($2)==0 {print $1}' /etc/shadow
apache

 

 例3:检测一个用户的所有的进程的ID

ps-aux    原数据格式:

[root@VM_0_12_centos sshDemo]# ps -aux|more
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.1 123036  3476 ?        Ss   Mar20   0:42 /usr/lib/syste
d/systemd --switched-root --system --deserialize 21
root         2  0.0  0.0      0     0 ?        S    Mar20   0:00 [kthreadd]
root         3  0.0  0.0      0     0 ?        S    Mar20   0:10 [ksoftirqd/0]

 

 

检测:

与qlq相关的信息:

[root@VM_0_12_centos sshDemo]# ps -aux |grep qlq
root     12920  0.0  0.2 148232  5316 ?        Ss   12:45   0:00 sshd: qlq [priv]
qlq      12924  0.0  0.1 148232  2240 ?        S    12:45   0:00 sshd: qlq@pts/0
qlq      12925  0.0  0.1 116036  2712 pts/0    Ss+  12:45   0:00 -bash
root     13615  0.0  0.0 112644   964 pts/2    R+   12:57   0:00 grep --color=auto qlq

 

awk提取信息:

[root@VM_0_12_centos sshDemo]# ps -aux |grep qlq |awk '$1=="qlq" {print $2}'
12924
12925

 

   从前面提取与qlq相关的信息,用awk分割,默认以空格分割,分割之后取第一位是qlq的证明是qlq用户的,并且打印第二位,第二位是PID。  

如果想让qlq退出登录直接杀死与qlq相关的进程: 

[root@VM_0_12_centos sshDemo]# kill $(ps -aux |grep qlq |awk '$1=="qlq" {print
$2}')
[root@VM_0_12_centos sshDemo]# who
root     pts/2        2018-04-01 11:21 (218.26.55.132)

 

  $(cmd)与命令替换符`cmd`的作用相对等。

 

例4:awk传递一个变量

[root@VM_0_12_centos ~]# echo ${word}
roo
[root@VM_0_12_centos ~]# /usr/bin/grep ${word} /etc/passwd | awk -F: -v word1=${word} 'index($1,word1)>0 {print $1}'
root

 

 

借助-v选项,可以将外部值(并非来自stdin)传递给awk:

VAR=10000
echo | awk -v VARIABLE=$VAR '{ print VARIABLE }'

 

 

 

更详细的awk用法参考:   http://man.linuxde.net/awk

 

其他常用的:

1.scp文件复制:

本地桌面复制到远程:   

  •  windows下的盘符可以/c/...  不用加冒号
liqiang@root MINGW64 /
$ cd /c/Users/liqiang/Desktop/

liqiang@root MINGW64 ~/Desktop
$ scp ./服务器.rar root@qiaoliqiang.cn:/home
服务器.rar                                 100%  145MB 956.3KB/s   02:34

liqiang@root MINGW64 ~/Desktop
$

 

 

  • 将本地C:\Users\liqiang\Desktop\redis\redis\bin目录下面的所有文件上传到服务器/usr/local/redis/bin/目录下:
$ scp /c/Users/liqiang/Desktop/redis/redis/bin/* root@qiaoliqiang.cn:/usr/local/redis/bin/
redis-benchmark                                                                                                                                               100% 1874KB   1.8MB/s   00:01
redis-check-aof                                                                                                                                               100% 4539KB 905.9KB/s   00:05
redis-check-rdb                                                                                                                                               100% 4539KB 755.1KB/s   00:06
redis-cli                                                                                                                                                     100% 2021KB   1.0MB/s   00:02
redis-server                                                                                                                                                  100% 4539KB   1.1MB/s   00:04

 

 

 

 

 

 

远程复制到本地桌面:  windows下的目录从盘开始,以斜杠分割,不用加冒号。

C:\Program Files\Git>scp root@qiaoliqiang.cn:/home/test.html /c/Users/liqiang/De
sktop/
test.html                                     100%   68     0.9KB/s   00:00

C:\Program Files\Git>

 

 

 

 

 

 

补充:

  

1、我的系统中安装了那些rpm软件包。

rpm -qa 讲列出所有安装过的包

如果要查找所有安装过的包含某个字符串sql的软件包

rpm -qa | grep sql

2、如何获得某个软件包的文件全名。

rpm -q mysql

可以获得系统中安装的mysql软件包全名,从中可以获得当前软件包的版本等信息。这个例子中可以得到信息mysql-3.23.54a-11

3、一个rpm包中的文件安装到那里去了?

rpm -ql 包名

注意这里的是不包括.rpm后缀的软件包的名称,也就是说只能用mysql或者mysql-3.23.54a-11而不是mysql-3.23.54a-11.rpm。如果只是想知道可执行程序放到那里去了,也可以用which,比如:

which mysql

 4.查看某个命令的安装路径 

  which命令用于查找并显示给定命令的绝对路径,环境变量PATH中保存了查找命令时需要遍历的目录。which指令会在环境变量$PATH设置的目录里查找符合条件的文件。也就是说,使用which命令,就可以看到某个系统命令是否存在,以及执行的到底是哪一个位置的命令。

 

5.source    

  source命令通常用于重新执行刚修改的初始化文件,使之立即生效,而不必注销并重新登录。因为linux所有的操作都会变成文件的格式存在。

6.export命令

  export命令用于将shell变量输出为环境变量,或者将shell函数输出为环境变量

[root@VM_0_12_centos ~]# export ww=ww  #将变量添加到环境变量
[root@VM_0_12_centos ~]# ee=ee      #定义普通的变量(不会加到环境变量)
[root@VM_0_12_centos ~]# env | grep ww  
ww=ww
[root@VM_0_12_centos ~]# env | grep rr
[root@VM_0_12_centos ~]# unset ww    #从环境变量删除
[root@VM_0_12_centos ~]# env | grep ww

 7.declare

   declare命令用于声明和显示已存在的shell变量。当不提供变量名参数时显示所有shell变量。declare命令若不带任何参数选项,则会显示所有shell变量及其值。

[root@VM_0_12_centos ~]# declare ppp=ppp  #声明变量
[root@VM_0_12_centos ~]# lll=lll      #声明变量
[root@VM_0_12_centos ~]# echo $ppp
ppp
[root@VM_0_12_centos ~]# echo $lll
lll
[root@VM_0_12_centos ~]#

 

 

 

 

4查看系统信息的命令

cat /proc/cpuinfo    查看cpu信息

cat /proc/meminfo    查看内存信息

cat /etc/redhat-release 查看系统版本(CentOS Linux release 7.3.1611 (Core))

uptime # 查看系统运行时间、用户数、负载

df  -h                   查看硬盘分区信息(-h 表示human,转换成人类可以识别的单位)

[root@VM_0_12_centos ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/vda1        50G  9.4G   38G  21% /
devtmpfs        911M     0  911M   0% /dev
tmpfs           920M   24K  920M   1% /dev/shm
tmpfs           920M  368K  920M   1% /run
tmpfs           920M     0  920M   0% /sys/fs/cgroup
tmpfs           184M     0  184M   0% /run/user/0

  

du -h  查看目录的大小

[root@VM_0_12_centos ~]# du -h ./sshDemo/
80K     ./sshDemo/

 

 

 

 

 

 curl ifconfig.me   好看本机的公网ip地址

runlevel   查看当前运行级别

init [01234567Sc] 改变运行级别为对应级别  (不用关机切换运行级别)

[root@iz2ze46xi6pjjj69ailg9lz ~]# systemctl get-default  #查看系统的缺省运行级别
multi-user.target

 setup   伪图形界面(配置一些信息,可以配置IP地址等信息)

  getconf LONG_BIT  查看linux-系统位数(32或者64)

 

 

5.域名解析:     nslookup命令是常用域名查询工具,就是查DNS信息用的命令。

例如:

[root@VM_0_12_centos ~]# nslookup tx.qiaoliqiang.cn
Server:         183.60.83.19
Address:        183.60.83.19#53

Non-authoritative answer:
Name:   tx.qiaoliqiang.cn
Address: 118.126.115.126

 

 

[root@VM_0_12_centos ~]# nslookup -q=mx tx.qiaoliqiang.cn
Server:         183.60.83.19
Address:        183.60.83.19#53

Non-authoritative answer:
*** Can't find tx.qiaoliqiang.cn: No answer

Authoritative answers can be found from:
qiaoliqiang.cn
        origin = dns31.hichina.com
        mail addr = hostmaster.hichina.com
        serial = 2017090208
        refresh = 3600
        retry = 1200
        expire = 86400
        minimum = 360

 

6.设置linux的域名服务器DNS

[root@iz2ze46xi6pjjj69ailg9lz yum.repos.d]# cat /etc/resolv.conf
nameserver 114.114.114.114
nameserver 8.8.8.8

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值