Linux & Windows 小技巧(3)

Linux & Windows 小技巧(3)

图源:Miv4t - 《MACHiNERY》,Pid:77186528

Windows 下的 cat 和 grep

lscatgrep这三个命令,往往是 Linux 初学者最先了解、日常使用 Linux 时不可或缺的。对于 Windows 系统而言,同样有三个命令可以完成上述工作:dirtypefindstr

例如,用 type 显示文件内容:

image-20210413151134589

或者用 findstr 结合管道匹配先前程序输出内容中的字符串:

image-20210413151152079

也可以结合正则表达式:

image-20210413151207393

当然,如果你不满足于系统命令的显示效果,你也可以尝试下面两款开源于 Github 的工具:

peco

项目链接:https://github.com/peco/peco

peco可以实现对管道输入的文本进行快速搜索、排序、多行选择等功能,使用 Golang 编写。目前仍不支持中文编码(GBK),且不支持默认的 Windows Powershell 终端(你需要使用传统的命令提示符,或者更先进的终端程序,比如 Hyper 或者 Windows Terminal)。该项目仍在活跃开发中,合理的使用可以提升效率。

image-20210413151244160

bat

项目链接:https://github.com/sharkdp/bat

bat是一款替代 cat/type 的工具,使用 Golang 编写。加入了代码高亮、Git 增删标识、不可打印字符显示等功能。

image-20210413151302358

使用参数 -A 可以显示所有不可打印字符:

image-20210413151355738

软连接、硬链接

使用命令行

在 Linux 下,软连接(即符号链接)和硬链接的使用是非常普遍的。我们经常需要使用 ln 命令来进行创建。

创建硬链接的命令是:

1
ln [Target] [Link name]

创建软连接,只需要再加上参数 -s 即可。既然是“链接”,那么说明对链接指向的文件进行修改实际上就是对源文件本身进行修改。

硬链接与源文件必须处于同一文件系统内,且源文件必须真实存在。且硬链接的源文件必须是单一文件,不能是目录,因为这样会造成硬链接包含地狱。软连接的源文件则可以是文件,也可以是目录,且无需实际存在。源文件不存在的软连接成为死链接。软连接具有自己的权限与文件属性。软连接可以跨文件系统链接。删除硬链接和软连接均不会对源文件产生影响。

Windows 下 NTFS 文件系统中也有类似的概念,且 Windows 中也提供了对应的创建链接的命令。

创建软链接的命令是:

1
mklink [Link] [Target]

这个命令 Powershell 中是没有的,因此依然需要使用传统的命令提示符。同时注意,Link 和 Target 的顺序是相反的(这也导致我总是记错)。如果创建硬链接,加参数 /H 即可。

同时要注意,对于目录的软连接与符号链接有稍许不同。目录符号连接与 Linux 下的目录软链接基本等同,具有独立的文件属性,在移动时仍保持相对或绝对路径的链接关系,在复制时创建源文件的副本(同 Linux 下使用 cp 直接复制软连接。Linux 下的 cp -d 可以将软连接复制为软连接,而不是将软连接复制为文件)。而 Win 下的目录软链接在移动时,会在目标位置创建同名文件夹,并把源目录内的所有文件移动至新的目录。

创建目录符号连接的命令:

1
mklink /D [Link] [Target Dir]

创建目录软连接的命令:

1
mklink /J [Link] [Target Dir]

rm 删除除指定文件外的其他文件

Linux 下,删除一个文件 / 文件夹,或删除该文件夹下所有文件等操作都很简单。但如果想保留文件夹下某一个或某几个文件,同时删除其他文件,该如何操作?

可以结合 lsgrepxargs

1
ls | grep -vE <FileName> | xargs rm -rf

这是最传统的方式。你也可以使用一种 bash 技巧,无需借助 xargs 即可完成这一目的。

1
2
3
4
# 开启需要的 shell option
shopt -s extglob
# 然后运行!
rm -rf !(<FileName>)

下面附上 shopt 的 cli 参数解释,以及 shell options 列表:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
Syntax
shopt [-pqsu] [-o] [optname ...]

Options
-s Enable (set) each optname

-u Disable (unset) each optname.

-p Display a list of all settable options, with an indication of
whether or not each is set. The output is displayed in a form
that can be reused as input. (-p is the default action)

-q Suppresses normal output; the return status indicates whether the optname
is set or unset. If multiple optname arguments are given with '-q',
the return status is zero if all optnames are enabled; non-zero otherwise.

-o Restricts the values of optname to be those defined for the '-o' option to
the set builtin.

If either '-s' or '-u' is used with no optname arguments, the display is limited to those options which are set or unset, respectively. Unless otherwise noted, the shopt options are disabled (off) by default.

The return status is zero unless an optname was *not* enabled or was not a valid shell option.

The list of shopt options is:

cdable_vars
If this is set, an argument to the cd builtin command that
is not a directory is assumed to be the name of a variable whose value is
the directory to change to.

cdspell
If set, minor errors in the spelling of a directory component in a cd
command will be corrected. The errors checked for are transposed characters,
a missing character, and a character too many. If a correction is found, the
corrected path is printed, and the command proceeds. This option is only used
by interactive shells.

checkhash
If this is set, Bash checks that a command found in the hash table exists
before trying to execute it. If a hashed command no longer exists, a normal
path search is performed.

checkwinsize
If set, Bash checks the window size after each command and, if necessary,
updates the values of LINES and COLUMNS.

cmdhist
If set, Bash attempts to save all lines of a multiple-line command in the
same history entry. This allows easy re-editing of multi-line commands.

dotglob
If set, Bash includes filenames beginning with a '.' in the results of filename
expansion.

execfail
If this is set, a non-interactive shell will not exit if it cannot execute
the file specified as an argument to the exec builtin command.
An interactive shell does not exit if exec fails.

expand_aliases
If set, aliases are expanded. This option is enabled by default for interactive
shells.

extglob
If set, the extended pattern matching features described above are enabled.

histappend
If set, the history list is appended to the history file when the shell exits,
rather than overwriting the history file.
shopt -s histappend
To append every line to history individually set:
PROMPT_COMMAND='history -a'
With these two settings, a new shell will get the history lines from all previous
shells instead of the default 'last window closed'>history
(the history file is named by the value of the HISTFILE variable)

histreedit
If set, and Readline is being used, a user is given the opportunity to re-edit
a failed history substitution.

histverify
If set, and Readline is being used, the results of history substitution
are not immediately passed to the shell parser. Instead, the resulting line
is loaded into the Readline editing buffer, allowing further modification.

hostcomplete
If set, and Readline is being used, Bash will attempt to perform hostname
completion when a word containing a '@' is being completed.
This option is enabled by default.

huponexit
If set, Bash will send SIGHUP to all jobs when an interactive
login shell exits.

interactive_comments
Allow a word beginning with '#' to cause that word and all
remaining characters on that line to be ignored in an interactive shell. This
option is enabled by default.

lithist
If enabled, and the cmdhist option is enabled, multi-line commands
are saved to the history with embedded newlines rather than using semicolon
separators where possible.

mailwarn
If set, and a file that Bash is checking for mail has been accessed since
the last time it was checked, the message "The mail in mailfile
has been read" is displayed.

no_empty_cmd_completion
If set, and Readline is being used, Bash will not attempt to search the
PATH for possible completions when completion is attempted on
an empty line.

nocaseglob
If set, Bash matches filenames in a case-insensitive fashion when performing
filename expansion.

nullglob
If set, Bash allows filename patterns which match no files to expand to
a null string, rather than themselves.

progcomp
If set, the programmable completion facilities are enabled. This option
is enabled by default.

promptvars
If set, prompt strings undergo variable and parameter expansion after being
expanded. This option is enabled by default.

restricted_shell
The shell sets this option if it is started in restricted mode. The value
can not be changed. This is not reset when the startup files are executed,
allowing the startup files to discover whether or not a shell is restricted.

shift_verbose
If this is set, the shift builtin prints an error message when
the shift count exceeds the number of positional parameters.

sourcepath
If set, the source builtin uses the value of PATH
to find the directory containing the file supplied as an argument. This option
is enabled by default.

xpg_echo
If set, the echo builtin expands backslash-escape sequences
by default.
作者

Zhou Weitong

发布于

2020-12-08

更新于

2021-04-13

许可协议

评论

Your browser is out-of-date!

Update your browser to view this website correctly.&npsb;Update my browser now

×