Linux常用命令总结

  1. 观察linux系统上虚拟内存的当前状态

    [root@localhost ~]# cat/proc/meminfo
    MemTotal:        1010536 kB
    MemFree:          178716 kB
    MemAvailable:     249504 kB
    Buffers:               0 kB
    Cached:           100180 kB
    SwapCached:        53608 kB
    Active:           206116 kB
    Inactive:         260760 kB
    Active(anon):     171404 kB
    Inactive(anon):   214548 kB
    Active(file):      34712 kB
    Inactive(file):    46212 kB
    Unevictable:           0 kB
    Mlocked:               0 kB
    SwapTotal:       2097148 kB
    SwapFree:        1767952 kB
    Dirty:                72 kB
    Writeback:             0 kB
    AnonPages:        330732 kB
    Mapped:            30380 kB
    Shmem:             19256 kB
    Slab:             223204 kB
    SReclaimable:     142948 kB
    SUnreclaim:        80256 kB
    KernelStack:        7776 kB
    PageTables:        10864 kB
    NFS_Unstable:          0 kB
    Bounce:                0 kB
    WritebackTmp:          0 kB
    CommitLimit:     2602416 kB
    Committed_AS:    1452468 kB
    VmallocTotal:   34359738367 kB
    VmallocUsed:       59056 kB
    VmallocChunk:   34359621948 kB
    HardwareCorrupted:     0 kB
    AnonHugePages:      4096 kB
    HugePages_Total:       0
    HugePages_Free:        0
    HugePages_Rsvd:        0
    HugePages_Surp:        0
    Hugepagesize:       2048 kB
    DirectMap4k:      178112 kB
    DirectMap2M:      870400 kB
    
    
    
  2. 查看系统当前共享内存页面

    [root@localhost ~]# ipcs -m 
    
    ------------共享内存段--------------  
    键       shmid|      拥有者  权限     字节     nattch     状态      
    0x0112e73f 0          root       600        1000       7                       
    0x0112e742 32769      root       600        1200712    7
    
  3. ls查看文件更多信息

    [root@localhost ~]# ls -l
    总用量 16
    -rw-------.  1 root root 1219 8月   2 08:56 anaconda-ks.cfg
    -rw-r--r--.  1 root root 6140 11月 12 2015 mysql-community-release-el7-5.noarch.rpm
    drwxr-xr-x.  2 root root   21 10月 27 13:07 python3.0
    drwxr-xr-x.  5 root root   64 11月  1 10:23 service
    drwxr-xr-x. 10 root root 4096 10月 19 17:07 web
    
    
  4. 创建文件touch

     [root@localhost ~]# touch test1
     [root@localhost ~]# ls -il test1
     105094843 -rw-r--r--. 1 root  root 0 11月  9 15:53 test1
    

    touch命令创建了你指定的新文件,并将你的用户名作为文件的属主。

  5. 复制文件cp sourece destination

    [root@localhost ~]# cp test1 test2
    [root@localhost ~]# ls -il
    total 0
    105094843 -rw-r--r--.  1 root root    0 11月  9 15:53 test1
    105094844 -rw-r--r--.  1 root root    0 11月  9 16:02 test2
    

    如果目标文件test2存在,那么将覆盖文件,如果新文件中和源文件同名,需要用绝对路径来表示。

  6. 重名文件

    [root@localhost ~]# mv test1 test3
    [root@localhost ~]# ls -il test*
    105094844 -rw-r--r--. 1 root root 0 11月  9 16:02 test2
    105094843 -rw-r--r--. 1 root root 0 11月  9 15:53 test3
    
  7. 删除文件

    [root@localhost ~]# rm test2
    rm:是否删除普通空文件 "test2"?y
    

    注意,提示是否删除文件,bash shell中没有回收站或者垃圾箱,所以一旦删除,就无法找回了。

  8. 创建目录

    [root@localhost ~]# mkdir dir1
    37353231 drwxr-xr-x.  2 root root    6 11月  9 16:17 dir1
    
    
  9. 删除目录

    [root@localhost ~]# rmdir dir1
    

    这个命令删空文件好用。如果需要删除则是rm -r dir;删库到跑路推荐用rm -rf file。

  10. 查看系统磁盘占用

    [root@localhost ~]# df -h
    文件系统        容量  已用  可用 已用% 挂载点
    /dev/sda2        30G  5.4G   25G   18% /
    devtmpfs        486M     0  486M    0% /dev
    tmpfs           494M   20K  494M    1% /dev/shm
    tmpfs           494M   50M  444M   11% /run
    tmpfs           494M     0  494M    0% /sys/fs/cgroup
    /dev/sda5        95G   33M   95G    1% /home
    /dev/sda1       497M  102M  396M   21% /boot
    
  11. 搜索数据

    [root@localhost ~]# grep a test3
    a
    apple
    

    加入函数可以grep -n a test3

  12. 打包和压缩文件

    bunzip2 file1.bz2 解压一个叫做 'file1.bz2'的文件 
    bzip2 file1 压缩一个叫做 'file1' 的文件 
    gunzip file1.gz 解压一个叫做 'file1.gz'的文件 
    gzip file1 压缩一个叫做 'file1'的文件 
    gzip -9 file1 最大程度压缩 
    rar a file1.rar test_file 创建一个叫做 'file1.rar' 的包 
    rar a file1.rar file1 file2 dir1 同时压缩 'file1', 'file2' 以及目录 'dir1' 
    rar x file1.rar 解压rar包 
    unrar x file1.rar 解压rar包 
    tar -cvf archive.tar file1 创建一个非压缩的 tarball 
    tar -cvf archive.tar file1 file2 dir1 创建一个包含了 'file1', 'file2' 以及 'dir1'的档案文件 
    tar -tf archive.tar 显示一个包中的内容 
    tar -xvf archive.tar 释放一个包 
    tar -xvf archive.tar -C /tmp 将压缩包释放到 /tmp目录下 
    tar -cvfj archive.tar.bz2 dir1 创建一个bzip2格式的压缩包 
    tar -xvfj archive.tar.bz2 解压一个bzip2格式的压缩包 
    tar -cvfz archive.tar.gz dir1 创建一个gzip格式的压缩包 
    tar -xvfz archive.tar.gz 解压一个gzip格式的压缩包 
    zip file1.zip file1 创建一个zip格式的压缩包 
    zip -r file1.zip file1 file2 dir1 将几个文件和目录同时压缩成一个zip格式的压缩包 
    unzip file1.zip 解压一个zip格式压缩包 
    
  13. 文件的权限 - 使用 "+" 设置权限,使用 "-" 用于取消

    ls -lh 显示权限 
    ls /tmp | pr -T5 -W$COLUMNS 将终端划分成5栏显示 
    chmod ugo+rwx directory1 设置目录的所有人(u)、群组(g)以及其他人(o)以读(r )、写(w)和执行(x)的权限 
    chmod go-rwx directory1 删除群组(g)与其他人(o)对目录的读写执行权限 
    chown user1 file1 改变一个文件的所有人属性 
    chown -R user1 directory1 改变一个目录的所有人属性并同时改变改目录下所有文件的属性 
    chgrp group1 file1 改变文件的群组 
    chown user1:group1 file1 改变一个文件的所有人和群组属性 
    find / -perm -u+s 罗列一个系统中所有使用了SUID控制的文件 
    chmod u+s /bin/file1 设置一个二进制文件的 SUID 位 - 运行该文件的用户也被赋予和所有者同样的权限 
    chmod u-s /bin/file1 禁用一个二进制文件的 SUID位 
    chmod g+s /home/public 设置一个目录的SGID 位 - 类似SUID ,不过这是针对目录的 
    chmod g-s /home/public 禁用一个目录的 SGID 位 
    chmod o+t /home/public 设置一个文件的 STIKY 位 - 只允许合法所有人删除文件 
    chmod o-t /home/public 禁用一个目录的 STIKY 位 
    
  14. 文本处理

    cat file1 file2 ... | command <> file1_in.txt_or_file1_out.txt general syntax for text manipulation using PIPE, STDIN and STDOUT 
    cat file1 | command( sed, grep, awk, grep, etc...) > result.txt 合并一个文件的详细说明文本,并将简介写入一个新文件中 
    cat file1 | command( sed, grep, awk, grep, etc...) >> result.txt 合并一个文件的详细说明文本,并将简介写入一个已有的文件中 
    grep Aug /var/log/messages 在文件 '/var/log/messages'中查找关键词"Aug" 
    grep ^Aug /var/log/messages 在文件 '/var/log/messages'中查找以"Aug"开始的词汇 
    grep [0-9] /var/log/messages 选择 '/var/log/messages' 文件中所有包含数字的行 
    grep Aug -R /var/log/* 在目录 '/var/log' 及随后的目录中搜索字符串"Aug" 
    sed 's/stringa1/stringa2/g' example.txt 将example.txt文件中的 "string1" 替换成 "string2" 
    sed '/^$/d' example.txt 从example.txt文件中删除所有空白行 
    sed '/ *#/d; /^$/d' example.txt 从example.txt文件中删除所有注释和空白行 
    echo 'esempio' | tr '[:lower:]' '[:upper:]' 合并上下单元格内容 
    sed -e '1d' result.txt 从文件example.txt 中排除第一行 
    sed -n '/stringa1/p' 查看只包含词汇 "string1"的行 
    sed -e 's/ *$//' example.txt 删除每一行最后的空白字符 
    sed -e 's/stringa1//g' example.txt 从文档中只删除词汇 "string1" 并保留剩余全部 
    sed -n '1,5p;5q' example.txt 查看从第一行到第5行内容 
    sed -n '5p;5q' example.txt 查看第5行 
    sed -e 's/00*/0/g' example.txt 用单个零替换多个零 
    cat -n file1 标示文件的行数 
    cat example.txt | awk 'NR%2==1' 删除example.txt文件中的所有偶数行 
    echo a b c | awk '{print $1}' 查看一行第一栏 
    echo a b c | awk '{print $1,$3}' 查看一行的第一和第三栏 
    paste file1 file2 合并两个文件或两栏的内容 
    paste -d '+' file1 file2 合并两个文件或两栏的内容,中间用"+"区分 
    sort file1 file2 排序两个文件的内容 
    sort file1 file2 | uniq 取出两个文件的并集(重复的行只保留一份) 
    sort file1 file2 | uniq -u 删除交集,留下其他的行 
    sort file1 file2 | uniq -d 取出两个文件的交集(只留下同时存在于两个文件中的文件) 
    comm -1 file1 file2 比较两个文件的内容只删除 'file1' 所包含的内容 
    comm -2 file1 file2 比较两个文件的内容只删除 'file2' 所包含的内容 
    comm -3 file1 file2 比较两个文件的内容只删除两个文件共有的部分 
    
  15. 网络

    ifconfig eth0 显示一个以太网卡的配置 
    ifup eth0 启用一个 'eth0' 网络设备 
    ifdown eth0 禁用一个 'eth0' 网络设备 
    ifconfig eth0 192.168.1.1 netmask 255.255.255.0 控制IP地址 
    ifconfig eth0 promisc 设置 'eth0' 成混杂模式以嗅探数据包 (sniffing) 
    dhclient eth0 以dhcp模式启用 'eth0' 
    route -n show routing table 
    route add -net 0/0 gw IP_Gateway configura default gateway 
    route add -net 192.168.0.0 netmask 255.255.0.0 gw 192.168.1.1 configure static route to reach network '192.168.0.0/16' 
    route del 0/0 gw IP_gateway remove static route 
    echo "1" > /proc/sys/net/ipv4/ip_forward activate ip routing 
    hostname show hostname of system 
    host www.example.com lookup hostname to resolve name to ip address and viceversa(1) 
    nslookup www.example.com lookup hostname to resolve name to ip address and viceversa(2) 
    ip link show show link status of all interfaces 
    mii-tool eth0 show link status of 'eth0' 
    ethtool eth0 show statistics of network card 'eth0' 
    netstat -tup show all active network connections and their PID 
    netstat -tupl show all network services listening on the system and their PID 
    tcpdump tcp port 80 show all HTTP traffic 
    iwlist scan show wireless networks 
    iwconfig eth1 show configuration of a wireless network card 
    hostname show hostname 
    host www.example.com lookup hostname to resolve name to ip address and viceversa 
    nslookup www.example.com lookup hostname to resolve name to ip address and viceversa 
    whois www.example.com lookup on Whois database 
    

你可能感兴趣的:(Linux常用命令总结)