zoukankan      html  css  js  c++  java
  • Linux常用命令

    命令总结

    uname 查看内核及版本信息
    uname -m -r -a
    [root@Aaron ~]# uname -m
    i686

    [root@Aaron ~]# uname -r
    2.6.32-642.el6.i686

    [root@Aaron ~]# uname -a (查看内核版本信息)
    Linux Aaron 2.6.32-642.el6.i686 #1 SMP Tue May 10 16:13:51 UTC 2016 i686 i686 i386 GNU/Linux

    [root@Aaron ~]# cat /etc/redhat-release
    CentOS release 6.8 (Final)


    启动或重启网卡
    ifup eth0
    ifdown eth0
    /etc/init.d/network restart 针对所有网卡


    调整字符集
    LANG=en
    字符集就是一套文字符号及其编码
    [root@Aaron ~]# cat /etc/sysconfig/i18n
    LANG="zh_CN.UTF-8"
    修改:1.cp
    2.sed -i 's#LANG="en"#LANG="zh_CN.UTF-8"#g' /etc/sysconfig/i18n
    3.source /etc/sysconfig/i18n
    4.修改客户端 LANG="zh_CN.UTF-8"


    编辑文件
    vim vi nano


    export 定义环境变量


    打印输出字符串
    echo
    重定向
    >(覆盖) >>(追加)
    [root@Aaron ~]# ech "111" >oldboy.txt 2>oldgirl.txt 把错误提示信息放到oldgirl里面

    [root@Aaron ~]# cat oldgirl.txt
    -bash: eco: command not found

    错误和正确的都扔到黑洞设备
    /dev/null 2>&1 (常用 )
    1>/dev/null 2>/dev/null
    & >/dev/null

    直接清空文件内容
    >m.txt


    cat 查看文件内容
    cat -n 查看行号 加序号
    [root@Aaron ~]# nl /etc/passwd (也可以添加行号)

    [root@Aaron ~]# echo "111" >oldboy.txt
    如果没有该文件则自动创建新文件
    [root@Aaron ~]# cat oldboy.txt
    111

    [root@Aaron ~]# cat oldboy.txt 如果没有该文件则自动创建新文件
    111

    [root@Aaron ~]# cat >>oldboy.txt<<EOF
    I am studying Liunx
    EOF

    [root@Aaron ~]# cat oldboy.txt
    111
    I am studying Liunx


    ls -l 长格式查看目录或文件信息
    -h 以人类可读的形式
    -a 显示所有的目录或文件(包括隐藏目录和文件)
    -F 给不同的文件类型标识
    -lrt 按时间倒序排序
    -color=auto

    diff & vimdiff (直接接两个文件)
    diff /etc/ssh/sshd_config /etc/ssh/sshd_config.20161104


    cp 源文件 目标文件
    cp -p 保持属性
    -a 相当于-pdr 文件的所有属性都一起复制过来
    -r 递归,用于复制目录
    -i 若目标文件已经存在时,在覆盖时会先询问操作的进行


    mv source destination 移动文件和目录,或者改名(rename)


    rm -f 强制 (生产场景尽量不要使用,要用的话也要先cp备份)
    -r 删除目录
    替代方法:mv到一个临时目录,用find删除


    find 查找
    find -a 同时成立 -o 或
    -type f,d(目录),-name,! 取反
    -maxdepth n
    -mtime (修改时间)
    -ctime
    -atime 访问时间(cat)
    find /root/data/ -type f|xargs rm -f 注意:xargs处理的是文件或者目录,文件内容不用接
    find /root/data -type f -exec rm -f {} ;
    删除一个目录下的所有文件但保留一个指定文件:
    find /root/data -type f ! -name "oldboy"|xargs rm -f
    [root@Aaron ~]# find ./ -maxdepth 3 -type d


    head 取文件的前n行(默认是前10行)
    [root@Aaron ~]# head -3 /etc/passwd
    root:x:0:0:root:/root:/bin/bash
    bin:x:1:1:bin:/bin:/sbin/nologin
    daemon:x:2:2:daemon:/sbin:/sbin/nologin

    [root@Aaron ~]# head -n 3 /etc/passwd
    root:x:0:0:root:/root:/bin/bash
    bin:x:1:1:bin:/bin:/sbin/nologin
    daemon:x:2:2:daemon:/sbin:/sbin/nologin

    tail 取文件的后几行
    tail -3 /etc/passwd
    tail -n 3 /etc/passwd
    tail -f 跟踪一个文件尾部的实时变化


    ex:
    方法1:
    [root@Aaron ~]# cat -n /etc/passwd|head -25|tail -5
    21 saslauth:x:499:76:Saslauthd user:/var/empty/saslauth:/sbin/nologin
    22 postfix:x:89:89::/var/spool/postfix:/sbin/nologin
    23 rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
    24 nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
    25 abrt:x:173:173::/etc/abrt:/sbin/nologin

    方法2:sed -n '20,25p' /etc/passwd


    grep 过滤器
    grep -v 不要
    [root@Aaron ~]# grep -v "aaa" oldboy.txt
    bbb
    ccc
    grep 30 -B 10 test.txt ???
    -A
    -C

    grep -E 相当于 egrep 和扩展的正则表达式配合使用
    [root@Aaron ~]# grep -E "3306|1521" /etc/services

    [root@Aaron ~]# egrep "3306|1521" /etc/services
    mysql 3306/tcp # MySQL
    mysql 3306/udp # MySQL
    ncube-lm 1521/tcp # nCube License Manager
    ncube-lm 1521/udp # nCube License Manager

    grep -o 只匹配结果
    [root@Aaron ~]# grep -o dd oldboy.txt
    dd

    grep -i 不区分大小写

    grep -n 显示行号
    [root@Aaron ~]# grep -n "nn" oldboy.txt
    1:nnm
    2:nnm


    sed 取行
    -n 取消默认输出,只会改变输出的结果,原内容不变
    -i 改变内容
    -r 不用转义
    -d 删除
    [root@Aaron ~]# sed -n '/aa/p' oldboy.txt p是打印
    aaa

    [root@Aaron ~]# sed '/aa/d' oldboy.txt
    bbb
    ccc

    [root@Aaron ~]# sed -i '/aa/d' oldboy.txt d是删除所在行

    [root@Aaron ~]# cat oldboy.txt
    bbb
    ccc

    取多行
    sed -n '20,25p' /etc/passwd

    取某一行
    sed -n '30p' /etc/passwd

    sed 替换 s代表查找并替换,g表示对当前全局匹配替换,#是分隔符,可以用“=”等特殊字符替换
    [root@Aaron ~]# sed -i 's#bb#aa#g' oldboy.txt

    [root@Aaron ~]# cat oldboy.txt
    aab
    ccc

    [root@Aaron ~]# find . -type f -name "oldboy.txt"|xargs sed -i 's#mmm#nn#g'

    [root@Aaron ~]# cat oldboy.txt
    nnm
    nnm
    mmc

    把不同目录底下的某一个同名文件里面的内容全替换掉
    find /root/data -type f -name "test.txt"|xargs sed -i 's#bb#aa#g'
    find /root/data -type f -name "test.txt" -exec sed -i 's#bb#aa#g' {} ;
    sed -i 's#bb#aa#g' `find /root/data -type f -name "test.txt"`

    后项引用(正则表达式,贪婪模式)
    sed -r 's#(.*) (.*)#1 2#g' oldboy.txt
    sed -r 's#(.*)#I am 1#g'
    [root@Aaron ~]# sed -rn 's#(.*) (.*)#I am 1,not 2#gp' oldboy.txt
    I am oldboy,not oldgirl (只是改变输出的内容,原内容没有改变,除非-i)

    [root@Aaron ~]# sed 's#(^I.*$)#oldboy oldgirl#g' oldboy.txt
    oldboy oldgirl

    [root@Aaron ~]# sed '=' /etc/rsyslog.conf =表示行号
    1
    # rsyslog v5 configuration file


    awk 一门语言 过滤内容(一般取列),打印删除
    awk '{print $1}' 文件
    awk -F 指定分隔符
    awk -F ":" '{print $1" "$2" "$3}' /etc/passwd
    awk -F ":" '{print $(NF-1)}' /etc/passwd NF 最后一列
    awk -F ":" '{if(NR>4 && NR<8) print $(NF-1)" "}' /etc/passwd

    [root@Aaron ~]# awk '{print NR" "$0}' oldboy.txt
    ...
    8 oldboy oldgirl
    [root@Aaron ~]# awk '{print NR" "$1}' oldboy.txt
    ...
    8 oldboy
    [root@Aaron ~]# awk '{print NR" "$2}' oldboy.txt
    ...
    8 oldgirl

    多分隔符
    [root@Aaron ~]# cat hello.txt
    I am oldboy,my qq is 423525

    [root@Aaron ~]# awk -F "[, ]" '{print $3" "$7}' hello.txt
    oldboy 423525


    cut 取列
    cut -d
    -c 按字节
    [root@Aaron ~]# cat hello.txt
    aaa bbb ccc ddd eee fff
    [root@Aaron ~]# cut -d" " -f3,6 hello.txt
    ccc fff

    cut -c 6-11,20- oldboy.txt


    man command
    command --help
    help command (bash的内置命令,如cd,pwd,echo)


    mkdir -p 递归创建目录


    rmdir 删除空目录


    yum Linux里的包管理器,帮助解决依赖问题
    yum install tree -y 下载tree包然后调用rpm命令安装tree包,如果依赖包,帮你提前下载安装
    yum update 更新所有软件到最新版本,线上系统尽量不做
    yum groupinstall 包组 -y
    yum grouplist 查看包组列表
    yum search 包 查找

    rpm
    rpm -ivh 依赖包问题不好解决
    -i 安装
    -v 显示输出
    -h human
    rpm -qa 查询


    tree 显示目录树结构
    tree -L 1 /
    -d 只显示目录

    mount 挂载
    mount /dev/sda1 /etc


    alias 别名
    [root@Aaron ~]# alias
    alias cp='cp -i'
    alias mv='mv -i'
    alias rm='rm -i'

    [root@Aaron ~]# alias oldboy='echo "I am oldboylinux"'

    [root@Aaron ~]# oldboy
    I am oldboylinux

    unalias 取消别名(命令行处理只是临时生效,重启后就会失效)
    [root@Aaron ~]# unalias oldboy

    [root@Aaron ~]# oldboy
    -bash: oldboy: command not found

    [root@Aaron ~]# grep alias /root/.bashrc
    # User specific aliases and functions
    alias rm='rm -i'
    alias cp='cp -i'
    alias mv='mv -i'

    别名生效的位置:针对root用户:/root/.bashrc
    所有用户都生效的位置:/etc/bashrc & /etc/profile 把命令写到里面
    然后执行:source /etc/profiles

    不用别名
    /bin/cp /mnt/test.txt /tmp/
    cp


    seq
    [root@Aaron ~]# seq 3
    1
    2
    3

    [root@Aaron ~]# seq 1 3
    1
    2
    3
    [root@Aaron ~]# seq 1 2 5
    1
    3
    5
    seq -s 指定序列的分隔符
    [root@Aaron ~]# seq -s "=" 3
    1=2=3

    seq -w
    [root@Aaron ~]# seq -w 3
    01
    02
    03


    wc 显示行数
    wc -l
    -c 打印字节数
    -m 打印字符数
    [root@Aaron ~]# wc -l /etc/passwd
    30 /etc/passwd

    [root@Aaron ~]# wc /etc/passwd
    30 51 1410 /etc/passwd

    [root@Aaron ~]# wc -c /etc/passwd
    1410 /etc/passwd

    [root@Aaron ~]# wc -m /etc/passwd
    1410 /etc/passwd

     


    history 查看历史命令
    history -c 清空历史命令
    -d 123 清楚第123条历史
    HISTORYSIZE=5 限制查看的历史命令数
    如果要永久生效,则:1.echo "export HISTORYSIZE=5" >>/etc/profile
    2.source /etc/profile


    关机,重启,注销命令:

    关机
    shutdown -h now
    shutdown -h +1 1分钟后关机

    init 0

    halt 立即停止系统,需要人工关闭电源
    halt -p

    poweroff 立即停止系统并关闭电源

    重启
    reboot

    shutdown -r now
    shutdown -r +1 1分钟后重启

    init6

    注销
    logout
    exit
    ctrl+d


    who & whoami
    [root@Aaron ~]# who
    root tty1 Nov 2 16:28
    root pts/1 Nov 3 06:00 (192.168.0.108)
    [root@Aaron ~]# whoami
    root


    which 查找命令所在路径
    [root@Aaron ~]# which passwd
    /usr/bin/passwd

    [root@Aaron ~]# echo $PATH
    /usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin

    [oldboy@Aaron ~]$ echo $PATH
    /usr/local/bin:/bin:/usr/bin:/home/oldboy/bin
    PATH系统路径变量,执行ls、cp等非内置命令时,系统会查找PATH里对应的路径是否有,如果没有就报告找不到该命令
    添加路径:export PATH="/tmp:$PATH" (临时生效)
    永久生效:1.放到 /etc/profile alias
    2.source /etc/profile 使立即生效
    内置命令暂时不能用visudo管理


    whereis
    whereis -b 查二进制所在文件
    [root@Aaron ~]# whereis -b passwd
    passwd: /usr/bin/passwd /etc/passwd

    [root@Aaron ~]# whereis passwd
    passwd: /usr/bin/passwd /etc/passwd /usr/share/man/man1/passwd.1.gz /usr/share/man/man5/passwd.5.gz


    locate


    su - 用户名 角色切换
    - 携带环境变量,切换用户的环境变量
    visudo <==> vi /etc/sudoers
    su - root <==> sudo su -root
    sudo -l 查看你有什么权限,前提是你登录到用户下面
    visudo -c 检查语法


    getenforce 检查SELinux状态


    setenforce 设置SELinux状态,1 启用 0 警告、不启用
    setenforce 0


    runlevel 查看运行级别
    [root@Aaron ~]# runlevel
    N 3


    init 切换运行级别
    init 5 <==> startx
    开机加载的配置文件
    /etc/inittab
    如果root密码忘了,必须以单用户模式启动
    0 关机模式 1 单用户模式 2 无NFS的多用户 3 文本模式(完整的多用户模式)
    4 未使用的 5 图形模式 6 重启模式


    chkconfig 设定开机自启动服务(越少越好,无用的服务不开启)
    chkconfig --list|grep 3:on
    chkconfig --list atd
    [root@Aaron ~]# chkconfig --list atd
    atd 0:off 1:off 2:off 3:on 4:on 5:on 6:off

    [root@Aaron ~]# chkconfig atd off

    [root@Aaron ~]# chkconfig --list atd
    atd 0:off 1:off 2:off 3:off 4:off 5:off 6:off

    [root@Aaron ~]# chkconfig --list atd
    atd 0:off 1:off 2:on 3:on 4:on 5:on 6:off

    [root@Aaron ~]# chkconfig --level 234 atd off (不加level,2,3,4,5一起处理))

    [root@Aaron ~]# chkconfig --list atd
    atd 0:off 1:off 2:off 3:off 4:off 5:on 6:off

    chkconfig管理脚本要求:
    1.脚本必须在/etc/init.d 目录下,并且能用/etc/init.d/oldboy restart这种格式正常启动
    因为chkconfig处理的命令是:/etc/init.d/sshd restart(start)
    ex:chkconfig --level 35 on
    <==> rm -f /etc/rc3.d/k25sshd 为关闭的顺序
    ln -s /etc/initd/sshd
    /etc/rc3.d/S55sshd 55为启动的顺序
    注意:[root@Aaron ~]# ls -lh /etc/init.d
    lrwxrwxrwx. 1 root root 11 Oct 8 00:49 /etc/init.d -> rc.d/init.d

    2.脚本开头: #chkconfig: 35 56 24
    #description: oldboy linux test


    查看端口
    netstat -lntup|grep ssh ss -lntup|grep ssh
    [root@Aaron ~]# netstat -lntup|grep ssh
    tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1613/sshd
    tcp 0 0 :::22 :::* LISTEN 1613/sshd

    [root@Aaron ~]# ss -lntup|grep ssh
    tcp LISTEN 0 128 :::22 :::* users:(("sshd",1613,4))
    tcp LISTEN 0 128 *:22 *:* users:(("sshd",1613,3))


    ps -ef 查看进程


    ifconfig or ip add 查看配置好的ip


    关闭防火墙
    /etc/init.d/iptables stop
    services iptables stop


    ulimit 查看文件描述符
    [root@Aaron ~]# ulimit -n
    1024


    sysctl -p 让配置生效

    chattr 锁定关键文件系统
    chattr +i /etc/passwd /etc/shadow /etc/group /etc/gshadow /etc/inittab
    chattr -i


    lsattr


    wget 下载
    wget http://url
    wget -O 把...下载到...改成...名字


    ntpdate 更新互联网时间


    less <==> cat 按屏或按行(Enter)查看文件
    [root@Aaron ~]# less -N /etc/rsyslog.conf 显示行号


    more 不能往上走,只能Enter和space


    df -h 查看分区情况
    [root@Aaron ~]# df -h
    Filesystem Size Used Avail Use% Mounted on
    /dev/sda5 17G 2.0G 14G 13% /
    tmpfs 504M 0 504M 0% /dev/shm
    /dev/sda1 190M 31M 150M 18% /boot
    /dev/sda2 1.9G 3.0M 1.8G 1% /home


    tar 打包 尽量切换到打包目录的上级目录,然后用相对路径打包
    tar -zcvf
    -z 调用gzip压缩
    -c 创建文件
    -v 显示创建过程
    -f 文件
    tar -zcvf a.tar.gz --exclude=oldboy/wodi.gz ./oldboy
    tar -zcvf a.tar.gz --exclude=oldboy/*.gz ./oldboy
    tar -zcvfX a.tar.gz paichu.list ./oldboy/ X <==> --exclude-from FILE

    tar tf 查看包里有什么 
    t 列表
    f 文件

    tar xf a.tar.gz -C /tmp/
    -C 指定往哪解压
    -P 不要用

     

    基础正则表达式:
    . 单个任意字符
    * 重复前面任意0个或多个字符
    .*匹配任意字符


    通配符
    * 代表任意(0个或多个)字符
    ?代表任意一个字符
    # 配置文件注释
    | 管道
    $ 变量前需要添加的符号
    / 路径分隔符
    转义
    {}输出字符序列或数字序列
    > >> < << 输入输出重定向,覆盖 追加重定向
    ` 两个反引号中间为命令,会先执行,等价于 $()
    ! 非
    ..上一级目录
    . 当前目录
    ~ 用户的家目录
    - 用户上一次所在目录
    ;连续不同命令的分隔符,即两个命令在同一行执行 ex:pwd;pwd
    ' 不具有变量置换功能,输出时所见即所得
    ""具有变量置换功能,解释变量后输出
    [root@Aaron ~]# a=oldboy
    [root@Aaron ~]# echo '$a'
    $a
    [root@Aaron ~]# echo "$a"
    oldboy


    {}
    [root@Aaron ~]# echo {1..10}
    1 2 3 4 5 6 7 8 9 10
    mkdir stu{1..10}
    echo {a..z}
    echo stud{001..100}


    echo cat history mkdir rmdir rm mv cp sed grep egrep awk cut ls alias ualias dd df du ps fsck id chkconfig cd chage uname ulimit mount nl logout exit sudo su chattr lsattr vi vim diff wc yum rpm mkfs ifconfig crontab chmod chown chgrp find shutdown halt reboot init tar usreradd groupadd less more wget whereis which locate
    chkconfig

     

  • 相关阅读:
    hadoop 2.x 简单实现wordCount
    httpClient连接超时设置
    Java io使用简介
    log4j使用教程
    F#中的自定义隐式转换
    Computation expressions and wrapper types
    Introducing 'bind'
    Understanding continuations
    Computation expressions: Introduction
    MySQL优化总结,百万级数据库优化方案
  • 原文地址:https://www.cnblogs.com/liangweixiong/p/6418047.html
Copyright © 2011-2022 走看看