<ul class="first_class_ul list-paddingleft-2"><ul class="second_class_ul list-paddingleft-2"></ul></ul>
<h3>Linux】1 Shell</h3>
<h4>1. Quotations</h4>
<p>Computers are not only used to develop websites or software, they are also in our hands Sharp tools, our tools. <br>Shell is the main way we interact with computers. The visual graphical interface is actually very limited. You can only do some things through preset buttons. </p>
<h4>2. Shell</h4>
<h5>2.1 Program</h5>
<p>Most operating systems have a "shell", such as PowerShell in Windows. There may be some differences between them, but generally they are similar. By installing Git, Windows systems can also use Git Bash similar to Linux. (Git bash under windows is sometimes different, and a real Linux system is more recommended) </p>
<p>You can execute the program by entering the program name in the shell. For example, if there is a program called date, enter ($ is Command prompt, just like > in Windows) </p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:bash;">$ date
Sat Mar 18 20:52:33 2023</pre><div class="contentsignin">Copy after login</div></div><p> can pass parameters to the program. For example, the running effect of the program <code>echo</code> is to print out the parameters passed to it, </p> <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:bash;">$ echo hello
hello</pre><div class="contentsignin">Copy after login</div></div><p>In addition, if there are multiple parameters, the parameters are separated by <strong>spaces</strong>. If you pass parameters composed of multiple words, you can use <code>\</code> (escape characters), For example, the following actually only passes one parameter to <code>echo</code>. The </p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:bash;">$ echo hello\ world
hello world</pre><div class="contentsignin">Copy after login</div></div><p> system can find the program you entered through <strong>Path</strong> (path), which can be viewed in the environment variables All paths of </p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:bash;">$ echo $PATH
/c/Users/ThinkPad/bin:/mingw64/bin:/usr/local/bin:/usr/bin:/bin:/mingw64/bin:/usr/bin:/c/Users/ThinkPad/bin:/c/Program Files/Common
...(我省略了)</pre><div class="contentsignin">Copy after login</div></div><p>When you enter a program name, such as <code>echo</code>, the shell will search for the program in the directories of all paths in the system. You can check the directory where a program is located like this Path </p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:bash;">$ which echo
/usr/bin/echo</pre><div class="contentsignin">Copy after login</div></div><p> Supplement: Regarding the parameters of the program (command), we use <code>-</code> followed by a single letter, and <code>--</code> followed by a word. For example <code>-a</code>, <code>--all</code>. </p><h4>2.2 Directory</h4><p>The program will run in the current directory by default. The following are some commonly used directory operations</p><table><tbody><tr class="firstRow"><th>Command</th><th>Introduction</th></tr><tr><td><code>pwd</code>( print work directory)</td><td>Print the current directory path</td></tr><tr><td><code>ls</code></td><td>List the files in the current directory, use ## The #-l<code> parameter will display more detailed information</code></td></tr><tr><td>cd<code></code></td>Switch directory to the specified directory, use <td>cd - <code>You can return to the directory you were in to facilitate switching between the two directories. </code>.<code> represents the current directory, </code>..<code> represents the parent directory, </code>~<code> represents the user directory, and the beginning of </code>/<code> represents the root directory. </code></td></tr></tbody><p>通过<code>--help</code>参数,可以查看某个命令的用法,例如</p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:bash;">$ ls --help</pre><div class="contentsignin">Copy after login</div></div><blockquote><p><strong>想一想</strong>:ls -l列出的目录列表及其信息,如何解读?</p></blockquote><p>为了对文件进行相应的操作,必须拥有整个路径的权限。下面是一些和文件操作有关的命令介绍。</p><h4>2.3 文件</h4><h5>2.3.1 简单文件操作</h5><p>你可以使用<code>mv</code>命令给文件重命名,即使它是<strong>move</strong>(移动)的意思。假设你在当前的目录下已经有个文件<strong>hello.txt</strong>,那么你可以使用命令</p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:bash;">$ mv hello.txt hello2.txt</pre><div class="contentsignin">Copy after login</div></div><p>将它重命名为<strong>hello2.txt</strong>。<code>cp</code>命令可以复制文件,它的意思是<strong>copy</strong> (很显然),你可以接着刚才的命令使用</p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:bash;">$ cp hello2.txt hello.txt</pre><div class="contentsignin">Copy after login</div></div><p>那么你的<strong>hello.txt</strong>文件又回来了!现在你有了两个相同内容的文件,却有着不一样的名字。但估计你会觉得这有些多余,但好在你可以使用命令<code>rm</code>删掉其中一个,就像下面这样</p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:bash;">$ rm hello2.txt</pre><div class="contentsignin">Copy after login</div></div><p>好了,现在我们又回到了刚刚开始的样子,但我们刚刚的操作已经在终端留下了杂乱的信息,使用<code>clear</code>命令清除它们!</p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:bash;">$ clear</pre><div class="contentsignin">Copy after login</div></div><p>如果你想新建文件,可以使用<code>touch</code>命令,比如新建一个空<strong>hello2.txt</strong>文件</p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:bash;">$ touch hello2.txt</pre><div class="contentsignin">Copy after login</div></div><p>有时你会感觉命令的名字有些奇怪,为什么创建文件要是touch?其实它就像前面的mv命令,重命名仅仅对它的一种使用方式,而它能做的不止于此,它其实可以将文件移动到你计算机的任何一个地方!</p><p>touch是一个时间戳命令,当操作的文件不存在时就会自动新建一个文件——而不是它本身只有新建文件的功能。当你感到疑惑时可以使用touch --help看看帮助文档。而我使用echo命令同样达到新建文件的目的</p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:bash;">$ echo '' > hello3.txt</pre><div class="contentsignin">Copy after login</div></div><p>它将新建一个空文件<strong>hello3.txt</strong> 。你可能看着这些linux命令就像回到了高中背单词的时候,同一个单词有着相互略微联系的多种用法——也确实如此,单词是语言的基础。</p><p>至于刚刚命令中的<code>>></code>符号,相信大家一看就懂,我们后面也将会进一步介绍。</p><h5>2.3.2 重定向</h5><p>你可以使用重定向来将程序的输出保存到文件中,而不是直接显示在屏幕上。这里有个流的概念,每个程序都有自己的输入流与输出流,程序从自己的输入流中读取数据,并将运行结果写入到输出流中。而程序默认从键盘输入获取数据,并将运行结果写到屏幕上,我们将键盘输入称为标准输入流,将屏幕称为标准输出流。而重定向将助你获得修改程序输入、输出方向的能力——而不总是键盘和屏幕。</p><p><code>echo</code>命令将输入流中的数据传递到输出流,但我现在不想输出到屏幕,我要将输入的内容保存到文件里,那么可以使用<code>></code>——重定向输出流。</p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:bash;">$ echo hello > hello.txt</pre><div class="contentsignin">Copy after login</div></div><blockquote><p><strong>想一想</strong>:为什么<code>echo < hello.txt</code>输出为空?</p></blockquote><p>类似地可以使用<code><</code>重定向输入流。<code>cat</code>命令可以将文件输入显示到屏幕,如</p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:bash;">$ cat hello.txt
hello</pre><div class="contentsignin">Copy after login</div></div><p>现在我们通过重定向,可以将<strong>hello.txt</strong>文件的内容复制到另一个文件<strong>hello2.txt</strong>中</p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:bash;">$ cat < hello.txt > hello2.txt
$ cat hello2.txt
hello</pre><div class="contentsignin">Copy after login</div></div><p><code>cat</code>程序从<strong>hello.txt</strong>中获得输入,并将输出写入了<strong>hello2.txt</strong>中——而不是屏幕上。</p><p><code>></code>重定向的输出流是<strong>覆盖模式</strong>,对应到文件操作中,就是先清空文件中原有的内容,然后写入新内容。如果你是想在原来的基础上添加一些内容,那么<code>>></code>很适合你,它将以<strong>追加模式</strong>写入新内容。</p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:bash;">$ echo world >> hello.txt
$ cat hello.txt
hello
world</pre><div class="contentsignin">Copy after login</div></div><table><tbody><tr class="firstRow"><th>命令</th><th>介绍</th></tr><tr><td><code><</code></td><td>重定向输入流</td></tr><tr><td><code>></code></td><td>重定向输出流,以覆盖模式写入</td></tr><tr><td><code>>></code></td><td>重定向输出流,以追加模式写入</td></tr></tbody></table><h4>2.4 管道</h4><p>一个程序的输出可以保存到文件里,也可以直接传递给另一个程序。管道符号 | 的作用就是将左侧程序的输出直接传递作为右侧程序的输入。</p><p>tail命令通过-n参数,可以输出它输入中的最后n行。要仅显示ls -l /命令结果的最后两行,可以使用以下命令</p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:bash;">$ ls -l / | tail -n 2
-rw-r--r-- 1 ThinkPad 197121 24183 Nov 28 12:20 unins000.msg
drwxr-xr-x 1 ThinkPad 197121 0 Nov 28 12:20 usr/</pre><div class="contentsignin">Copy after login</div></div><p><code>tee</code>命令可以将输入同时输出到屏幕和指定的文件中,那么加上管道你可以像下面这样</p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:bash;">$ echo linux1234 | tee hello.txt
linux1234</pre><div class="contentsignin">Copy after login</div></div>
The above is the detailed content of What are the common operations of Linux Shell?. For more information, please follow other related articles on the PHP Chinese website!