This passage is about getting started with GNU/Linux by reading The Linux Command Line. Chinese version is available here. It’s an excellent introductory book of Linux. No more talk, let’s just get it started. If you want to see more details, read this book, because I will ignore many details of a commmand like “How many options do this command has ?” or “how to use every option of a command ?”.
我的环境: Unbuntu 18.04LTS virtual machine.
还是不用我那四级水平的英语了。这不是文档,只是笔记。详细信息请使用man
或者查看文档。
Part 1 – Learning The Shell
shell
首先什么是Shell?Shell翻译为壳,是对应于核的概念,shell是CLI(Command Line Interface,与之对应的为GUI)的命令解析工具,类似于windows下的cmd。Shell读取用户从键盘的输入,将其解释为对应的命令,然后执行对应的程序。在Linux中,常用的Shell有Bash,这里使用的即是Bash。
当使用GUI时,通常我们需要使用终端模拟器来和Shell交互。不同GUI叫法不太一样,或Terminal(终端)或Console(控制台)或其他。
Terminal Emulators 终端模拟器
tiko@killing-boat:~$ #普通用户
root@killing-boat:/home/tiko# #superuser
键入每一条命令前的Shell提示符:username@machinename : current-working-directory $
last character: #
means superuser/administrator.
最简单的几条命令:
date
- display date
cal
- display the calender
df
- to see the current amount of free space on your disk drives
free
- display the amount of free memory
exit
- closing the terminal emulator window
The Console Behind The Curtain:
Pressing Ctrl-Alt-F1 through Ctrl-Alt-F6 to switch.
My machine : Ctrl-Alt-F1/F2 are GUIs, Ctrl-Alt-F3 to F6 are CLIs.
Command
Shell命令基本格式:
command [选项options] [参数arguments]
有的选项不需要参数像ls -l
,有的选项参数可选,像ls <file>
不给参数则使用默认参数.
。有的命令可以带多个参数。
选项分长选项与短选项,长选项如--help
以--
开头,短选项如-l
以-
开头。多个短选项可合并如ls -alh
。
每个命令具体选项信息、参考文档可以通过以下帮助命令得到:
command --help # 获取命令帮助的选项,较为简略
help command # Shell内部命令帮助,仅对内部命令有效,比如cd
man command # 命令手册manual,常用
info command # 比man更为详细
Navigation
使用下面的命令在系统中游走:
pwd
- Print name of current working directory
cd
- Change directory
cd .. # 上一级
cd - # 上一次工作目录
cd ./subdir # 子目录
cd . # 当前目录
Linux中使用目录(directory)而不使用文件夹(folder)的概念。
文件与目录的路径可以是绝对路径(like /root/code/me
)或者相对路径(like ../mydir/mydir/file
./subdir/file
)。
ls
- List directory contents
ls dir-or-file-path # default is .
常用选项:
ls -a # 所有文件与目录,包括.开头的隐藏文件
ls -l # 长格式
ls -h # 文件大小以人可读的方式列出
ls -d # 列出目录本身,而不是其中的内容
ls -F # 在文件结尾附加文件类型
# * 可执行文件
# / 目录
# @ 链接文件
# | 管道文件
# = 套接字
# > 进程间通讯设备
上面的-a
-l
等称为选项(options),而后跟参数(parameter)。
ls
命令所有选项与详细信息可使用下列命令得到:
ls --help
man ls
info ls
根目录/
下每一个目录都有其作用,可参见这里。
/ 根目录,万物起源
/bin 二进制程序
/boot Linux内核,RAM系统映像,启动加载程序
/dev 设备节点,一切皆是文件
/etc 系统层面配置文件,shell脚本
/home 普通用户家目录上一级
/lib 系统核心程序库文件
/media 可移除设备自动挂载点
/mnt 同上,早期linux系统中
/opt 安装可选软件
/proc 虚拟文件系统,内核相关
/root root用户家目录
/sbin 系统二进制文件,为root用户保留
/tmp 存储临时文件,重启清空
/usr 包含用户所需要所有程序和文件
/var 动态文件存储地址
我的根目录:
tiko@killing-boat:~$ ls -F /
bin/ cdrom/ etc/ initrd.img@ lib/ lost+found/ mnt/ proc/ run/ snap/ swapfile tmp/ var/ vmlinuz.old@
boot/ dev/ home/ initrd.img.old@ lib64/ media/ opt/ root/ sbin/ srv/ sys/ usr/ vmlinuz@
file
– Determine file type 显示文件类型
less
– View file contents 查看文本文件内容
cat
– 亦是查看文本内容,与less
有区别
tree
– 以树形结构打印目录文件(需要先安装:sudo apt-get install tree
)
Manipulating Files And Directories
cp
– Copy files and directories
使用-i
选项来提示是否要覆盖已存在文件,若没有则直接覆盖。
mv
– Move/rename files and directories
移动到不同目录即移动,移动到当前目录即重命名。
mkdir
– Create directories
mkdir dir1 dir2 ../dir3
mkdir -p ./mydir1/mydir2 # 递归创建
rm
– Remove files and directories 删除文件与目录
rm -rf /* # 人们常开的玩笑 -f 强制执行,-r递归删除目录与文件
其中*
是通配符,详细信息查看这里。
ln
– Create hard and symbolic links 创建硬链接或者符号链接
ln fun fun-hard # to create a hard link, point to a block of disk
ln -s fun fun-soft # to create a symbolic link, point to the path of a file
Working With Commands
type
– Indicate how a command name is interpreted 表明命令类型,分类:alias
别名、bulitin
Shell内建命令、file
磁盘可执行文件、function
Shell函数、keyword
关键字/Shell保留字。
tiko@killing-boat:~$ type ls
ls 是 `ls --color=auto' 的别名
tiko@killing-boat:~$ type cd
cd 是 shell 内建
tiko@killing-boat:~$ type cp
cp 是 /bin/cp
which
– Display which executable program will be executed
tiko@killing-boat:~$ which gcc
/usr/bin/gcc
help
– Get help for shell builtins
man
– Display a command’s manual page 使用man
查手册
apropos
– Display a list of appropriate commands
info
– Display a command’s info entry
whatis
– Display a very brief description of a command
tiko@killing-boat:~$ whatis ls
ls (1) - list directory contents
alias
– Create an alias for a comma