Linux下常用命令整理

这篇note记录linux下常用command

Linux系统相关

查看Linux Server系统版本

lsb_release -a

查看当前系统的所有进程:

top

查看系统时间

timedatectl status

设置系统时间,例如America/New_York

sudo timedatectl set-timezone America/New_York

文件相关

显示当前目录下文件大小:

ls -l

显示特定文件的大小:

ls -l | grep 'name'

查看某个目录下的文件总大小:

du -sh path/to/folder

更改文件名:

mv file file'

GPU相关命令

持续显示显卡状况:

watch -n 1 nvidia-smi

查看使用GPU的所有进程:

fuser -v /dev/nvidia*

杀掉GPU的某个进程(把PID替换为对应进程ID):

kill -9 PID

查看Nvidia driver版本

nvidia-smi

查看Nvidia toolkit版本

nvcc -V

在Linux Server上创建新用户

在/home/目录下为用户username创建一个用户目录:

sudo useradd -m -d /home/username username

设置新用户的初始密码:

sudo passwd username

设置-s选项允许指定新用户的登录shell,例如/bin/bash

sudo useradd -s /bin/bash username

sudo chsh -s /bin/bash username (如果用户已存在)

查看用户的user id和groups信息:

id username

查看用户的所有信息:

sudo cat /etc/passwd | grep username

查看某一用户所拥有的权限:

groups username

Git Rebase

Merge multiple commits into one

First, Run the following command to start an interactive rebase for the last 5 commits:

git rebase -i HEAD~5

Change pick to squash (or s) for the last 9 commits, keeping pick for the first commit:

pick 123abc Commit message 1
squash 456def Commit message 2
squash 789ghi Commit message 3
...

Save and close the editor. Another editor window will open, allowing you to edit the commit message for the squashed commit. Modify it as needed and save.

Last, force-push if you've already pushed these commits to a remote branch:

git push origin branch-name --force

UI style

.vimrc

This is the .vimrc configuration for Vim. This controls Vim’s behavior. Put it under ~/home/username/ folder.

" Configuration file for vim
set modelines=0     " CVE-2007-2438

" Normally we use vim-extensions. If you want true vi-compatibility
" remove change the following statements
set nocompatible    " Use Vim defaults instead of 100% vi compatibility
set backspace=2     " more powerful backspacing

" Don't write backup file if vim is being called by "crontab -e"
au BufWrite /private/tmp/crontab.* set nowritebackup nobackup
" Don't write backup file if vim is being called by "chpass"
au BufWrite /private/etc/pw.* set nowritebackup nobackup

let skip_defaults_vim=1
colorscheme default     " 设置颜色主题

syntax on               " 语法高亮

filetype on             " 检测文件的类型

set number              " 显示行号
set cursorline          " 用浅色高亮当前行
"autocmd InsertLeave * se nocul
"autocmd InsertEnter * se cul

set ruler               " 在编辑过程中,在右下角显示光标位置的状态行
set laststatus=2        " 显示状态栏 (默认值为 1, 无法显示状态栏)
set statusline=\ %<%F[%1*%M%*%n%R%H]%=\ %y\ %0(%{&fileformat}\ %{&encoding}\ %c:%l/%L%)\
                        " 设置在状态行显示的信息

set tabstop=4           " Tab键的宽度
set softtabstop=4
set shiftwidth=4        " 统一缩进为4

set autoindent          " vim使用自动对齐,也就是把当前行的对齐格式应用到下一行(自动缩进)
set cindent             " (cindent是特别针对 C语言语法自动缩进)
set smartindent         " 依据上面的对齐格式,智能的选择对齐方式,对于类似C语言编写上有用

set scrolloff=3         " 光标移动到buffer的顶部和底部时保持3行距离

set incsearch           " 输入搜索内容时就显示搜索结果
set hlsearch            " 搜索时高亮显示被找到的文本

set foldmethod=indent   " 设置缩进折叠
set foldlevel=99        " 设置折叠层数
nnoremap <space> @=((foldclosed(line('.')) < 0) ? 'zc' : 'zo')<CR>
                        " 用空格键来开关折叠

" 自动跳转到上次退出的位置
if has("autocmd")
    au BufReadPost * if line("'\"") > 1 && line("'\"") <= line("$") | exe "normal! g'\"" | endif
endif

.zprofile

.zprofile is the login shell configuration for Zsh. It is typically used for environment variables that should be set once per login. Example setup:

#export LS_OPTIONS='--color=auto'       # 如果没有指定,则自动选择颜色 
alias ls='ls -G'
export LSCOLORS=GxExCxDxcxegedabagaced   # 指定颜色
# Setting PATH for Python 3.11
# The original version is saved in .zprofile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/3.11/bin:/opt/homebrew/bin:${PATH}"
export PATH

.zshrc

This is the interactive shell configuration for Zsh. You can set it to configure prompt settings, shell options etc.

# Enable syntax highlighting in Zsh
autoload -Uz syntax-highlighting
syntax-highlighting on

# Set the prompt colors (example colors)
PROMPT='%F{magenta}%n%f@%F{green}%m%f:%F{cyan}%~%f %# '
RPROMPT='%F{yellow}%T%f'
# >>> conda initialize  >>>                                                                           
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$('/Users/yuxin/miniconda3/bin/conda' 'shell.zsh' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
    eval "$__conda_setup"
else
    if [ -f "/Users/yuxin/miniconda3/etc/profile.d/conda.sh" ]; then
        . "/Users/yuxin/miniconda3/etc/profile.d/conda.sh"
    else
        export PATH="/Users/yuxin/miniconda3/bin:$PATH"
    fi
fi
unset __conda_setup
# <<< conda initialize <<<

eval "$(rbenv init - zsh)"