小技巧

日常使用

  • 终端指定编辑器打开文件/文件夹(vscode为例)

    shell
    1
    
    open /opt/homebrew/etc/nginx -a 'Visual Studio Code'

    提示

    针对vscode更便捷的方式: 打开vscode命令cmd+shift+p,输入shell,选择Install 'code' command in PATH,就可以在path中自动安装code命令,之后就可以直接用code命令打开文件/文件夹

    shell
    1
    
    code /opt/homebrew/etc/nginx
  • 查看进程

    shell
    1
    
    ps -ef | grep nginx
  • 查看端口占用

    shell
    1
    2
    3
    4
    5
    6
    7
    8
    9
    
    lsof -i:8080:查看8080端口占用
    lsof abc.txt:显示开启文件abc.txt的进程
    lsof -c abc:显示abc进程现在打开的文件
    lsof -c -p 1234:列出进程号为1234的进程所打开的文件
    lsof -g gid:显示归属gid的进程情况
    lsof +d /usr/local/:显示目录下被进程开启的文件
    lsof +D /usr/local/:同上,但是会搜索目录下的目录,时间较长
    lsof -d 4:显示使用fd为4的进程
    lsof -i -U:显示所有打开的端口和UNIX domain文件

    netstat -tunlp 用于显示 tcp,udp 的端口和进程等相关情况。

    netstat 查看端口占用语法格式:

    shell
    1
    
    netstat -tunlp | grep 端口号
    • -t (tcp) 仅显示tcp相关选项
    • -u (udp)仅显示udp相关选项
    • -n 拒绝显示别名,能显示数字的全部转化为数字l
    • -l 仅列出在Listen(监听)的服务状态
    • -p 显示建立相关链接的程序名