Linux Command

命令参数

man <command>		# 查看详细介绍
<command> --help	# 通常可以查看命令的参数信息

软件管理

which python            # 查看当前使用的 python 位置

用户/权限

查看用户组

groups <username>

添加用户到 sudoer

  • 方法一:
sudo /usr/sbin/usermod -a -G sudo <username>
  • 方法二:
# 修改文件, 在 root 下一行写上用户名, 格式和 root 那一行相同
vi /etc/sudoers

切换用户

su -<username>

修改密码

passwd

列出所有已连接的SSH会话

who [/w]
who am i
who -a

关闭指定的 SSH 连接

pkill -kill -t pts/5

Shell 使用

I/O 重定向

  • 重定向 stderrfile
<command> 2>file
<command> 2>>file   # 追加
  • 丢弃重定向结果 /dev/null
<command> > /dev/null

管道 pipe

ps -aux | grep <keyword>

后台

<CTRL + Z>          # 放在后台运行, 且处于暂停状态
fg                  # 将后台程序放在前台运行
bg [%job_id]        # 将后台暂停的程序继续运行

清屏

<CTRL + L>
clear

过滤

cat <file_txt> | grep <keyword> | grep -v <not_keyword>
grep <keyword>      # 选择包含 keyword
grep -v <keyword>   # 反选
grep -i             # 忽略大小写

Terminal 工具

Screen

  • 基本命令
screen -ls              # 查看
screen -S <name>        # 创建
screen -r <id.name>     # 连接
  • screen 窗口中的常用操作
Ctrl-a [                # 复制模式, 可以使用鼠标滑轮
Ctrl-a d                # 退出 screen 窗口, 还可以再连接
Ctrl-d                  # 结束窗口

系统资源

内存 Memory

free -h
cat /proc/meminfo |grep MemTotal

磁盘 Disk

du -h -d 1	# (disk usage) 查看当前目录下文件占用的空间大小
df -hl		# (disk free) 查看整个系统磁盘占用情况
sudo du -sh /home/*          # (user) 查看各用户的磁盘占用情况

显卡 GPU

nvidia-smi
watch -n 0.5 nvidia-smi                     # 每 0.5 秒刷新显示

选择可见的显卡

CUDA_VISIBLE_DEVICES=1 python script.py
CUDA_VISIBLE_DEVICES=0,1 python script.py   # 选择可见的 GPU

查看 CUDA 版本

nvcc --version
cat /usr/local/cuda/version.txt
ll /usr/local/ | grep cuda  # 查看软链接连向的版本

切换 CUDA 版本

  • ~/bashrc 中设置固定的版本
export CUDA_DIR=/usr/local/cuda-10.0
  • 修改 /usr/localcuda 软连接
# 1. ~/.bashrc
export CUDA_DIR=/usr/local/cuda
# 2. setting cuda link
sudo ln -s /usr/local/cuda-10.0/ /usr/local/cuda/

处理器 CPU

cat /proc/cpuinfo|grep name|cut -f2 -d:|uniq -c

网络 Network

ifconfig

进程管理

当前进程查看

top

搜索运行中的进程信息

ps -aux | grep 'keyword'

结束/清除/中止/杀进程

  • 方法一: 根据 pid, 进程 id, 使用 kill
ps -aux | grep 'keyword'
kill <pid>
  • 方法二: 根据 job id, 使用 kill (只能查看当前 terminal 上的进程)
jobs
kill %<job_id>
  • 方法三: 根据用户名, 使用 pkill, 结束指定用户下的所有进程
pkill -u <username>

暂停状态进程 kill, ps -aux 可以看到进程状态, Tl 状态不执行但是可能会占用资源

kill -9 <PID>		# -9 参数适用于 Tl (暂停状态) 的进程

文件管理

递归创建文件夹

mkdir -p <dirA/.../dirX>

远程复制 scp

  • 基本使用
scp <username>@<hostname/ip>:<path> <path>
scp -r -P <port> <username>@<hostname/ip>:<remote_path> <local_path>
  • 复制文件
scp crayon@MyServer:/mnt/disk/data/* .
  • 复制文件夹
scp crayon@MyServer:/mnt/disk/data .

创建软链接 ln -s

# 让 <b> 相当于 <a>, 其中 <a> 是真正存储数据的文件
ln -s <a> <b>
# 有时会报错, 说太多层级等等, 使用绝对路径就好了
ln -s mntdiskmyfile ~/workspace

常用工具

ubuntu 软件安装

apt-get install <app>
apt-get remove <app>

压缩/解压

  • zip
zip -r <name.zip> <filedir>
unzip <filename>
unzip </path/to/file.zip> -d <temp_for_zip_extract>
  • tar
tar zxvf XXX.gz				# 解压
tar zcvf XXX.gz <DirName>	# 压缩/打包

日志内容查看

tail -n 100 -f	# 末尾 100 行, 动态显示更新

字符/文本文件信息统计

wc -l			# 统计行数
cat <filename> | grep 'xxx' | wc -l

文件校验 MD5

md5sum S3DIS.zip > S3DIS.zip.md5  	# 生成校验结果
md5sum -c S3DIS.zip.md5				# 使用校验结果校验

Jupyter Notebook & Jupyter Lab

  • screen 窗口中开启一个 jupyter-lab 用于跑程序, 可以防止程序中断.
jupyter lab --no-browser --port 2333

系统使用

shell environment

功能

  • 全局环境设定: /etc/profile, /etc/bashrc
  • 用户环境设定: ~/.profile~/.bashrc

过程

  1. 首先执行的是全局环境变量设定脚本 /etc/profile
    • 根据其内容执行额外的设定脚本 /etc/profile.d, /etc/inputrc
  2. 按顺序执行登录用户的环境 ~/.bash_profile, ~/.bash_login, ~/.profile
    • 最后读取 ~/.bashrc

使用

  • ~/.profile 可以设定用户专有路径, 环境变量等. 它只能登入的时候执行一次.
  • ~/.bashrc 是用户专有设定文档, 可以设定路径, 命令别名, 每次 shell script 都会执行.

基础

ls, cd, pwd, cd ~, cd -         # 路径
mv, rm, rm -rf, cp              # 文件
mkdir -p, rmdir -p              # 文件夹
top, free, ps, jobs, kill       # 资源&进程
su, sudo                        # 用户|权限
cat, vi                         # 文本内容
source ~/.bashrc                # 环境变量
nvidia-smi                      # GPU
grep, grep -v, grep -i          # 过滤
tail, tail -n, tail -f          # 文本查看, 动态查看

References

原文地址:https://www.cnblogs.com/crayonsea/p/14507372.html