Scott's Blog

学则不固, 知则不惑

0%

Linux-基本命令与目录结构

这篇文章涵盖了Linux中关于基本命令、环境变量、特殊符号、Shell 编辑工具、帮助文档查询、标准输入输出、符号链接、解压缩、超级用户以及目录的结构介绍等内容。

关于 Unix 和 Linux

Linux is a Unix flavor at heart. You’ll see the word Unix in this chapter more than Linux because you can take what you learn straight over to BSD and other Unix-flavored systems.

Bourne Shell: /bin/sh

A shell is a program that runs commands.

现在有很多的 Shell,但大部分的都是起源于 Bourne shell (/bin/sh),这是贝尔实验室早期开发给 Unix 用的。

Linux 也有一个优化过的 Shell 叫 bash,全称是 bourne again shell.

基本命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
ls
cp
mv
rm
touch
mkdir
echo

grep
less
more
pwd
diff 对比两个文件不同
file 查看一个文件的格式
find
head
tail
sort
passwd 改密码

环境和 Shell 变量

Shell 环境下可以保存一些变量:

如果希望当 shell 启动的时候就将一些变量存起来,则可通过环境变量。

The main difference between environment and shell variables is that the operating system passes all of your shell’s environment variables to programs that the shell runs, whereas shell variables cannot be accessed in the commands that you run.

命令地址

Path 是一个特殊的环境变量,我们输入的指令 shell 会去这些路径中寻找程序,例如这是我的电脑上 Path 的值:

这个地址中的可以编辑,添加。

特殊符号

符号 符号名 含义
* star, asterisk Regular expression, glob character
. dot Current directory, file/hostname delimiter
! bang Negation, command history
| pipe Command pipes
/ (forward) slash Directory delimiter, search command
\ backslash Literals, macros (never directories)
$ dollar Variables, end of line
' tick, (single) quote Literal strings
` backtick, backquote Command substitution
" double quote Semi-literal strings
^ caret Negation, beginning of line
~ tilde, squiggle Negation, directory shortcut
# hash, sharp, pound Comments, preprocessor, substitutions
[ ] (square) brackets Ranges
{ } braces, (curly) brackets Statement blocks, ranges
_ underscore, under Cheap substitute for a space used when spaces aren’t wanted or allowed, or when autocomplete algorithms get confused

Shell 中编辑

快捷键 含义
CTRL-B Move the cursor left
CTRL-F Move the cursor right
CTRL-P View the previous command (or move the cursor up)
CTRL-N View the next command (or move the cursor down)
CTRL-A Move the cursor to the beginning of the line
CTRL-E Move the cursor to the end of the line
CTRL-W Erase the preceding word
CTRL-U Erase from cursor to beginning of line
CTRL-K Erase from cursor to end of line
CTRL-Y Paste erased text (for example, from CTRL-U)

查询帮助文档

man ls 查询 ls 的帮助;man -k kerword 查询有 keyword 关键字的帮助,结果你会发现有些数字,这些数字是告诉你这个命令在哪个分类下出现,数字对应的分类信息如下

Section Description
1 User commands
2 Kernel system calls
3 Higher-level Unix programming library documentation
4 Device interface and driver information
5 File descriptions (system configuration files)
6 Games
7 File formats, conventions, and encodings (ASCII, suffixes, and so on)
8 System commands and servers

标准输入输出和错误

  • command > file # 命令的输出存到 file
  • command >> file # 命令的输出追加到 file
  • command1 | command2 # 命令1 的输出作为命令2 的输入
  • head < /proc/cpuinfo# 文件内容到输出

如果执行一个命令会报错,但希望将报错的内容单独保存到其他文件中,可使用下图的命令,The number 2 specifies the stream ID that the shell modifies. Stream ID 1 is standard output (the default), and 2 is standard error. >& notation will send both standard output and standard error to the file named f

管理进程

列出进程

在系统中,每个进程都有一个 PID 作为标识进程的唯一 ID,在 Linux 可以使用 ps 命令直接查看所有进程的情况:

  • PID, The process ID.
  • TTY, The terminal device where the process is running. More about this later.
  • STAT, what the process is doing and where its memory resides. For example, S means sleeping and R means running. (See the ps(1) manual page for a description of all the symbols.)
  • TIME, the total amount of time that the process has spent running instructions on the processor. Remember that because processes don’t run constantly, this is different from the time since the process started (or “wall-clock time”).
  • COMMAND, This one might seem obvious as the command used to run the program, but be aware that a process can change this field from its original value. Furthermore, the shell can perform glob expansion, and this field will reflect the expanded command instead of what you enter at the prompt.

Mac 下没有 STAT 这列

Ps 还有一些选项可以指定,方便我们定义输出:

  • ps x Show all of your running processes.
  • ps ax Show all processes on the system, not just the ones you own.
  • ps u Include more detailed information on processes.
  • ps w Show full command names, not just what fits on one line.

杀死进程

杀死一个进程使用 kill 指令。

1
2
3
kill pid, 让内核向进程发送一个 TERM 信号(默认),停止该进程
kill -STOP pid, 发送 STOP 信号, 冻结该进程,冻结的进程还会存在于内存
kill -CONT pid, 恢复一个冻结的进程

发送上面的指令可能你还会看到进程存在,因为操作系统会给进程机会去处理现场,进程也可能直接忽略你发送的信号,这时候你可以强制杀死一个进程,不给其任何机会

1
2
kill -KILL pid
kill -9 pid

作业管理

作业管理,allows you to suspend and switch between programs you’re using。

For example, you can send a TSTP signal with CTRL-Z and then start the process again by entering fg (bring to foreground) or bg (move to background; see the next section).

可使用 jobs 查看是否有什么作业正在运行。

screen 或者 tmux 来管理作业是一个更好的选择。

进程放到后台使用

如果你有一个解压大量文件的需求,当你运行的时候,只有当全部任务结束你才会看到输出,这时候你也没办法做什么,但你可以将这个任务放到后台去做:

1
gunzip file.gz &

这会给你一个 PID 告诉你正在处理这个任务的进程。

如果这个后台运行的进程需要标准输入,那么它会进入冻结状态,可以使用 fg 将其带入前台,如果这个后台进程报错了,那你会收到报错的信息(即便你当时正在忙别的事情😂),如果不想被打扰,最好将其错误信息重定向到其他地方(前面介绍过).

文件模式和权限

所有 Unix 文件都有一组权限来决定你是否可以读或者写:

1
-rw-r--r-- 1 juser somegroup 7041  Mar 26 19:34  endnotes.html

最开始的 -rw-r--r-- 表明了该文件的权限,其含义如下:

第一个 - 往后开始,三个一组为权限组,其中各个字母的意思如下:

  • r means that the file is readable.
  • w means that the file is writable.
  • x means that the file is executable (you can run it as a program).
  • - means “nothing” (more specifically, the permission for that slot in the set has not been granted).
  • 有些会是s,用来表示用户执行这个程序时,将以这个程序所有者的身份执行,可以直接设置s权限,而无需先有x,但是有可能未生效(将以大S表示)
  • Mac OS X文件系统还有一个的附加属性@.

权限修改

使用 chmod 来修改权限,如对 group 和 others 增加写权限:

1
2
3
4
5
chmod g+r file
chmod o+r file

# 或者通过一条命令
chmod go+r file

你可能会看到通过数字来指定权限的形式:

1
2
# sets all permission bits at once
chmod 644 file

无需记住所有数字的意思,常用的权限可以参考下表:

Mode Meaning Used for
644 user: read/write; group, other: read files
600 user: read/write; group, other: none files
755 user: read/write/execute; group, other: read/execute directories, programs
700 user: read/write/execute; group, other: none directories, programs
711 user: read/write/execute; group, other: execute directories

关于目录的权限:

  • You can list the contents of a directory if it’s readable, but you can only access a file in a directory if the directory is executable.
  • You need both in most cases;

unmask 命令:

  • ou can specify a set of default permissions with the umask shell command, which applies a predefined set of permissions to any new file you create.
  • use umask 022 if you want everyone to be able to see all of the files and directories that you create, and use umask 077 if you don’t.

符号链接

symbolic link is a file that points to another file or a directory, effectively creating an alias (like a shortcut in Windows). Symbolic links offer quick access to obscure directory paths.

1
lrwxrwxrwx 1 ruser users  11 Feb 27 13:52  somedir -> /home/origdir

若符号链接代表的文件不存在,则依赖该文件的命令会出错,除了 ls file

通过符号链接,你无法知道指向的文件或者文件夹的特征,你必须访问该路径去看了才知道,而且一个符号链接可以指向另外一个符号链接,这种被称为 chained symbolic links.

创建一个符号链接可以通过下面的指令:

1
2
# -s 指定为软链接,若没有则是硬链接
ln -s target linkname

注意不要将 target 和 linkname 搞反了,否则会发生的事情就是:creates a link named target inside linkname, and the link will point to itself unless linkname is a full path.

符号链接可能还有一个问题是,你以为你编辑的是一个文件(可以编辑),但其实它是一个符号链接。

符号链接本质上是创建一个新的文件,这个文件保存的是目标文件的地址,而硬链接则是指向文件系统中底层的数据。

  • 当对一个文件创建了硬链接,如果对硬链接修改,那么原始文件也会被修改
  • 当原始文件被删除,访问硬链接还可以看到原始文件内容,因为底层的文件数据存在硬链接这个引用,并未被真的删除;访问软链接则会提示原始文件已经不见。
  • 原始文件被删除后,当向硬链接追加内容,硬链接文件内容会有原始内容和新内容;软连接则会创建一个新文件,并将新内容写入。

解压缩

  • gzip, The program gzip (GNU Zip) is one of the current standard Unix compression programs. 但无法压缩多个文件.
  • tar, 可压缩多个文件,细节如下:
1
2
3
4
5
6
7
8
9
# 压缩
# c, create mode
# v, 显示输入,两个v则显示更多细节
# f, 指定要压缩的文件
tar cvf archive.tar file1 file2 ...

# 解压
tar xvf archive.tar
# x, 解压模式

在解压一个文件之前,先查看压缩文件内有哪些东西是有必要的,可以通过 t 模式来实现。

解压时的权限控制: consider using the p option to preserve permissions. Use this in extract mode to override your umask and get the exact permissions specified in the archive.

压缩文件有时候会有多重压缩的情况,比如你可能看到一个这样的文件: file.tar.gz,解压这种文件需要从右往左使用对应工具解压。

1
2
gunzip file.tar.gz
tar xvf file.tar

但分别解压是比较慢的,需要两次磁盘读写和io时间,可以利用管道来处理:

1
zcat file.tar.gz | tar xvf -

zcat 相当于 gunzip -dc, 其中 d 的意思是解压,c 的意思是将结果放到标准输出。

zcat 使用非常频繁, 所以 tar 将其放在内置参数中:

1
tar ztvf file.tar.gz

其他的解压工具还有 xzbzip2.

Linux 目录结构

目录结构一般如下图所示:


一般的目录文件夹介绍:

  • /bin Contains ready-to-run programs (also known as executables), including most of the basic Unix commands such as ls and cp. Most of the programs in /bin are in binary format, having been created by a C compiler, but some are shell scripts in modern systems.
  • /dev Contains device files. You’ll learn more about these in Chapter 3.
  • /etc This core system configuration directory (pronounced EHT-see) contains the user password, boot, device, networking, and other setup files.
  • /home Holds home (personal) directories for regular users. Most Unix installations conform to this standard.
  • /lib An abbreviation for library, this directory holds library files containing code that executables can use. There are two types of libraries: static and shared. The /lib directory should contain only shared libraries, but other lib directories, such as /usr/lib, contain both varieties as well as other auxiliary files. (We’ll discuss shared libraries in more detail in Chapter 15.)
  • /proc Provides system statistics through a browsable directory-and-file interface. Much of the /proc subdirectory structure on Linux is unique, but many other Unix variants have similar features. The /proc directory contains information about currently running processes as well as some kernel parameters.
  • /run Contains runtime data specific to the system, including certain process IDs, socket files, status records, and, in many cases, system logging. This is a relatively recent addition to the root directory; in older systems, you can find it in /var/run. On newer systems, /var/run is a symbolic link to /run.
  • /sys This directory is similar to /proc in that it provides a device and system interface. You’ll read more about /sys in Chapter 3.
  • /sbin The place for system executables. Programs in /sbin directories relate to system management, so regular users usually do not have /sbin components in their command paths. Many of the utilities found here don’t work if not run as root.
  • /tmp A storage area for smaller, temporary files that you don’t care much about. Any user may read to and write from /tmp, but the user may not have permission to access another user’s files there. Many programs use this directory as a workspace. If something is extremely important, don’t put it in /tmp because most distributions clear /tmp when the machine boots and some even remove its old files periodically. Also, don’t let /tmp fill up with garbage because its space is usually shared with something critical (the rest of /, for example).
  • /usr Although pronounced “user,” this subdirectory has no user files. Instead, it contains a large directory hierarchy, including the bulk of the Linux system. Many of the directory names in /usr are the same as those in the root directory (like /usr/bin and /usr/lib), and they hold the same type of files. (The reason that the root directory does not contain the complete system is primarily historic—in the past, it was to keep space requirements low for the root.)
  • /var The variable subdirectory, where programs record information that can change over the course of time. System logging, user tracking, caches, and other files that system programs create and manage are here. (You’ll notice a /var/tmp directory here, but the system doesn’t wipe it on boot.)

其他 root 目录下的子文件夹:

  • /boot Contains kernel boot loader files. These files pertain only to the very first stage of the Linux startup procedure, so you won’t find information about how Linux starts up its services in this directory. See Chapter 5 for more about this.
  • /media A base attachment point for removable media such as flash drives that is found in many distributions.
  • /opt This may contain additional third-party software. Many systems don’t use /opt.

其他用户目录下存在的子文件夹:

  • /include Holds header files used by the C compiler.
  • /local Is where administrators can install their own software. Its structure should look like that of / and /usr.
  • /man Contains manual pages.
  • /share Contains files that should work on other kinds of Unix machines with no loss of functionality. These are usually auxiliary data files that programs and libraries read as necessary. In the past, networks of machines would share this directory from a file server, but today a share directory used in this manner is rare because there are no realistic space restraints for these kinds of files on contemporary systems. Instead, on Linux distributions, you’ll find /man, /info, and many other subdirectories here because it is an easily understood convention.

内核路径

在 Linux 中,内核一般放在 /vmlinuz or /boot/vmlinuz,在系统启动的时候,boot loader 会启动内核。关于 boot loader 会在之后的文章中介绍。

以超级用户执行命令

1
sudo vipw

可通过 /etc/sudoers 文件配置谁可以使用 sudo 命令执行,是否需要密码等。

下面的配置表明,user1 和 user2 可以直接使用 sudo 命令,而不需要输入密码:

1
2
3
4
5
6
7
8
9
10
11
12
13
# defines an ADMINS user alias with the two users
User_Alias ADMINS = user1, user2

# users in the ADMINS alias can use sudo to execute commands as root
# The second ALL means “any command.” The first ALL means “any host.”
ADMINS ALL = NOPASSWD: ALL

# superuser may also use sudo to run any command on any host.
# extra (ALL) means that the superuser may also run commands as any other user.
root ALL=(ALL) ALL

# 上面的这句,可以扩展为
ADMINS ALL = (ALL) NOPASSWD: ALL

使用 visudo 命令来编辑上面这个文件,它会检查你的语法。

要查看 sudo 命令的历史记录,可使用:

1
journalctl SYSLOG_IDENTIFIER=sudo

来启用,对于老一点的系统,可能需要在 /var/log 查看, 比如 /var/log/auth.log.