天天看点

重要的linux shell命令

下面是我收藏的一些重要的linux shell 命令。

  • 切换到上一次所在的目录
# cd -
  • 获取当前剩余内存
#  cat /proc/meminfo |grep MemAvailable|cut -d: -f2|awk '{print $1}'
  • 获取linux 的 eth0网卡的mac地址

//获取的mac地址:有冒号 小写

# ifconfig | grep eth0| awk 'NR==1{print $5}'

//获取的mac地址:有冒号 大写

# ifconfig | grep eth0| awk 'NR==1{print $5}' | tr a-z A-Z

//获取的mac地址:无冒号 小写

# cat /sys/class/net/eth0/address|awk -F ':' '{print $1$2$3$4$5$6}' 

//获取的mac地址:无冒号 大写

# cat /sys/class/net/eth0/address|awk -F ':' '{print $1$2$3$4$5$6}' | tr a-z A-Z

  • 获取linux的eth0的网卡的ip地址
# ifconfig eth0 | grep 'inet addr:'| cut -d: -f2 | awk '{ print $1}'
  • 查找文件
//在当前目录及子目录下查找文件名为filename的文件
# find . -name "filename"
  • 查找文件里的内容

//在当前目录及子目录下查找有关键字keyword的文件,并打印出关键字所在文件的行号

# grep "keyword" * -nR

//在查找到的文件名为filename的里查找关键字keyword

# find . -name "filename" | xargs grep -in "keyword"

//在当前目录及子目录,不输出错误信息
# find . -type f -exec grep -swnrH 'keyword' {} \; 2>/dev/null
注意:s : 不输出错误信息 
   w :全词匹配
   n:  输出行号
   H:  输出文件名
  • 一行命令统计代码
# find . -iregex ".*\.\(cpp\|h\|java\|sh\)$" | xargs wc -l
  • sudo !!

    !! 代表上一次执行的命令,对于忘记加sudo权限的命令特定有用。

  • 统计文件夹的总大小

    # du -sh file_path   如果统计多个文件加参数-c

    # df -h .; du -sh -- * | sort -hr 列出当前分区的大小,每个文件的大小并排序

  • python 命令开启文件树结构的Web服务器

    # python -m SimpleHTTPServer

  • arp-scan使用ARP协议列出MAC和IP地址的映射关系 

        # sudo arp-scan -l 如果没有先使用sudo apt-get install arp-scan 安装

  • 查看文件的信息

    # stat file_name

   File: ‘color.sh’

  Size: 412        Blocks: 8          IO Block: 4096   regular file

Device: 801h/2049d Inode: 4067531     Links: 1

Access: (0775/-rwxrwxr-x)  Uid: ( 1000/     lyx)   Gid: ( 1000/     lyx)

Access: 2018-03-23 09:21:25.983301373 +0800

Modify: 2017-07-20 08:14:32.071341931 +0800

Change: 2017-07-20 08:14:32.071341931 +0800

 Birth: -

  • 模拟自动打字

    pv命令   

    # echo "你看看你,我打字快不快?" | pv -qL 20

  • screen命令 把前台运行的程序隐藏起来

       1. 对于长时间运行的程序,可以先用screen命令分离出会话,时间到了在恢复会话。    

        # screen ./long-run-program

          Ctrl+A and 然后按"d" 分离会话

         # screen -ls 查看当前的会话

         # screen -r xxxx 恢复会话

        2. Ctrl+A and 大写H,打开会话log文件记录

        再按Ctrl+A and 大写H,关闭会话log记录

  • 列出所有的可以运行的命令

     compgen -ac | grep searchstr

  • 搜索man手册的关键字,对于忘记命令时非常有用

        # aprogpos download 搜索包含download的关键的命

  •  查看Ubuntu中已经安装的程序,用于想要卸载程序却不知道程序名的情况

apt list --installed

想要知道安装的mysql的依赖packages

apt list --installed | grep mysql