Linux大棚命令记录

  1. 查看系统支持的shell: cat  /etc/shells
  2. 查看当前系统用的shell: echo $SHELL
  3. 从bash切换到zsh: 先yum安装,然后 chsh -s /bin/zsh ,退出重新登录即可(chsh -s 实际上是修改了/etc/passwd文件)
  4.  env和export显示的环境变量
  5. set和declare显示的环境变量和自定义变量
  6. 指定PATH变量: export PATH=$PATH:/home/roc
  7. 有export则指定的是环境变量,没有export 的是指定自定义变量
  8. 在子进程中自定义变量无效的
  9. 从键盘输入自带提示语:
    1. read -p "please input your name and place:"
      echo "welcome $REPLY"  $REPLY变量输出结果

    2. #!/bin/bash
      if read -t 5 -p "please input your name and place:" name
      then
      echo "welcome $name"
      else
      echo "sorry,too slow"
      fi
      exit 0  #用-t设置超时时间,在read语句后面添加变量来接受输入

    3.  if read -s -p "please input your name and place:" name    # -s用来输入密码

    4. exec 3< c.txt
      count=0
      while read -u 3 var
      do
      let count=$count+1
      echo "Line $count:$var"
      done
      echo "finished"
      echo "Line no is $count" #从文件读入

    5. #!/bin/bash
      count=0
      cat c.txt | while read line
      do
      echo "Line $count:$line"
      let count=$count+1
      done
      echo "finiished"
      echo "Line no is $count"
      exit 0  #这种方法的结果是返回1 ,因为管道的两边一般需要新建进程,当执行完while后,新进程结束,当脚本执行结束后,输出的count是原来进程中的count,而不是while中的count了

    6. #!/bin/bash
      count=0
      while read line
      do
      let count=count+1
      echo "Line $count:$line"
      done< c.txt
      echo "finished"
      echo "Line no is $count"    #使用重定向技术可以避免像上一个脚本在循环中又新建变量的问题


  • scp -r root@43.224.34.73:/home/lk /root  # 将远端的文件夹下载到本地的/root
  • scp local_file remote_username@remote_ip:remote_file # 从本地复制到远程
  • nohup ./a.out &
    1.  ./a.out &:用ctrl+c的话照样运行,但是断开远程终端就停止,nohup ./a.out:按ctrl+c会停止,但是关了终端仍然运行
    2. nohup ./a.out &  :ctrl+c和关闭终端都可以运行,类似于守护进程

  1.  alias vi='vim'  #如果起的别名和原来的命令重复则shelll默认使用别名,如果想使用原来的命令则通过/bin/vi这样的形式使用
  2.  #!/bin/bash --login 用这行代替/bin/bash可以使用系统中的别名,加上--login的作用是使shell称为一个login shell,可以读取用户的profile和rc文件
  3.  !15 #打开history命令后执行需要的命令
  4.    防止密码被保存到history的方法: 
    1.  export HISTCONTROL=ignorespace 
    2.  输入重要命令功能的时候在前面加一个空格
    3.    在输入命令前输入export=HISTINGNORE=*
    4.    在输入命令结束后再次输入exprot=HISTINGNORE=
  5.   xargs #将echo作为自己的标准输入,eg: echo a.txt | cat #则会读取a.txt的内容
  6. file -i a.txt #a.txt: text/plain; charset=us-ascii  
  • 列出了MIME类型:用来标记文件打开方式
  • 超文本标记语言文本 .html,.html text/html 
      普通文本 .txt text/plain 
      GIF图形 .gif image/gif 
      JPEG图形 .jpeg,.jpg image/jpeg 
      .pdf     application/pdf
      GZIP文件 .gz application/x-gzip 
      TAR文件 .tar application/x-tar

  • file -f a.txt  #用于输出文件列表的文件类型
  •  find test a b c d -name a.txt  #在几个文件夹之间查找
  • find . -type f -mmin -5 # 5分钟以内有哪些文件被修改过
  • find . -type f -mmin +5  # 5分钟之前有哪些文件修改过
  • find . -type f -size +1M  -exec ls -al {} ;   #查找大于1M的文件
    • +1M #大于1M
    • -1M  # 小于1M
    • 1M #等于1M
    •   还有k,G
  •  find / -maxdepth 1 #搜索深度为1
  •    find . -type f -a -name "aa" # 查找文件,文件名为aa

  


  1. du -sh #列出当前文件夹的占用量
  2. du -ah # 列出每个文件和文件夹的使用量
  3. du -ah | sort -hr #列出大文件
  4. du显示的是一个blok的大小,ls显示的是实际大小

echo的用法: 

  单引号无视所有特殊字符,所有字符都是普通的字符

   双引号识别 $    和`

grep用法

  grep -n root /etc/passwd # 将含有root的行找出来并加上行号

grep查找前和后相邻行:

  After1行 :grep -A 1 root /etc/passwd

  Before1行: grep -B 1 root /etc/passwd

  前后1 行: grep -C 1 root /etc/passwd

不区分大小写: grep -i "aa" /etc/passwd

找词: grep  -w "bin"  /etc/passwd

egrep '^root | bash$' /etc/passwd

   


sed命令用法:

    head -n /etc/passwd | sed 's/:.*$//' #替换冒号后面的值

    cat /etc/passwd | sed -n '10,20p' # 打印10到20行的内容

    cat /etc/passwd | sed '/^[r]/s/o/AA/g'  # 把r开头的行中的o替换为AA     

    # /AA/s/BB/CC/g  匹配文件中带有AA的行,并且将所有BB替换成CC

 cat mytext.txt | sed 's/Bei/&2008/'  # &为在后面拼接 eg: Beijing ----Bei2008jing

cat mytext.txt | sed 'y/ei/ie/' #将Beijing替换成Biejing

sed '/200[4-6]/w new.txt' mytext.txt #把筛选结果写入新的文件中


 awk用法:

  awk 'NR%2==0 {next} {print NR,$0}' input.txt # 打印奇数行

  awk '{print $0 }' input.txt test.txt  #两个文件一起输出

  awk 'NR=FNR&&FNR==1 {print FILENAME,$0} NR>FNR&&FNR==8 {print FILENAME,$0}' my,txt test.txt #将第一个文件的第一行数据和第二个文件的第八行数据还有文件名输出

 


cut的用法:

  who | cut -b 3 # 获取每行的第三个字节

  who | cut -b 8,3-5 # 获取每行第八,三到五个字节,顺序按照文本的顺序,所以和3-5,8等价

  cat /etc/passwd | head -n 5 | cut -d : -f 1 # -d:指定分隔符 -d:指定第一个域

  cat /etc/passwd | head -n 5 | cut -d : -f 1-2 # -d:指定分隔符 -d:指定第一到第二个域

  cat /etc/passwd | head -n 5 | cut -d : -f 1-2 # -d:指定分隔符 -d:开头到第二个域

      cat test.txt | cut -d ' ' -f 1 # 以空格为分隔符

      split -b 400M a.txt # 拆分成400Mb的文件

    cat xaa xab > a.txt # 合并文件

    md5sum b.txt # 检查合并后的文件是否完整

       split -b 400M a.tx b_   #指定前缀的拆分

     split -b 400M  -d a.tx b_   #-d 指定数字的拆分

    split -d -10 c.txt bb_ # -10:指定每10行拆分

    md5sum b.txt # 检查合并后的文件是否完整

 sort用法:

  -u:消除重复

  -r:倒序

  sort -r number.txt -o number.txt # 保存到原文件

  -n: 按照数值来排序而不是ascii码


vim用法:

%s/abc/xyz/g  # 全文替换字符串abc为xyz

%s/([0-9]{4}-[0-9]{2}-[0-9]{2})/'1'/g   # 把2001-01-01 变成 '2001-01-01'


des解密:

openssl enc -des3 -d  -in passwd.des3 -out passwd.txt  

des加密:

openssl enc -des3 -k 123 -in mysed.txt -out passwd.des3  #123为密码

   

  

  

    

  

  

原文地址:https://www.cnblogs.com/Baronboy/p/9163925.html