1. 杂项
1.1 管理员用户
每次打开ubuntu环境,执行以下命令并输入root密码进入root用户
su
如果root用户把什么环境搞坏了修不好了,大不了重装一次(至少我入职至今从没搞坏过环境
1.2 查看空间
选项 [ -h ] 以 K, M, G 为单位显示
df 查看磁盘空间信息
df -h
du 查看文件(夹)的大小
du -h
du --help
du -h manifest
Usage: du [OPTION]... [FILE]...
or: du [OPTION]... --files0-from=F
Summarize disk usage of the set of FILEs, recursively for directories.
Mandatory arguments to long options are mandatory for short options too.
-0, --null end each output line with NUL, not newline
-a, --all write counts for all files, not just directories
--apparent-size print apparent sizes, rather than disk usage; although
the apparent size is usually smaller, it may be
larger due to holes in ('sparse') files, internal
fragmentation, indirect blocks, and the like
-B, --block-size=SIZE scale sizes by SIZE before printing them; e.g.,
'-BM' prints sizes in units of 1,048,576 bytes;
see SIZE format below
-b, --bytes equivalent to '--apparent-size --block-size=1'
-c, --total produce a grand total
-D, --dereference-args dereference only symlinks that are listed on the
command line
-d, --max-depth=N print the total for a directory (or file, with --all)
only if it is N or fewer levels below the command
line argument; --max-depth=0 is the same as
--summarize
--files0-from=F summarize disk usage of the
NUL-terminated file names specified in file F;
if F is -, then read names from standard input
-H equivalent to --dereference-args (-D)
-h, --human-readable print sizes in human readable format (e.g., 1K 234M 2G)
--inodes list inode usage information instead of block usage
-k like --block-size=1K
-L, --dereference dereference all symbolic links
-l, --count-links count sizes many times if hard linked
-m like --block-size=1M
-P, --no-dereference don't follow any symbolic links (this is the default)
-S, --separate-dirs for directories do not include size of subdirectories
--si like -h, but use powers of 1000 not 1024
-s, --summarize display only a total for each argument
-t, --threshold=SIZE exclude entries smaller than SIZE if positive,
or entries greater than SIZE if negative
--time show time of the last modification of any file in the
directory, or any of its subdirectories
--time=WORD show time as WORD instead of modification time:
atime, access, use, ctime or status
--time-style=STYLE show times using STYLE, which can be:
full-iso, long-iso, iso, or +FORMAT;
FORMAT is interpreted like in 'date'
-X, --exclude-from=FILE exclude files that match any pattern in FILE
--exclude=PATTERN exclude files that match PATTERN
-x, --one-file-system skip directories on different file systems
--help display this help and exit
--version output version information and exit
Display values are in units of the first available SIZE from --block-size,
and the DU_BLOCK_SIZE, BLOCK_SIZE and BLOCKSIZE environment variables.
Otherwise, units default to 1024 bytes (or 512 if POSIXLY_CORRECT is set).
The SIZE argument is an integer and optional unit (example: 10K is 10*1024).
Units are K,M,G,T,P,E,Z,Y (powers of 1024) or KB,MB,... (powers of 1000).
GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
Report du translation bugs to <https://translationproject.org/team/>
Full documentation at: <https://www.gnu.org/software/coreutils/du>
or available locally via: info '(coreutils) du invocation'
翻译参考:https://www.runoob.com/linux/linux-comm-du.html
1.3 清屏
clear 刷新屏幕,可以向上滚动屏幕看到之前操作信息
reset 清空窗口,不能向上滚动屏幕看到之前操作信息
按住回车(推荐),方便辨别各次独立操作的输出信息,避免混淆看漏
1.4 操作历史
history
1.5 更改权限
选项 [ -R ] 递归修改子文件夹
chmod -R go+rwx [路径]
chmod -R go+rwx /home/L2/foundation/graphic
本地IDE无法保存文件时可在ubuntu环境内执行此命令以解决问题,而且git分析差异及提交时不会显示
参考:https://www.runoob.com/linux/linux-comm-chmod.html
1.6 统计代码行数
wc -l 查看行数
`find .` 筛选当前文件夹内的内容
-name '*.cpp' 筛选.cpp
一个示例(文件夹内所有文件):
wc -l `find .`
一个示例(文件夹内.cpp和.h):
wc -l `find . -name '*.cpp'` `find . -name '*.h'`
2. 文件管理
2.1 切换目录 cd
cd 路径 | 切换至此路径 |
---|---|
cd . | 切换至当前路径(不变 |
cd .. | 切换至上一层路径 |
cd /home
cd teach
cd dir2/dir21
cd ..
cd ../dir1
cd ../..
cd ~
cd /
2.2 列举目录信息 ls
显示文件或目录信息
选项 [ -a ] 显示包括隐藏文件的所有文件
选项 [ -l ] 显示文件详细信息
ls
ls -a
ls /home
2.3 创建文件 touch
可在一条命令中创建多文件
touch file
touch file1 file2 file3
2.4 创建文件夹 mkdir
选项 [ -p ] 递归创建文件夹
mkdir dir1
mkdir -p dir2/dir21
mkdir dir2/dir22
2.5 复制 cp
选项 [ -r ] 复制文件夹
如果目标文件已存在,会被覆盖,慎重
亦可不指定目标文件名,以同名文件(夹)形式复制到指定目录下
cp file file1
cp -r dir2 dir3
cp -r dir4/
此命令只会自动创建一层文件夹,所以需提前创建好目标路径文件夹
2.6 移动 mv
可移动文件/文件夹,可以用此命令实现改名
文件:如果目标文件存在,会用源文件覆盖目标文件,请谨慎
文件夹:如果目标文件夹存在,会把源文件夹移动到目标文件夹内,而非覆盖,可按需提前删除目标文件夹
mv file file4
mv dir1 dir6
2.7 删除 rm
选项 [ -r ] 删除文件夹
选项 [ -f ] 忽略不存在的文件(夹),不提示
rm file11
rm -r dir4
rm -f test
rm -rf xxx
2.8 链接 ln
选项 [ -r ] 软链接,类似C++等语言的指针,修改源文件夹内文件时,目标处亦可读取
不加 [ -r ] 硬链接,等同复制
ln -s teach peach
ls -l 可以查看软链接信息
2.9 显示内容 cat
选项 [ -n ] 查看行号
空文件无输出
cat file1
cat -n file1
3. vim
参考:https://www.runoob.com/linux/linux-vim.html
用vim编辑器打开文件的方法:
vim [文件]
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 ]
3.3 按行删除内容
dd | 删除光标所在一整行 |
---|---|
ndd | 删除光标所在行开始的n行,n为数字 |
d1G | 删除开始到光标所在行的所有行 |
dG | 删除光标所在行到结尾的所有行 |
3.4 复制与粘贴
一般右键就可以了,但是粘贴时记得先进入输入模式
“命令模式输入 [ i ] 切换输入模式”
4. git
4.1 help
git --help
git help git
usage: git [--version] [--help] [-C <path>] [-c <name>=<value>]
[--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
[-p | --paginate | -P | --no-pager] [--no-replace-objects] [--bare]
[--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
<command> [<args>]
These are common Git commands used in various situations:
start a working area (see also: git help tutorial)
clone Clone a repository into a new directory
init Create an empty Git repository or reinitialize an existing one
work on the current change (see also: git help everyday)
add Add file contents to the index
mv Move or rename a file, a directory, or a symlink
restore Restore working tree files
rm Remove files from the working tree and from the index
sparse-checkout Initialize and modify the sparse-checkout
examine the history and state (see also: git help revisions)
bisect Use binary search to find the commit that introduced a bug
diff Show changes between commits, commit and working tree, etc
grep Print lines matching a pattern
log Show commit logs
show Show various types of objects
status Show the working tree status
grow, mark and tweak your common history
branch List, create, or delete branches
commit Record changes to the repository
merge Join two or more development histories together
rebase Reapply commits on top of another base tip
reset Reset current HEAD to the specified state
switch Switch branches
tag Create, list, delete or verify a tag object signed with GPG
collaborate (see also: git help workflows)
fetch Download objects and refs from another repository
pull Fetch from and integrate with another repository or a local branch
push Update remote refs along with associated objects
'git help -a' and 'git help -g' list available subcommands and some
concept guides. See 'git help <command>' or 'git help <concept>'
to read about a specific subcommand or concept.
See 'git help git' for an overview of the system.
4.2 文件添加暂存区
git add [文件]