1. 杂项

1.1 管理员用户

每次打开ubuntu环境,执行以下命令并输入root密码进入root用户

  1. su

如果root用户把什么环境搞坏了修不好了,大不了重装一次(至少我入职至今从没搞坏过环境

1.2 查看空间

选项 [ -h ] 以 K, M, G 为单位显示

  1. df 查看磁盘空间信息
  2. df -h
  3. du 查看文件(夹)的大小
  4. du -h
  5. du --help
  6. du -h manifest

image.png

  1. Usage: du [OPTION]... [FILE]...
  2. or: du [OPTION]... --files0-from=F
  3. Summarize disk usage of the set of FILEs, recursively for directories.
  4. Mandatory arguments to long options are mandatory for short options too.
  5. -0, --null end each output line with NUL, not newline
  6. -a, --all write counts for all files, not just directories
  7. --apparent-size print apparent sizes, rather than disk usage; although
  8. the apparent size is usually smaller, it may be
  9. larger due to holes in ('sparse') files, internal
  10. fragmentation, indirect blocks, and the like
  11. -B, --block-size=SIZE scale sizes by SIZE before printing them; e.g.,
  12. '-BM' prints sizes in units of 1,048,576 bytes;
  13. see SIZE format below
  14. -b, --bytes equivalent to '--apparent-size --block-size=1'
  15. -c, --total produce a grand total
  16. -D, --dereference-args dereference only symlinks that are listed on the
  17. command line
  18. -d, --max-depth=N print the total for a directory (or file, with --all)
  19. only if it is N or fewer levels below the command
  20. line argument; --max-depth=0 is the same as
  21. --summarize
  22. --files0-from=F summarize disk usage of the
  23. NUL-terminated file names specified in file F;
  24. if F is -, then read names from standard input
  25. -H equivalent to --dereference-args (-D)
  26. -h, --human-readable print sizes in human readable format (e.g., 1K 234M 2G)
  27. --inodes list inode usage information instead of block usage
  28. -k like --block-size=1K
  29. -L, --dereference dereference all symbolic links
  30. -l, --count-links count sizes many times if hard linked
  31. -m like --block-size=1M
  32. -P, --no-dereference don't follow any symbolic links (this is the default)
  33. -S, --separate-dirs for directories do not include size of subdirectories
  34. --si like -h, but use powers of 1000 not 1024
  35. -s, --summarize display only a total for each argument
  36. -t, --threshold=SIZE exclude entries smaller than SIZE if positive,
  37. or entries greater than SIZE if negative
  38. --time show time of the last modification of any file in the
  39. directory, or any of its subdirectories
  40. --time=WORD show time as WORD instead of modification time:
  41. atime, access, use, ctime or status
  42. --time-style=STYLE show times using STYLE, which can be:
  43. full-iso, long-iso, iso, or +FORMAT;
  44. FORMAT is interpreted like in 'date'
  45. -X, --exclude-from=FILE exclude files that match any pattern in FILE
  46. --exclude=PATTERN exclude files that match PATTERN
  47. -x, --one-file-system skip directories on different file systems
  48. --help display this help and exit
  49. --version output version information and exit
  50. Display values are in units of the first available SIZE from --block-size,
  51. and the DU_BLOCK_SIZE, BLOCK_SIZE and BLOCKSIZE environment variables.
  52. Otherwise, units default to 1024 bytes (or 512 if POSIXLY_CORRECT is set).
  53. The SIZE argument is an integer and optional unit (example: 10K is 10*1024).
  54. Units are K,M,G,T,P,E,Z,Y (powers of 1024) or KB,MB,... (powers of 1000).
  55. GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
  56. Report du translation bugs to <https://translationproject.org/team/>
  57. Full documentation at: <https://www.gnu.org/software/coreutils/du>
  58. or available locally via: info '(coreutils) du invocation'

翻译参考:https://www.runoob.com/linux/linux-comm-du.html

1.3 清屏

  1. clear 刷新屏幕,可以向上滚动屏幕看到之前操作信息
  2. reset 清空窗口,不能向上滚动屏幕看到之前操作信息
  3. 按住回车(推荐),方便辨别各次独立操作的输出信息,避免混淆看漏

1.4 操作历史

  1. history

1.5 更改权限

选项 [ -R ] 递归修改子文件夹

  1. chmod -R go+rwx [路径]
  2. chmod -R go+rwx /home/L2/foundation/graphic

本地IDE无法保存文件时可在ubuntu环境内执行此命令以解决问题,而且git分析差异及提交时不会显示

参考:https://www.runoob.com/linux/linux-comm-chmod.html

1.6 统计代码行数

  1. wc -l 查看行数
  2. `find .` 筛选当前文件夹内的内容
  3. -name '*.cpp' 筛选.cpp
  4. 一个示例(文件夹内所有文件):
  5. wc -l `find .`
  6. 一个示例(文件夹内.cpp和.h):
  7. wc -l `find . -name '*.cpp'` `find . -name '*.h'`

2. 文件管理

2.1 切换目录 cd

cd 路径 切换至此路径
cd . 切换至当前路径(不变
cd .. 切换至上一层路径
  1. cd /home
  2. cd teach
  3. cd dir2/dir21
  4. cd ..
  5. cd ../dir1
  6. cd ../..
  7. cd ~
  8. cd /

image.png

2.2 列举目录信息 ls

显示文件或目录信息
选项 [ -a ] 显示包括隐藏文件的所有文件
选项 [ -l ] 显示文件详细信息

  1. ls
  2. ls -a
  3. ls /home

image.png

2.3 创建文件 touch

可在一条命令中创建多文件

  1. touch file
  2. touch file1 file2 file3

image.png

2.4 创建文件夹 mkdir

选项 [ -p ] 递归创建文件夹

  1. mkdir dir1
  2. mkdir -p dir2/dir21
  3. mkdir dir2/dir22

image.png

2.5 复制 cp

选项 [ -r ] 复制文件夹
如果目标文件已存在,会被覆盖,慎重
亦可不指定目标文件名,以同名文件(夹)形式复制到指定目录下

  1. cp file file1
  2. cp -r dir2 dir3
  3. cp -r dir4/

image.png

此命令只会自动创建一层文件夹,所以需提前创建好目标路径文件夹

image.png

2.6 移动 mv

可移动文件/文件夹,可以用此命令实现改名
文件:如果目标文件存在,会用源文件覆盖目标文件,请谨慎
文件夹:如果目标文件夹存在,会把源文件夹移动到目标文件夹内,而非覆盖,可按需提前删除目标文件夹

  1. mv file file4
  2. mv dir1 dir6

image.png

2.7 删除 rm

选项 [ -r ] 删除文件夹
选项 [ -f ] 忽略不存在的文件(夹),不提示

  1. rm file11
  2. rm -r dir4
  3. rm -f test
  4. rm -rf xxx

image.png

2.8 链接 ln

选项 [ -r ] 软链接,类似C++等语言的指针,修改源文件夹内文件时,目标处亦可读取
不加 [ -r ] 硬链接,等同复制

  1. ln -s teach peach
  2. ls -l 可以查看软链接信息

image.png

2.9 显示内容 cat

选项 [ -n ] 查看行号
空文件无输出

  1. cat file1
  2. cat -n file1

image.png

3. vim

参考:https://www.runoob.com/linux/linux-vim.html

用vim编辑器打开文件的方法:

  1. vim [文件]
  2. vim L2/.repo/manifest.xml

3.1 命令模式

输入模式按 [ Esc ] 切换命令模式
命令模式输入 [ i ] 切换输入模式
进入输入模式后才可以修改内容

3.1.1 底线命令模式

命令模式输入 [ : ] 切换底线命令模式
执行命令后(输入命令后按 [ Enter ])自动退回命令模式

:q 退出
:q! 不保存,强制退出
:w 保存
:wq 保存并退出

3.2 搜索

/word 光标以下开始搜索
?word 光标以上开始搜索
n 下一个
N 上一个

记得输入搜索命令后按 [ Enter ] 后再 [ n ] 或 [ N ]
image.png

3.3 按行删除内容

dd 删除光标所在一整行
ndd 删除光标所在行开始的n行,n为数字
d1G 删除开始到光标所在行的所有行
dG 删除光标所在行到结尾的所有行

3.4 复制与粘贴

一般右键就可以了,但是粘贴时记得先进入输入模式
“命令模式输入 [ i ] 切换输入模式”

4. git

4.1 help

  1. git --help
  2. git help git
  1. usage: git [--version] [--help] [-C <path>] [-c <name>=<value>]
  2. [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
  3. [-p | --paginate | -P | --no-pager] [--no-replace-objects] [--bare]
  4. [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
  5. <command> [<args>]
  6. These are common Git commands used in various situations:
  7. start a working area (see also: git help tutorial)
  8. clone Clone a repository into a new directory
  9. init Create an empty Git repository or reinitialize an existing one
  10. work on the current change (see also: git help everyday)
  11. add Add file contents to the index
  12. mv Move or rename a file, a directory, or a symlink
  13. restore Restore working tree files
  14. rm Remove files from the working tree and from the index
  15. sparse-checkout Initialize and modify the sparse-checkout
  16. examine the history and state (see also: git help revisions)
  17. bisect Use binary search to find the commit that introduced a bug
  18. diff Show changes between commits, commit and working tree, etc
  19. grep Print lines matching a pattern
  20. log Show commit logs
  21. show Show various types of objects
  22. status Show the working tree status
  23. grow, mark and tweak your common history
  24. branch List, create, or delete branches
  25. commit Record changes to the repository
  26. merge Join two or more development histories together
  27. rebase Reapply commits on top of another base tip
  28. reset Reset current HEAD to the specified state
  29. switch Switch branches
  30. tag Create, list, delete or verify a tag object signed with GPG
  31. collaborate (see also: git help workflows)
  32. fetch Download objects and refs from another repository
  33. pull Fetch from and integrate with another repository or a local branch
  34. push Update remote refs along with associated objects
  35. 'git help -a' and 'git help -g' list available subcommands and some
  36. concept guides. See 'git help <command>' or 'git help <concept>'
  37. to read about a specific subcommand or concept.
  38. See 'git help git' for an overview of the system.

4.2 文件添加暂存区

  1. git add [文件]