Table of Contents
Linux】1 Shell
1. Quotations
2. Shell
2.1 Program
2.2 Directory
2.3 文件
2.3.1 简单文件操作
2.3.2 重定向
2.4 管道
Home Operation and Maintenance Linux Operation and Maintenance What are the common operations of Linux Shell?

What are the common operations of Linux Shell?

May 30, 2023 pm 09:05 PM
linux shell

<ul class="first_class_ul list-paddingleft-2"><ul class="second_class_ul list-paddingleft-2"></ul></ul> <h3 id="Linux-Shell">Linux】1 Shell</h3> <h4 id="Quotations">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 id="Shell">2. Shell</h4> <h5 id="Program">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:php;toolbar:false;'>$ 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:php;toolbar:false;'>$ 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:php;toolbar:false;'>$ 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:php;toolbar:false;'>$ 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:php;toolbar:false;'>$ 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 id="Directory">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:php;toolbar:false;'>$ ls --help</pre><div class="contentsignin">Copy after login</div></div><blockquote><p><strong>想一想</strong>:ls -l列出的目录列表及其信息,如何解读?</p></blockquote><p>为了对文件进行相应的操作,必须拥有整个路径的权限。下面是一些和文件操作有关的命令介绍。</p><h4 id="文件">2.3 文件</h4><h5 id="简单文件操作">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:php;toolbar:false;'>$ 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:php;toolbar:false;'>$ 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:php;toolbar:false;'>$ 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:php;toolbar:false;'>$ 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:php;toolbar:false;'>$ 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:php;toolbar:false;'>$ echo &#39;&#39; > hello3.txt</pre><div class="contentsignin">Copy after login</div></div><p>它将新建一个空文件<strong>hello3.txt</strong> 。你可能看着这些linux命令就像回到了高中背单词的时候,同一个单词有着相互略微联系的多种用法——也确实如此,单词是语言的基础。</p><p>至于刚刚命令中的<code>>></code>符号,相信大家一看就懂,我们后面也将会进一步介绍。</p><h5 id="重定向">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:php;toolbar:false;'>$ 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:php;toolbar:false;'>$ 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:php;toolbar:false;'>$ 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:php;toolbar:false;'>$ 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 id="管道">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:php;toolbar:false;'>$ 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:php;toolbar:false;'>$ 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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What computer configuration is required for vscode What computer configuration is required for vscode Apr 15, 2025 pm 09:48 PM

VS Code system requirements: Operating system: Windows 10 and above, macOS 10.12 and above, Linux distribution processor: minimum 1.6 GHz, recommended 2.0 GHz and above memory: minimum 512 MB, recommended 4 GB and above storage space: minimum 250 MB, recommended 1 GB and above other requirements: stable network connection, Xorg/Wayland (Linux)

Linux Architecture: Unveiling the 5 Basic Components Linux Architecture: Unveiling the 5 Basic Components Apr 20, 2025 am 12:04 AM

The five basic components of the Linux system are: 1. Kernel, 2. System library, 3. System utilities, 4. Graphical user interface, 5. Applications. The kernel manages hardware resources, the system library provides precompiled functions, system utilities are used for system management, the GUI provides visual interaction, and applications use these components to implement functions.

How to run java code in notepad How to run java code in notepad Apr 16, 2025 pm 07:39 PM

Although Notepad cannot run Java code directly, it can be achieved by using other tools: using the command line compiler (javac) to generate a bytecode file (filename.class). Use the Java interpreter (java) to interpret bytecode, execute the code, and output the result.

vscode terminal usage tutorial vscode terminal usage tutorial Apr 15, 2025 pm 10:09 PM

vscode built-in terminal is a development tool that allows running commands and scripts within the editor to simplify the development process. How to use vscode terminal: Open the terminal with the shortcut key (Ctrl/Cmd). Enter a command or run the script. Use hotkeys (such as Ctrl L to clear the terminal). Change the working directory (such as the cd command). Advanced features include debug mode, automatic code snippet completion, and interactive command history.

How to check the warehouse address of git How to check the warehouse address of git Apr 17, 2025 pm 01:54 PM

To view the Git repository address, perform the following steps: 1. Open the command line and navigate to the repository directory; 2. Run the "git remote -v" command; 3. View the repository name in the output and its corresponding address.

Where to write code in vscode Where to write code in vscode Apr 15, 2025 pm 09:54 PM

Writing code in Visual Studio Code (VSCode) is simple and easy to use. Just install VSCode, create a project, select a language, create a file, write code, save and run it. The advantages of VSCode include cross-platform, free and open source, powerful features, rich extensions, and lightweight and fast.

What is the main purpose of Linux? What is the main purpose of Linux? Apr 16, 2025 am 12:19 AM

The main uses of Linux include: 1. Server operating system, 2. Embedded system, 3. Desktop operating system, 4. Development and testing environment. Linux excels in these areas, providing stability, security and efficient development tools.

vscode terminal command cannot be used vscode terminal command cannot be used Apr 15, 2025 pm 10:03 PM

Causes and solutions for the VS Code terminal commands not available: The necessary tools are not installed (Windows: WSL; macOS: Xcode command line tools) Path configuration is wrong (add executable files to PATH environment variables) Permission issues (run VS Code as administrator) Firewall or proxy restrictions (check settings, unrestrictions) Terminal settings are incorrect (enable use of external terminals) VS Code installation is corrupt (reinstall or update) Terminal configuration is incompatible (try different terminal types or commands) Specific environment variables are missing (set necessary environment variables)

See all articles