zoukankan      html  css  js  c++  java
  • 玩转Linux系统之常用命令

    下面是Linux系统一些常见的操作命令的使用情况介绍。

    1.文件相关命令

    1) 文件创建

    ① 创建单个文件

    touch test.txt
    

    ② 创建多个文件

    touch test1.txt test2.txt
    
    touch {test1.txt,test2.txt}
    

    2) 文件删除

    ① 删除文件时,系统会询问是否删除

    rm test.txt
    

    ② 强制删除文件

    rm -rf test.txt
    

    3) 文件移动或重命名

    ① 文件移动

    mv test.txt /tmp/
    

    ② 文件重命名

    mv test.txt a.txt
    

    ③ 文件移动并重命名

    mv test.txt /tmp/a.txt
    

    4) 文件拷贝、追加或重命名

    ① 文件拷贝

    cp test.txt /tmp/
    

    ② 文件拷贝并重命名

    cp test.txt /tmp/a.txt
    

    ③ 一个文件内容拷贝到里另一个文件中

    cat test1.txt > test2.txt
    

    ④ 一个文件内容追加到里另一个文件中

    cat test1.txt >> test2.txt
    

    5) vim文件操作

    ① vim工作模式

    ② 插入命令

    ③ 定位命令

    ④ 删除命令

    ⑤ 替换和取消命令

    6) 文件内容查看cat、more、less、head、tail、grep、cut、sort

    ① 正序查看

    cat test.txt
    

    ② 倒序查看

    tac test.txt
    

    ③ 从前向后以页读取文件(上翻:[b],下翻:[空格键])

    more test.txt
    

    ④ 从第5行向后以页读取文件(上翻:[b],下翻:[空格键])

    more +5 test.txt
    

    ⑤ 查找第一个出现"test"字符串的行,并从该处前两行开始显示输出

    more +/test test.txt
    

    ⑥ 从前向后以页读取文件(上翻:[pageup],下翻:[pagedown])

    less test.txt
    

    ⑦ 显示文件的前n行(默认10行,不带n)

    head -n test.txt
    

    ⑧ 显示文件的后n行(默认10行,不带n)

    tail -n test.txt
    

    ⑨ 跟踪显示文件新追加的内容

    tail -f test.txt
    

    ⑩ 文本过滤,模糊查找含字母a的行

    grep a test.txt
    

    ⑪ 显示test.txt文件第1,3,5行(-d:指定分隔符;-f:指定文件)

    cut -d : -f 1,3,5 test.txt
    

    ⑫ 文件内容排序(-k:指定字段;3:第三列;-t:指定分割符;-n:以数字大小进行排序,-u:去重)

    sort -k 3 -t : -n -u test.txt
    

    7) 给文件设置拥有者,权限

    ① 将test.txt的用户拥有者设为zwh1,组的拥有者设为zwh2

    chown zwh1:zwh2 test.txt
    

    ② 将当前目录下的所有文件与子目录的用户拥有者为zwh1,组拥有者设为zwh2

    chown -R zwh1:zwh2 *
    

    ③ 将当前目录下的所有文件与子目录皆设为任何人可读取

    chmod -R a+r *
    

    ④ 将test1.txt 与test2.txt设为其拥有者和其所属同一个组者可写入,但其他以外的人则不可写入

    chmod ug+w,o-w test1.txt test2.txt
    

    8)其他他文件操作

    ① 查看文件详情

    stat test.txt
    

    ② 查看文件大小

    du -h test.txt
    [root@hdp1 /]# du -h zwh/
    4.0K    zwh/
    

    ③ 查看文件夹及子目录文件大小

    du -ah zwh/
    [root@hdp1 /]# du -ah zwh/
    0       zwh/test1.txt
    0       zwh/test2.txt
    4.0K    zwh/test.txt
    4.0K    zwh/
    

    ④ 回到原来路径

    cd -
    

    ⑤ 回到上级路径

    cd ..
    

    2.系统命令

    1)主机名操作

    ① 查看主机名

    hostname
    

    ② 修改主机名(重启后永久生效),将名字修改即可

    vim /etc/hostname
    

    2)修改IP

    vim /etc/sysconfig/network-scripts/ifcfg-eth0
    

    3)查看系统信息

    ① 显示全部信息

    uname -a
    

    ② 显示操作系统的发行编号

    uname -r
    

    ③ 显示操作系统名称

    uname -s
    

    4) 查看文件信息

    file test.txt
    

    5) 查看各分区使用情况

    df -h
    

    6) 查看当前用户的计划任务服务

    crontab -l
    

    7) 查看系统所有用户

    cut -d: -f1 /etc/passwd
    

    8) 查看系统所有组

    cut -d: -f1 /etc/group
    

    9) 实时显示进程状态用户

    top
    

    10) 查看所有进程

    cut -d: -f1 /etc/group
    

    11) 查看网络统计信息进程

    netstat -s
    

    12) 查看所有监听端口

    netstat -lntp
    

    13) 查看内存总量

    grep MemTotal /proc/meminfo
    

    14) 查看空闲内存量

    grep MemFree /proc/meminfo
    

    15) 查看系统运行时间、用户数、负载

    uptime
    

    16) 查看用户最近登录情况

    last
    

    3.用户和组

    1) 用户操作

    ① 添加一个tom用户,设置它属于users组,并添加注释信息

    useradd -g users -c "hr tom" tom
    

    ② 设置tom用户的密码

    passwd tom
    

    ③ 修改tom用户的登陆名为tom1

    usermod -l tom1 tom
    

    ④ 将tom添加到zwh和root组中

    usermod -G zwh,root tom
    

    2) 用户组操作

    ① 添加一个zwh的组

    groupadd zwh
    

    ② 将tom用户从root组和zwh组删除

    gpasswd -d tom root和gpasswd -d tom zwh
    

    ③ 将zwh组名修改为zwh1

    groupmod -n zwh1 zwh
    

    4.查找

    1) 查找可执行的命令

    which ls
    

    2) 从某个文件夹开始查找

    find / -name "zwh*" -ls
    

    3) 查找并删除

    find / -name "zwh*" -ok rm {} ;
    
    find / -name "zwh*" -exec rm {} ;
    

    4) 查找用户为hadoop的文件

    find /usr -user hadoop -ls
    

    5) 查找用户为hadoop并且(-a)拥有组为root的文件

    find /usr -user hadoop -a -group root -ls
    

     6) 查找用户为hadoop或者(-o)拥有组为root并且是文件夹类型的文件

    find /usr -user hadoop -o -group root -a -type d
    

    7) 查找权限为777的文件

    find / -perm -777 -type d -ls
    

    8) 显示命令历史

    history
    

    5.打包和压缩

    1) gzip压缩

    gzip test.txt
    

    2) 解压

    gzip -d test.txt.gz
    

    3) bzip2压缩

    bzip2 test
    

    4) 解压

    bzip2 -d test.bz2
    

    5) 打包并压缩成bz2

    tar -jcvf a.tar.bz2
    

    6) 解压bz2

    tar -jxvf a.tar.bz2
    

    7) 将当前目录的文件打包

    tar -cvf bak.tar .
    

    8) 解压

    tar -xvf bak.tar
    

    9) 打包并压缩gzip

    tar -zcvf test.tar.gz
    

    10) 解压缩

    tar -zxvf test.tar.gz
    

    11) 解压到/usr/下

    tar -zxvf test.tar.gz -C /usr
    

    12) 查看压缩包内容

    tar -ztvf test.tar.gz
    

    6.防火墙

    Centos7:

    1) 查看防火墙状态

    firewall-cmd --state
    

    2) 停止防火墙

    systemctl stop firewalld.service
    

    3) 开启防火墙

    systemctl start firewalld.service
    

    4) 禁止firewall开机启动

    systemctl disable firewalld.service 
    

    Centos6:

    1) 查看防火墙状态

    service iptables status
    

    2)停止防火墙

    service iptables stop
    

    3)开启防火墙

    service iptables star
    

    4)查看iptables是否开机启

    chkconfig iptables --list
    

    5)设置iptables开机启动

    chkconfig iptables on
    

    6)设置iptables开机不启动

    chkconfig iptables off
    
  • 相关阅读:
    数组及其方法
    Web Worker
    nodejs输入输出
    head标签中的meta
    对象副本的拷贝
    bower指南(一)
    gulp指南(一)
    云服务器搭建
    http协议简单介绍(转)
    使用traits
  • 原文地址:https://www.cnblogs.com/zhaohadoopone/p/13679199.html
Copyright © 2011-2022 走看看