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

    bash shell的文件头

    #!/bin/bash

    expect的文件头

    #!/usr/bin/expect

    1.设置文件编码

    在vi的命令模式下 输入

    :set fileencoding=utf-8

    2. enconv 转换文件编码,比如要将一个GBK编码的文件转换成UTF-8编码,操作如下

    enconv -L zh_CN -x UTF-8 filename

     3.查看端口占用情况

    netstat -tunlp |grep 22
    #需要安装 net-tools

     4.查看已开启的服务

    systemctl list-unit-files|grep enabled

     5.按正则表达式查找指定目录下的文件,并根据内容找出的行

    find / -type f -name "*.txt" | xargs grep "somecontent"

    6.从文件内容查找与正则表达式匹配的行:

    grep –e 'hello*' ./yyy.txt

    7.从文件内容查找与正则表达式匹配的行,查找时不区分大小写:

    grep –i "word" ./yy.txxt  [-v 取反] [^$...正则]

    8.查看某路径下的文件树结构

    tree -C -D -s -p  /home   #tree这个命令需要安装

    9.在后台启动服务(因为有些服务启动之后会挂起命令窗口,关闭连接客户端的时候 服务也会关闭)

    nohup <command> &   如  nohup bin/hive --service metastore &

    10.列出文件内容并展示行号

    nl filename 或 cat -n filename

    11. 使用命令编辑文件内容(对整行进行增删改,正则匹配并替换字符串等)。参考 Linux大神养成之正则表达式(grep,sed)

    nl filename | sed '2,5d'   #将展示的文件内容删除2到5行 (文件不变)
    sed -i '3c newcontent' filename #将第三行替换成 newcontent
    sed -i '4a newcontent' filename  #在第4行后面追加一行,内容是newcontent
    sed -i '4i newcontent'  filename  #在第4行前面插入一行,内容是newcontent
    sed -i 's/oldcontent/newcontent/g' filename  #将匹配oldcontent的内容替换成newcontent

    12.按行读取文件内容

    #1
    while read line
    do
        echo $line
    done < filename
    
    #2
    cat filename | while read line
    do
        echo $line
    done

     解压缩

    #压缩
    tar -zcvf filename.tgz targetDir
    #查询
    tar -ztvf -f filename.tgz
    #解压缩
    tar -zxvf -f filename.tgz -C targetDir

    读取用户输入

    read -p 'Enter some words :' word
    echo entered words is $word

    服务开机启动

    chkconfig servicename on
    chkconfig servicename off #取消开机启动

    让脚本开机启动
    echo 'some scipt' >> /etc/rc.local

     根据进程名关闭进程

    kill `jps |grep Elasticsearch |cut -c1-5`

     查看目录占用大小

    du -lh --max-depth=1  /opt/module/

     CentOS7升级内核

    rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org
    rpm -Uvh http://www.elrepo.org/elrepo-release-7.0-3.el7.elrepo.noarch.rpm
    yum --enablerepo=elrepo-kernel install -y kernel-ml
    sed -i s/GRUB_DEFAULT=saved/GRUB_DEFAULT=0/g /etc/default/grub
    grub2-mkconfig -o /boot/grub2/grub.cfg
    
    echo 到这一步需要重启
    
    echo 然后使用以下命令删除旧内核数据 
    #rpm -qa | grep kernel
    #yum autoremove kernel-3.10.0-327.13.1.el7.x86_65

    免交互设置SSH免密登录

    ssh-keygen -t rsa -P "" -f ~/.ssh/id_rsa
    
    yum install -y sshpass
    sshpass -p '123456' ssh-copy-id -o StrictHostKeyChecking=no -i /root/.ssh/id_rsa -p 22 root@node1

    hostname修改

    修改 /etc/hostname是永久生效,但是需要重启机器

    使用命令 hostname  newhostname 是立即生效,但是重启后会失效

    所以可以通过这两种方式结合,实现修改后立即且永久有效

    以某用户执行命令

    1
    su
    -c testuser <somecommand> 2
    sudo -u testuser <somecommand>

    查看系统资源 

     

    #实时资源占用列表(CPU、MEM、SWAP)
    top
    
    #当前内存占用统计
    free [option]
    option为  -g  -m -k
    
    
    #历史资源使用情况
    sar

     

     

  • 相关阅读:
    python深浅拷贝
    软件开发目录规范
    编码规范
    python进程、线程、协程的介绍及使用
    soket粘包问题及解决方案
    python socket通信
    数据开发_机器学习
    数据开发_开发工具以及工具链
    数据开发_Python读取文件
    数据开发_Java设计模式_IO以及读取资源文件
  • 原文地址:https://www.cnblogs.com/TiestoRay/p/6117465.html
Copyright © 2011-2022 走看看