zoukankan      html  css  js  c++  java
  • Linux部分常用命令总结

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

    cat(查看文件数据)

    # 查看多个文件
    [root@stardust ~]# cat 1.txt 2.txt test.txt
    =======================================================================
    # 查看文件并显示行号
    [root@stardust ~]# cat -n /etc/fstab 
    =======================================================================
    # 使用标准输入 利用cat命令 生成文件
    [root@stardust ~]# cat >2.txt<<EOF
    > asd
    > 123
    > eee
    > EOF

    cp(复制)

    [root@stardust ~]# cp -a     # -dRp 归档,保留元信息拷贝,在拷贝软连接时不会跟踪链接文件
    [root@stardust ~]# cp -p     # --preserve=mode,ownership,timestamps 保留属主属组,时间戳,权限拷贝
    [root@stardust ~]# cp -r     # -r -R 递归拷贝 可以拷贝目录
    [root@stardust ~]# cp install.log{,.bak}     # 小技巧 快速备份文件

    curl

    [root@stardust ~]# curl -A                        # 指定User Agent
    [root@stardust ~]# curl -e                        # 指定referer
    
    [root@stardust ~]# curl -H                        # 自定义header信息
    [root@stardust ~]# curl -I                        # 使用HEAD方法请求资源
    
    [root@stardust ~]# curl -L                        # 跟踪地址重定向请求(301,302)
    [root@stardust ~]# curl -k                        # 请求https不验证证书
    
    [root@stardust ~]# curl -0                        # 使用HTTP/1.0
    
    [root@stardust ~]# curl --cacert <file>           # 指定CA证书 (SSL)
    [root@stardust ~]# curl --compressed              # 要求返回是压缩的格式
    [root@stardust ~]# curl --limit-rate <rate>       # 限速访问

    cut(文本列切割)

    # -d 指定分隔符;-f 指定显示的列;
    # --out 简写 指定输出的分隔符 默认还是原来的分隔符
    [root@stardust ~]# cut -d: -f 1,3-5 --out='|' /etc/passwd
    
    
    # -c 按字符取 取6-11,20-$的字符
    cut -c6-11,20- --out=' ' /etc/passwd
    x:0:0: ot:
    :1:1:b /sb
    n:x:2: :/s
    :3:4:a adm
    4:7:lp ool
    own:x: dow

    date(显示或修改时间)

    # 按照指定格式 显示时间
    [root@stardust /]# date +%D
    07/07/17
    [root@stardust /]# date +%F %T
    2017-07-07 02:57:30
    =======================================================================
    # 显示 指定的时间 可以设置 sec min hour day month yead
    [root@stardust /]# date +%F_%T
    2017-07-07_03:00:59
    [root@stardust /]# date +%F_%T -d -20day
    2017-06-17_03:01:10
    [root@stardust /]# date +%F_%T -d +3month
    2017-10-07_03:01:21
    [root@stardust /]# date +%F_%T -d +3year
    2020-07-07_03:01:28
    [root@stardust /]# date +%F_%T -d -30min
    2017-07-07_02:31:33
    =======================================================================
    # 设置时间 格式为 `月日时分年`
    [root@stardust /]# date 080823222017
    Tue Aug 8 23:22:00 CST 2017
    =======================================================================
    # 同步硬件时钟
    [root@stardust /]# hwclock -w     # --systohc 同步到硬件
    [root@stardust /]# hwclock -s     # --hctosys 从硬件同步

    dd(底层复制)

    dd命令是比cp要更加底层的复制命令,因此效率更高

    # `if` 源
    # `of` 目标
    # `bs` 每一个单元的大小
    # `count` 复制多少个单元
    =======================================================================
    # 使用示例
    
    # 文件复制
    [root@stardust tmp]# dd if=/etc/fstab of=./fstab_dd
    
    # 备份mbr
    [root@stardust ~]# dd if=/dev/sda of=mbr.bak bs=512 count=1
    
    # 生成大的测试文件
    [root@stardust /]# dd if=/dev/zero of=/tmp/test_file bs=1M count=5

    echo(命令回显)

    [root@Stardust stardust]# echo 'hmy
    > hmy2
    > hmy3'>1.txt
    =================================================================
    [root@stardust1 ~]# echo -n "hmy"; echo "hmy"     # -n 不换行输出
    hmyhmy
    =================================================================
    # -e 可以解析特殊含义的字符
    # 	 解析为tab
    # 
     解析为换行
    #  解析为退格
    # a 会发出声音警告
    [root@stardust ~]# echo -e "hmy
    test"
    hmy
    test

    history(查看命令历史)

    登录shell时,会读取命令历史文件记录中的命令,默认文件为~/.bash_history
    登录进shell后,新执行的命令只会记录在缓存中,在用户注销时,再追加至history文件中

    [root@stardust ~]# history -a         # 立即将缓存中的数据写入历史文件中
    [root@stardust ~]# history -d num     # 删除制定条目的记录
    [root@stardust ~]# history -c         # 清除历史
    [root@stardust ~]# history num        # 查看指定条目的历史记录

    ls(查看文件信息)

    [root@stardust ~]# ll        # ls -l 长显示 显示详细信息
    total 40
    -rw-------. 1 root root  1066 Sep 20  2016 anaconda-ks.cfg
    -rw-r--r--. 1 root root 24146 Sep 20  2016 install.log
    -rw-r--r--. 1 root root  5890 Sep 20  2016 install.log.syslog
    =======================================================================
    [root@stardust ~]# ll -h        # -h 人类可读 提高可读性
    total 40K
    -rw-------. 1 root root 1.1K Sep 20  2016 anaconda-ks.cfg
    -rw-r--r--. 1 root root  24K Sep 20  2016 install.log
    -rw-r--r--. 1 root root 5.8K Sep 20  2016 install.log.syslog
    =======================================================================
    [root@stardust ~]# ll -d        # -d 查看目录信息
    dr-xr-x---. 2 root root 4096 Jul 14 21:04 .
    =======================================================================
    [root@stardust ~]# ll --full        # --full-time 显示完整时间 可以省略为full
    total 40
    -rw-------. 1 root root  1066 2016-09-20 16:00:47.112999477 +0800 anaconda-ks.cfg
    -rw-r--r--. 1 root root 24146 2016-09-20 16:00:44.754999481 +0800 install.log
    -rw-r--r--. 1 root root  5890 2016-09-20 15:59:20.521999566 +0800 install.log.syslog
    =======================================================================
    [root@stardust ~]# ll --time-style=long        # --time-style 指定时间的显示格式
    total 40
    -rw-------. 1 root root  1066 2016-09-20 16:00 anaconda-ks.cfg
    -rw-r--r--. 1 root root 24146 2016-09-20 16:00 install.log
    -rw-r--r--. 1 root root  5890 2016-09-20 15:59 install.log.syslog
    =======================================================================
    [root@stardust ~]# ll -tr        # -t 按照修改时间排序,-r 倒序
    total 40
    -rw-r--r--. 1 root root  5890 Sep 20  2016 install.log.syslog
    -rw-r--r--. 1 root root 24146 Sep 20  2016 install.log
    -rw-------. 1 root root  1066 Sep 20  2016 anaconda-ks.cfg
    =======================================================================
    [root@stardust ~]# ll -A        # 查看隐藏文件 不包含 . 和 ..
    [root@stardust ~]# ll -a        # 查看所有隐藏文件
    [root@stardust ~]# ll -R        # 递归查看目录下文件
    [root@stardust ~]# ll -S        # 按照文件大小进行排序

    mktemp(创建临时文件或目录)

    # 按照模板创建临时文件 X最少为3个
    [root@Stardust ~]# mktemp filename.XXX
    
    # 创建临时目录
    [root@Stardust ~]# mktemp -d dirname.XXX
    
    # 在指定目录下创建临时文件
    [root@Stardust ~]# mktemp -p DIR

    seq(sequence序列)

    # `seq [start [step]] end` 使用方法
    [root@localhost ~]# seq -s                    # 指定分隔符 默认为
     
    [root@localhost ~]# seq -w 0010               # 自动在前边补零

    screen(打开虚拟屏幕)

    # 打开虚拟屏幕  使用`Ctrl + a,d`释放screen
    [root@stardust ~]# screen
    
    # 查看正在运行的虚拟屏幕
    [root@stardust ~]# screen -ls
    
    # 恢复虚拟屏幕
    [root@stardust ~]# screen -r PID

    sleep(阻塞指定时长)

    # 单位支持 `s m h d`
    [root@localhost ~]# sleep 5

    sort(排序)

    [root@stardust ~]# sort -u files.txt               # 排序后去重显示
    [root@stardust ~]# sort -r files.txt               # 降序排序
    [root@stardust ~]# sort -f files.txt               # 忽略大小写(默认貌似就是忽略大小写的)
    [root@stardust ~]# sort -n files.txt               # 以数值排序 
    [root@stardust ~]# sort -t files.txt               # 指定分隔符
    [root@stardust ~]# sort files.txt -o res.txt       # 可以将结果写入到指定文件
    [root@stardust ~]# sort -k2 files.txt              # 指定以第二列排序
    [root@stardust ~]# sort -k2.3 files.txt            # 指定以第二列第三字符排序
    [root@stardust ~]# sort -k1.4,2.3 files.txt        # 指定以第一列第四字符,第二列第三字符排序

    tee

    从标准输入获取结果后 输出两份分别至标准输出和文件 默认是覆盖输出

    [root@stardust ~]# cat test.txt |tee -a 2.txt        # -a 追加写 默认是覆盖

    tree(查看目录层级结构)

    [root@Stardust stardust]# tree -L 2 /data/     # -L指定显示的深度 
    /data/
    -- stardust
        |-- dir1
        |-- file1.txt
    2 directory, 1 files
    [root@Stardust stardust]# tree -d  /data/      # 只显示目录 不显示文件
    /data/
    -- stardust
        |-- dir1
    2 directory

    touch(时间戳管理工具)

    # 一个文件拥有三个时间戳
    # AccessTime 访问时间 atime 文件被读取,内容被修改时会改变此时间戳
    # ModifyTime 修改时间 mtime 文件被修改时会改变,访问不会改变此时间戳
    # ChangeTime 改变时间 ctime 元数据发生改变时会改变(文件名 属主属组 其他两个时间戳)
    [root@stardust ~]# stat install.log
    Access: 2017-07-07 02:51:28.484317960 +0800
    Modify: 2016-05-24 11:09:49.158999844 +0800
    Change: 2016-05-24 11:10:02.474999841 +0800
    
    
    # 修改文件的atime和mtime
    [root@stardust ~]# touch -t 201501010101.23 install.log
    [root@stardust ~]# touch -d '18:50 07/25/2014' install.log
    [root@stardust ~]# touch -d '4:50pm 07/25/2014' install.log
    
    [root@stardust ~]# touch -a install.log     # 仅修改atime
    [root@stardust ~]# touch -m install.log     # 仅修改mtime

    tr(字符串替换)

    # 通过输入重定向 进行字符串替换
    [root@stardust ~]# cat test.txt
    adsdqwer
    [root@stardust ~]# tr abc 123 < test.txt
    1dsdqwer
    [root@stardust ~]# tr adq 123 < test.txt  
    12s23wer
    =======================================================================
    # 删除指定字符串
    [root@stardust ~]# cat test.txt 
    adsdqwer
    [root@stardust ~]# tr -d qw < test.txt
    adsder

    uname(显示运行中的内核相关信息)

    [root@stardust ~]# uname -a                 # 显示all信息
    [root@stardust ~]# uname -n                 # 显示主机名
    [root@stardust ~]# uname -r                 # 显示内核 版本号+release号
    
    # 以下三项显示的信息 一般是相同的
    [root@stardust ~]# uname -m                 # 显示CPU硬件类型
    [root@stardust ~]# uname -p                 # 显示处理器类型
    [root@stardust ~]# uname -i                 # 显示硬件平台类型

    uniq(去重)

    # 去重 并统计重复行的数量
    [root@stardust ~]# uniq -c files.txt 
    
    # 仅显示出现次数大于一次的行
    [root@stardust ~]# uniq -d files.txt 
    
    # 仅显示唯一的行,即出现次数为一次的行
    [root@stardust ~]# uniq -u files.txt 

    watch(定时自动刷新查看)

    # `-n` 指定间隔时间单位是秒 自动执行命令
    [root@stardust ~]# watch -n TIME COMMAND

    wget(网页下载器)

    [root@stardust ~]# wget -q URL                    # 静默模式
    [root@stardust ~]# wget -c URL                    # 断点续传
    [root@stardust ~]# wget -O URL                    # 保存位置
    [root@stardust ~]# wget --limit-rates URL         # 限速

    alias和unalias(别名)

    # alias和unalias是临时生效的,永久生效需要修改配置文件
    [root@Stardust stardust]# alias rm='echo hmy'
    [root@Stardust stardust]# rm
    hmy
    =======================================================================
    # cp,mv,rm在使用时,系统默认别名会加上`-i`进行交互式操作,如果不想进行交互式操作:
    # 方法一:/bin/cp     #使用全路径,就不调用别名
    # 方法二:cp         #转义符可以使别名失效




  • 相关阅读:
    php多态
    ssl certificate problem: self signed certificate in certificate chain
    test plugin
    open specific port on ubuntu
    junit vs testng
    jersey rest service
    toast master
    use curl to test java webservice
    update folder access
    elk
  • 原文地址:https://www.cnblogs.com/hemingyuan/p/7914409.html
Copyright © 2011-2022 走看看