1209 lines
458 KiB
HTML
1209 lines
458 KiB
HTML
|
<!DOCTYPE html>
|
|||
|
<html><head><title>Linux 命令行操作</title><meta charSet="utf-8"/><meta name="viewport" content="width=device-width, initial-scale=1.0"/><meta property="og:title" content="Linux 命令行操作"/><meta property="og:description" content="Shell 命令行是一个文本界面,通过它你可以与计算机的操作系统进行交互。用户可以在命令行中输入命令,操作系统在执行完这些命令后返回结果。."/><meta property="og:image" content="https://wiki.7wate.com/static/og-image.png"/><meta property="og:width" content="1200"/><meta property="og:height" content="675"/><link rel="icon" href="../../../../static/icon.png"/><meta name="description" content="Shell 命令行是一个文本界面,通过它你可以与计算机的操作系统进行交互。用户可以在命令行中输入命令,操作系统在执行完这些命令后返回结果。."/><meta name="generator" content="Quartz"/><link rel="preconnect" href="https://fonts.googleapis.com"/><link rel="preconnect" href="https://fonts.gstatic.com"/><script async src="https://umami.7wate.com/script.js" data-website-id="c061efdc-95dd-4d21-9d04-a1ffda0a85b9"></script><script>
|
|||
|
var _hmt = _hmt || [];
|
|||
|
(function() {
|
|||
|
var hm = document.createElement("script");
|
|||
|
hm.src = "https://hm.baidu.com/hm.js?94d8ccb156eb7c65abf317e6e01cdba9";
|
|||
|
var s = document.getElementsByTagName("script")[0];
|
|||
|
s.parentNode.insertBefore(hm, s);
|
|||
|
})();
|
|||
|
</script><script async src="https://www.googletagmanager.com/gtag/js?id=G-MHMEL0F832"></script><script>
|
|||
|
(function() {
|
|||
|
window.dataLayer = window.dataLayer || [];
|
|||
|
function gtag() {
|
|||
|
window.dataLayer.push(arguments);
|
|||
|
}
|
|||
|
gtag('js', new Date());
|
|||
|
gtag('config', 'G-MHMEL0F832');
|
|||
|
})();
|
|||
|
</script><link href="../../../../index.css" rel="stylesheet" type="text/css" spa-preserve/><link href="https://cdn.jsdelivr.net/npm/katex@0.16.0/dist/katex.min.css" rel="stylesheet" type="text/css" spa-preserve/><link href="https://fonts.googleapis.com/css2?family=IBM Plex Mono&family=Schibsted Grotesk:wght@400;700&family=Source Sans Pro:ital,wght@0,400;0,600;1,400;1,600&display=swap" rel="stylesheet" type="text/css" spa-preserve/><script src="../../../../prescript.js" type="application/javascript" spa-preserve></script><script type="application/javascript" spa-preserve>const fetchData = fetch(`../../../../static/contentIndex.json`).then(data => data.json())</script></head><body data-slug="Tech/getting-started/命令行工具/Shell/Shell-命令操作"><div id="quartz-root" class="page"><div id="quartz-body"><div class="left sidebar"><h1 class="page-title "><a href="../../../..">📚 X·Eden</a></h1><div class="spacer mobile-only"></div><div class="search "><div id="search-icon"><p>Search</p><div></div><svg tabIndex="0" aria-labelledby="title desc" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 19.9 19.7"><title id="title">Search</title><desc id="desc">Search</desc><g class="search-path" fill="none"><path stroke-linecap="square" d="M18.5 18.3l-5.4-5.4"></path><circle cx="8" cy="8" r="7"></circle></g></svg></div><div id="search-container"><div id="search-space"><input autocomplete="off" id="search-bar" name="search" type="text" aria-label="Search for something" placeholder="Search for something"/><div id="results-container"></div></div></div></div><div class="darkmode "><input class="toggle" id="darkmode-toggle" type="checkbox" tabIndex="-1"/><label id="toggle-label-light" for="darkmode-toggle" tabIndex="-1"><svg xmlns="http://www.w3.org/2000/svg" xmlnsXlink="http://www.w3.org/1999/xlink" version="1.1" id="dayIcon" x="0px" y="0px" viewBox="0 0 35 35" style="enable-background:new 0 0 35 35;" xmlSpace="preserve"><title>Light mode</title><path d="M6,17.5C6,16.672,5.328,16,4.5,16h-3C0.672,16,0,16.672,0,17.5 S0.672,19,1.5,19h3C5.328,19,6,18.328,6,17.5z M7.5,26c-0.414,0-0.789,0.168-1.061,0.439l-2,2C4.168,28.711,4,29.086,4,29.5 C4,30.328,4.671,31,5.5,31c0.414,0,0.789-0.168,1.06-0.44l2-2C8.832,28.289,9,27.914,9,27.5C9,26.672,8.329,26,7.5,26z M17.5,6 C18.329,6,19,5.328,19,4.5v-3C19,0.672,18.329,0,17.5,0S16,0.672,16,1.5v3C16,5.328,16.671,6,17.5,6z M27.5,9 c0.414,0,0.789-0.168,1.06-0.439l2-2C30.832,6.289,31,5.914,31,5.5C31,4.672,30.329,4,29.5,4c-0.414,0-0.789,0.168-1.061,0.44 l-2,2C26.168,6.711,26,7.086,26,7.5C26,8.328,26.671,9,27.5,9z M6.439,8.561C6.711,8.832,7.086,9,7.5,9C8.328,9,9,8.328,9,7.5 c0-0.414-0.168-0.789-0.439-1.061l-2-2C6.289,4.168,5.914,4,5.5,4C4.672,4,4,4.672,4,5.5c0,0.414,0.168,0.789,0.439,1.06 L6.439,8.561z M33.5,16h-3c-0.828,0-1.5,0.672-1.5,1.5s0.672,1.5,1.5,1.5h3c0.828,0,1.5-0.672,1.5-1.5S34.328,16,33.5,16z M28.561,26.439C28.289,26.168,27.914,26,27.5,26c-0.828,0-1.5,0.672-1.5,1.5c0,0.414,0.168,0.789,0.439,1.06l2,2 C28.711,30.832,29.086,31,29.5,31c0.828,0,1.5-0.672,1.5-1.5c0-0.414-0.168-0.789-0.439-1.061L28.561,26.439z M17.5,29 c-0.829,0-1.5,0.672-1.5,1.5v3c0,0.828,0.671,1.5,1.5,1.5s1.5-0.672,1.5-1.5v-3C19,29.672,18.329,29,17.5,29z M17.5,7 C11.71,7,7,11.71,7,17.5S11.71,28,17.5,28S28,23.29,28,17.5S23.29,7,17.5,7z M17.5,25c-4.136,0-7.5-3.364-7.5-7.5 c0-4.136,3.364-7.5,7.5-7.5c4.136,0,7.5,3.364,7.5,7.5C25,21.636,21.636,25,17.5,25z"></path></svg></label><label id="toggle-label-dark" for="darkmode-toggle" tabIndex="-1"><svg xmlns="http://www.w3.org/2000/svg" xmlnsXlink="http://www.w3.org/1999/xlink" version="1.1" id="nightIcon" x="0px" y="0px" viewBox="0 0 100 100" style="enable-background='new 0 0 100 100'" xmlSpace="preserve"><title>Dark mode</title><path d="M96.76,66.458c-0.853-0.852-2.15-1.064-3.23-0.534c-6.063,2.991-12.858,4.571-19.655,4.571 C62.022,70.495,50.88,65.88,42.5,57.5C29.043,44.043,25.658,23.536,34.076,6.47c0.532-1.08,0.318-2.379-0.534-3.23 c-0.851-0.852-2.15-1.064-3.23-0.534c-4.918,2.427-9.375,5.619-13.246,9.491c-9.447,9.447-14.65,22.
|
|||
|
<p>Shell 就是终端程序的统称,它充当了人与内核(硬件)之间的翻译官,用户把一些命令告诉终端程序,它就会调用相应的程序服务去完成某些工作。现在包括红帽系统在内的<strong>许多主流 Linux 系统默认使用的终端是 Bash(Bourne-Again SHell)解释器</strong>,这个 Bash 解释器主要有以下 4 项优势:</p>
|
|||
|
<ul>
|
|||
|
<li>通过上下方向键来调取执行过的 Linux 命令;</li>
|
|||
|
<li>命令或参数仅需输入前几位就可以用 Tab 键补全;</li>
|
|||
|
<li>具有强大的批处理脚本;</li>
|
|||
|
<li>具有实用的环境变量功能。</li>
|
|||
|
</ul>
|
|||
|
<h2 id="执行命令">执行命令<a aria-hidden="true" tabindex="-1" href="#执行命令" class="internal"> §</a></h2>
|
|||
|
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="shell" data-theme="default"><code data-language="shell" data-theme="default"><span data-line><span style="color:var(--shiki-token-function);">命令</span><span style="color:var(--shiki-color-text);"> [命令参数] [命令对象]</span></span></code></pre></div>
|
|||
|
<p>可选择的、可加或可不加的、非必需的参数使用中括号引起来,而命令所要求的、必须有的参数或对象值,则不带中括号。</p>
|
|||
|
<ul>
|
|||
|
<li>
|
|||
|
<p><strong>命令名称</strong>:就是语法中的动词,表达的是<strong>想要做的事情</strong>,例如创建用户、查看文件、重启系统等操作。</p>
|
|||
|
</li>
|
|||
|
<li>
|
|||
|
<p><strong>命令参数</strong>:用于<strong>对命令进行调整</strong>,让修改过的命令能更好地贴合工作需求,达到事半功倍的效果。</p>
|
|||
|
</li>
|
|||
|
</ul>
|
|||
|
<p>Linux 命令参数的长格式与短格式示例:</p>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
<table><thead><tr><th>类别</th><th>示例</th></tr></thead><tbody><tr><td>长格式</td><td>man —help</td></tr><tr><td>短格式</td><td>man -h</td></tr></tbody></table>
|
|||
|
<ul>
|
|||
|
<li><strong>命令对象</strong>:一般指要处理的文件、目录、用户等资源名称,也就是<strong>命令执行后的承受方</strong>。例如创建一位叫小明的用户、查看一个叫工资表的文件、重启一个 IP 为 192.168.10.10 的系统等。</li>
|
|||
|
</ul>
|
|||
|
<p>Linux 系统中的<strong>命令、参数、对象都是严格区分大小写</strong>的。</p>
|
|||
|
<h2 id="目录移动">目录移动<a aria-hidden="true" tabindex="-1" href="#目录移动" class="internal"> §</a></h2>
|
|||
|
<p>Linux 终端执行命令的很多情况下需要经常的更换目录,cd 命令、pushd 命令、popd 命令可以快速移动目录。</p>
|
|||
|
<ul>
|
|||
|
<li>cd:回到当前用户目录。</li>
|
|||
|
<li>cd ~:回到当前用户目录(方便切换到其他用户目录)。</li>
|
|||
|
<li>cd -:回到上次工作的目录。</li>
|
|||
|
<li>pushd path:存入 path 目录到目录栈。</li>
|
|||
|
<li>popd:移动到目录栈弹出的目录。</li>
|
|||
|
</ul>
|
|||
|
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="shell" data-theme="default"><code data-language="shell" data-theme="default"><span data-line><span style="color:var(--shiki-token-comment);"># 使用 `cd` 命令回到当前用户目录</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">$</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">cd</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">$</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">pwd</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">/home/username</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 使用 `cd ~` 命令也可以回到当前用户目录</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">$</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">cd</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">~</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">$</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">pwd</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">/home/username</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 使用 `cd -` 命令回到上次工作的目录</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">$</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">cd</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">/tmp</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">$</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">cd</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">-</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">$</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">pwd</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">/home/username</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 使用 `pushd` 命令将目录存入目录栈</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">$</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">pushd</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">/var</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">/var</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">/home/username</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 使用 `popd` 命令移动到目录栈弹出的目录</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">$</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">popd</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">/home/username</span></span></code></pre></div>
|
|||
|
<h2 id="快捷键">快捷键<a aria-hidden="true" tabindex="-1" href="#快捷键" class="internal"> §</a></h2>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
<table><thead><tr><th>快捷键</th><th>功能描述</th></tr></thead><tbody><tr><td><strong>Tab</strong></td><td>自动补全命令或文件名</td></tr><tr><td><strong>Ctrl+C</strong></td><td>终止当前命令</td></tr><tr><td><strong>Ctrl+Z</strong></td><td>将当前进程放到后台</td></tr><tr><td><strong>Ctrl+D</strong></td><td>退出当前终端</td></tr><tr><td><strong>Ctrl+L</strong></td><td>清屏</td></tr><tr><td><strong>Ctrl+U</strong></td><td>删除从光标位置到行首的字符</td></tr><tr><td><strong>Ctrl+K</strong></td><td>删除从光标位置到行尾的字符</td></tr><tr><td><strong>Ctrl+A</strong></td><td>移动光标到行首</td></tr><tr><td><strong>Ctrl+E</strong></td><td>移动光标到行尾</td></tr><tr><td><strong>Ctrl+R</strong></td><td>反向搜索命令历史</td></tr><tr><td><strong>Ctrl+W</strong></td><td>删除光标前的一个单词</td></tr><tr><td><strong>Ctrl+Y</strong></td><td>粘贴由 Ctrl+U、Ctrl+K 或 Ctrl+W 删除的文本</td></tr><tr><td><strong>Ctrl+T</strong></td><td>交换光标处字符与之前的字符</td></tr><tr><td><strong>Alt+B</strong></td><td>向后移动一个单词(或 Ctrl+ 左箭头)</td></tr><tr><td><strong>Alt+F</strong></td><td>向前移动一个单词(或 Ctrl+ 右箭头)</td></tr><tr><td><strong>Ctrl+Shift+C</strong></td><td>复制(部分图形终端)</td></tr><tr><td><strong>Ctrl+Shift+V</strong></td><td>粘贴(部分图形终端)</td></tr></tbody></table>
|
|||
|
<h2 id="特殊符号">特殊符号<a aria-hidden="true" tabindex="-1" href="#特殊符号" class="internal"> §</a></h2>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
<table><thead><tr><th>符号</th><th>功能描述</th></tr></thead><tbody><tr><td><code>!!</code></td><td>引用上一条命令。例如,如果上一条命令是 <code>ls -l</code>,那么输入 <code>!!</code> 将再次执行 <code>ls -l</code>。</td></tr><tr><td><code>!$</code></td><td>引用上一条命令的最后一个参数。例如,如果上一条命令是 <code>cp /some/long/path /another/path</code>,那么输入 <code>cd !$</code> 将执行 <code>cd /another/path</code>。</td></tr><tr><td><code>!string</code></td><td>引用最近一条以 <code>string</code> 开头的命令。例如,如果你之前执行过 <code>ls -l</code>,那么输入 <code>!ls</code> 将再次执行 <code>ls -l</code>。</td></tr><tr><td><code>^string1^string2^</code></td><td>快速替换上一条命令中的字符串并执行。例如,如果上一条命令是 <code>cat file1</code>,那么输入 <code>^file1^file2^</code> 将执行 <code>cat file2</code>。</td></tr><tr><td><code>&&</code></td><td>逻辑 AND。如果前一个命令成功执行(退出状态为 0),则执行后一个命令。例如,<code>command1 && command2</code> 会在 <code>command1</code> 成功后执行 <code>command2</code>。</td></tr><tr><td><code>;</code></td><td>命令分隔符。允许在一行中执行多个命令。例如,<code>command1; command2</code> 会依次执行 <code>command1</code> 和 <code>command2</code>,不考虑前一个命令的成功与否。</td></tr><tr><td><code>></code></td><td>输出重定向。将命令的输出写入到文件中,而不是显示在屏幕上。例如,<code>ls -l > file.txt</code> 将 <code>ls -l</code> 的输出保存到 <code>file.txt</code>。</td></tr><tr><td><code>>></code></td><td>追加输出重定向。将命令的输出追加到现有文件的末尾。例如,<code>echo "hello" >> file.txt</code> 将追加 “hello” 到 <code>file.txt</code>。</td></tr><tr><td><code><</code></td><td>输入重定向。使用文件作为命令的输入。例如,<code>cat < file.txt</code> 会显示 <code>file.txt</code> 的内容。</td></tr><tr><td>“</td><td>反引号。执行其中的命令,并将输出作为另一个命令的参数。例如,echo “Today is `date`” 会显示今天的日期。</td></tr><tr><td><code>$( )</code></td><td>命令替换。类似于反引号,但更易读,易用。例如,<code>echo "Today is $(date)"</code> 会显示今天的日期。</td></tr><tr><td><code>{ }</code></td><td>大括号扩展。生成序列或列表。例如,<code>echo {1..5}</code> 会输出 <code>1 2 3 4 5</code>。</td></tr><tr><td><code>#</code></td><td>注释符号。<code>#</code> 后面的内容将被视为注释,不会被执行。例如,<code># This is a comment</code>。</td></tr></tbody></table>
|
|||
|
<h2 id="常用命令">常用命令<a aria-hidden="true" tabindex="-1" href="#常用命令" class="internal"> §</a></h2>
|
|||
|
<h3 id="文件操作">文件操作<a aria-hidden="true" tabindex="-1" href="#文件操作" class="internal"> §</a></h3>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
<table><thead><tr><th>命令</th><th>全拼</th><th>说明</th></tr></thead><tbody><tr><td>ls</td><td>list</td><td>列出目录中的文件列表</td></tr><tr><td>cd</td><td>change directory</td><td>更改当前工作目录</td></tr><tr><td>pwd</td><td>print working directory</td><td>显示当前工作目录</td></tr><tr><td>mkdir</td><td>make directory</td><td>创建新目录</td></tr><tr><td>rmdir</td><td>remove directory</td><td>删除空目录</td></tr><tr><td>touch</td><td>touch</td><td>创建新文件</td></tr><tr><td>cp</td><td>copy</td><td>复制文件或目录</td></tr><tr><td>mv</td><td>move</td><td>移动文件或目录</td></tr><tr><td>rm</td><td>remove</td><td>删除文件或目录</td></tr></tbody></table>
|
|||
|
<h3 id="文件处理">文件处理<a aria-hidden="true" tabindex="-1" href="#文件处理" class="internal"> §</a></h3>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
<table><thead><tr><th>命令</th><th>全拼</th><th>说明</th></tr></thead><tbody><tr><td>cat</td><td>concatenate</td><td>查看、合并文件内容</td></tr><tr><td>head</td><td>head</td><td>查看文件头部内容</td></tr><tr><td>tail</td><td>tail</td><td>查看文件尾部内容</td></tr><tr><td>grep</td><td>global regular expression print</td><td>在文件中查找指定文本</td></tr><tr><td>sed</td><td>stream editor</td><td>流编辑器,用于对文件进行基本文本处理</td></tr><tr><td>awk</td><td>awk</td><td>文本和数据处理工具</td></tr></tbody></table>
|
|||
|
<h3 id="系统管理">系统管理<a aria-hidden="true" tabindex="-1" href="#系统管理" class="internal"> §</a></h3>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
<table><thead><tr><th>命令</th><th>全拼</th><th>说明</th></tr></thead><tbody><tr><td>ps</td><td>process status</td><td>查看进程状态</td></tr><tr><td>top</td><td>top</td><td>实时查看系统进程状态</td></tr><tr><td>kill</td><td>kill</td><td>终止进程</td></tr><tr><td>shutdown</td><td>shutdown</td><td>关闭系统</td></tr><tr><td>reboot</td><td>reboot</td><td>重启系统</td></tr><tr><td>uname</td><td>uname</td><td>显示系统信息</td></tr><tr><td>df</td><td>disk free</td><td>显示磁盘使用情况</td></tr><tr><td>du</td><td>disk usage</td><td>显示目录空间使用情况</td></tr><tr><td>free</td><td>free</td><td>显示系统内存使用情况</td></tr></tbody></table>
|
|||
|
<h3 id="网络管理">网络管理<a aria-hidden="true" tabindex="-1" href="#网络管理" class="internal"> §</a></h3>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
<table><thead><tr><th>命令</th><th>命令全拼</th><th>描述</th></tr></thead><tbody><tr><td>ip</td><td>internet protocol</td><td>显示和配置网络接口信息</td></tr><tr><td>ping</td><td>ping</td><td>测试主机之间的连通性</td></tr><tr><td>nslookup</td><td>name server lookup</td><td>查询域名解析信息</td></tr><tr><td>traceroute</td><td>trace route</td><td>显示数据包到达目的地经过的路由信息</td></tr><tr><td>route</td><td>route</td><td>显示和配置系统的路由表</td></tr><tr><td>netstat</td><td>network statistics</td><td>显示网络连接状态和统计信息</td></tr><tr><td>tcpdump</td><td>TCP dump</td><td>抓取网络数据包并进行分析</td></tr></tbody></table>
|
|||
|
<h3 id="权限管理">权限管理<a aria-hidden="true" tabindex="-1" href="#权限管理" class="internal"> §</a></h3>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
<table><thead><tr><th>命令</th><th>命令全拼</th><th>描述</th></tr></thead><tbody><tr><td>chmod</td><td>change mode</td><td>修改文件或目录的访问权限</td></tr><tr><td>chown</td><td>change owner</td><td>修改文件或目录的所有者</td></tr><tr><td>chgrp</td><td>change group</td><td>修改文件或目录的所属组</td></tr><tr><td>useradd</td><td>user add</td><td>添加用户账号</td></tr><tr><td>usermod</td><td>user modify</td><td>修改用户账号信息</td></tr><tr><td>userdel</td><td>user delete</td><td>删除用户账号</td></tr><tr><td>groupadd</td><td>group add</td><td>添加用户组</td></tr><tr><td>groupmod</td><td>group modify</td><td>修改用户组信息</td></tr><tr><td>groupdel</td><td>group delete</td><td>删除用户组</td></tr><tr><td>passwd</td><td>password</td><td>修改用户密码</td></tr><tr><td>su</td><td>switch user</td><td>切换用户身份</td></tr><tr><td>sudo</td><td>superuser do</td><td>以超级用户身份执行命令</td></tr><tr><td>visudo</td><td>visual sudo</td><td>编辑 sudo 配置文件</td></tr><tr><td>id</td><td>identity</td><td>显示当前用户信息</td></tr><tr><td>whoami</td><td>who am i</td><td>显示当前登录用户名</td></tr></tbody></table>
|
|||
|
<h3 id="软件管理">软件管理<a aria-hidden="true" tabindex="-1" href="#软件管理" class="internal"> §</a></h3>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
<table><thead><tr><th>命令</th><th>全拼</th><th>说明</th></tr></thead><tbody><tr><td>apt-get</td><td>Advanced Packaging Tool</td><td>Ubuntu 系统软件包管理工具</td></tr><tr><td>yum</td><td>Yellowdog Updater Modified</td><td>CentOS、Fedora 等系统软件包管理工具</td></tr><tr><td>dpkg</td><td>Debian Package</td><td>Debian、Ubuntu 等系统软件包管理工具</td></tr><tr><td>rpm</td><td>Red Hat Package Manager</td><td>CentOS、Fedora 等系统软件包管理工具</td></tr></tbody></table>
|
|||
|
<h3 id="磁盘管理">磁盘管理<a aria-hidden="true" tabindex="-1" href="#磁盘管理" class="internal"> §</a></h3>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
<table><thead><tr><th>命令</th><th>全拼</th><th>描述</th></tr></thead><tbody><tr><td>df</td><td>disk filesystem</td><td>显示磁盘空间使用情况</td></tr><tr><td>du</td><td>disk usage</td><td>显示文件和目录的磁盘使用情况</td></tr><tr><td>fdisk</td><td>fixed disk</td><td>磁盘分区管理工具</td></tr><tr><td>fsck</td><td>file system check</td><td>检查和修复文件系统</td></tr><tr><td>mkfs</td><td>make filesystem</td><td>创建文件系统</td></tr><tr><td>mount</td><td>mount</td><td>挂载文件系统到目录上</td></tr><tr><td>umount</td><td>unmount</td><td>卸载文件系统</td></tr></tbody></table>
|
|||
|
<h3 id="进程管理">进程管理<a aria-hidden="true" tabindex="-1" href="#进程管理" class="internal"> §</a></h3>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
<table><thead><tr><th>命令</th><th>命令全拼</th><th>描述</th></tr></thead><tbody><tr><td>ps</td><td>process status</td><td>显示当前进程状态</td></tr><tr><td>top</td><td>task-oriented processor</td><td>动态显示进程信息</td></tr><tr><td>kill</td><td>kill</td><td>终止一个进程</td></tr><tr><td>killall</td><td>killall</td><td>终止一个指定名称的进程</td></tr><tr><td>nice</td><td>nice</td><td>改变进程优先级</td></tr><tr><td>renice</td><td>renice</td><td>改变正在运行进程的优先级</td></tr><tr><td>jobs</td><td>jobs</td><td>显示当前 shell 的活动作业</td></tr><tr><td>bg</td><td>background</td><td>将一个进程置于后台</td></tr><tr><td>fg</td><td>foreground</td><td>将一个进程置于前台</td></tr><tr><td>nohup</td><td>no hang up</td><td>在后台运行命令,并不挂起</td></tr><tr><td>pstree</td><td>process tree</td><td>显示进程树</td></tr></tbody></table>
|
|||
|
<h2 id="任务">任务<a aria-hidden="true" tabindex="-1" href="#任务" class="internal"> §</a></h2>
|
|||
|
<p>Linux 系统中运行程序时,可以选择在前台或后台运行。在前台运行时,程序会占用终端并且输出信息直接显示在终端中。而在后台运行时,程序在后台默默地执行,不会占用终端并且不会直接显示输出信息。</p>
|
|||
|
<h3 id="运行">运行<a aria-hidden="true" tabindex="-1" href="#运行" class="internal"> §</a></h3>
|
|||
|
<ul>
|
|||
|
<li><code>&</code> 符号:在命令的末尾添加 <code>&</code> 符号,例如:<code>command &</code>。这将使命令在后台运行。</li>
|
|||
|
<li><code>nohup</code> 命令:<code>nohup</code> 命令可以使命令在后台运行,并忽略挂断信号(SIGHUP),**这意味着即使你退出当前终端,程序仍会继续运行。**用法为:<code>nohup command &</code>。</li>
|
|||
|
</ul>
|
|||
|
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="shell" data-theme="default"><code data-language="shell" data-theme="default"><span data-line><span style="color:var(--shiki-token-comment);"># 在后台运行 sleep 命令(这个命令仅仅会让 shell 睡眠一段时间)</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">$</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">sleep</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">300</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-punctuation);">&</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 使用 nohup 命令在后台运行 sleep 命令,并且将输出信息存入 nohup.out 文件中</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">$</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">nohup</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">sleep</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">300</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-punctuation);">&</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 使用 nohup 命令在后台运行 sleep 命令,并且将输出信息重定向到 output.txt 文件中</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">$</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">nohup</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">sleep</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">300</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-keyword);">></span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">output.txt</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-punctuation);">&</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 运行一个会产生错误的命令,并将错误信息重定向到 err.txt 文件中</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">$</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">ls</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">/nonexistent</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-keyword);">></span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">/dev/null</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-keyword);">2></span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">err.txt</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 使用 nohup 命令在后台运行一个会产生错误的命令,并将输出和错误信息重定向到相应的文件中</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">$</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">nohup</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">ls</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">/nonexistent</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-keyword);">></span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">out.txt</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-keyword);">2></span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">err.txt</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-punctuation);">&</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 使用 &> 符号将标准输出和错误输出都重定向到同一个文件中</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">$</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">ls</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">/nonexistent</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-punctuation);">&</span><span style="color:var(--shiki-token-keyword);">></span><span style="color:var(--shiki-color-text);"> all.txt</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 使用 |& 符号将错误输出重定向到另一个命令的标准输入</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">$</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">ls</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">/nonexistent</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-keyword);">|</span><span style="color:var(--shiki-token-punctuation);">&</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-function);">less</span></span></code></pre></div>
|
|||
|
<h3 id="管理">管理<a aria-hidden="true" tabindex="-1" href="#管理" class="internal"> §</a></h3>
|
|||
|
<h4 id="jobs">Jobs<a aria-hidden="true" tabindex="-1" href="#jobs" class="internal"> §</a></h4>
|
|||
|
<p>jobs 命令可以列出<strong>当前终端会话中正在运行的后台任务</strong>。每个任务都有一个唯一的编号,<strong>可以使用该编号来操作任务</strong>,例如将任务切换到前台或终止任务。</p>
|
|||
|
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="shell" data-theme="default"><code data-language="shell" data-theme="default"><span data-line><span style="color:var(--shiki-token-comment);"># 列出当前终端会话中的后台任务</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">$</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">jobs</span></span></code></pre></div>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
<table><thead><tr><th>选项</th><th>选项全拼</th><th>描述</th></tr></thead><tbody><tr><td>-l</td><td>—list</td><td>列出每个作业的进程 ID 号</td></tr><tr><td>-p</td><td>—pid</td><td>仅列出作业的进程 ID 号</td></tr><tr><td>-n</td><td>—new</td><td>显示自上次通知以后已更改的作业</td></tr><tr><td>-r</td><td>—running</td><td>仅列出运行中的作业</td></tr><tr><td>-s</td><td>—stopped</td><td>仅列出停止的作业</td></tr></tbody></table>
|
|||
|
<h4 id="fgbg">fg、bg<a aria-hidden="true" tabindex="-1" href="#fgbg" class="internal"> §</a></h4>
|
|||
|
<p><code>fg</code> 命令可以将一个后台任务切换到前台运行,<code>bg</code> 命令可以将一个在后台暂停的任务切换到后台继续运行。</p>
|
|||
|
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="shell" data-theme="default"><code data-language="shell" data-theme="default"><span data-line><span style="color:var(--shiki-token-comment);"># 将一个后台任务切换到前台运行,假设任务的编号是 1</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">$</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">fg</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">%1</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 将一个后台任务切换到后台继续运行,假设任务的编号是 1</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">$</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">bg</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">%1</span></span></code></pre></div>
|
|||
|
<h4 id="ps">Ps<a aria-hidden="true" tabindex="-1" href="#ps" class="internal"> §</a></h4>
|
|||
|
<p><code>ps</code> 命令可以<strong>显示系统中所有进程的状态,包括后台运行的进程。</strong></p>
|
|||
|
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="shell" data-theme="default"><code data-language="shell" data-theme="default"><span data-line><span style="color:var(--shiki-token-comment);"># 列出系统中所有进程的状态</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">$</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">ps</span></span></code></pre></div>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
<table><thead><tr><th>选项</th><th>选项全拼</th><th>描述</th></tr></thead><tbody><tr><td>-A</td><td>—all</td><td>显示所有进程(在某些系统上,此选项为 -e)</td></tr><tr><td>-f</td><td>—full</td><td>完整格式列出</td></tr><tr><td>-u</td><td>—user</td><td>按用户列出</td></tr><tr><td>-x</td><td>—all-user-processes</td><td>列出所有用户的进程</td></tr><tr><td>-l</td><td>—long</td><td>长格式列出</td></tr><tr><td>-j</td><td>—jobs</td><td>工作格式列出</td></tr><tr><td>-r</td><td>—running</td><td>列出运行中的进程</td></tr><tr><td>-a</td><td>—all-with-tty</td><td>列出拥有终端的所有进程</td></tr><tr><td>-e</td><td>—every</td><td>列出所有进程(在某些系统上,此选项为 -A)</td></tr><tr><td>-p</td><td>—pid</td><td>列出指定进程号的进程</td></tr></tbody></table>
|
|||
|
<h4 id="kill">Kill<a aria-hidden="true" tabindex="-1" href="#kill" class="internal"> §</a></h4>
|
|||
|
<p><code>kill</code> 命令是用于发送特定的信号到操作系统中的进程。默认情况下,如果不指定信号,<code>kill</code> 命令会发送 <code>TERM</code> (终止) 信号,这会请求一个进程自我终止。</p>
|
|||
|
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="shell" data-theme="default"><code data-language="shell" data-theme="default"><span data-line><span style="color:var(--shiki-token-comment);"># 终止一个后台任务,假设任务的编号是 1</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">$</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">kill</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">%1</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 使用 ps 命令找到进程的 PID,然后使用 kill 命令终止进程</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">$</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">ps</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">$</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">kill</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">PID</span></span></code></pre></div>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
<table><thead><tr><th>选项</th><th>描述</th></tr></thead><tbody><tr><td><code>-l</code></td><td>列出所有可用的信号</td></tr><tr><td><code>-s signal</code></td><td>指定发送的信号,可以是信号名或信号号码</td></tr><tr><td><code>-p</code></td><td>不发送信号,只打印进程 ID</td></tr><tr><td><code>-9</code> 或 <code>-KILL</code></td><td>发送 <code>KILL</code> 信号强制终止进程</td></tr></tbody></table>
|
|||
|
<h2 id="管道">管道<a aria-hidden="true" tabindex="-1" href="#管道" class="internal"> §</a></h2>
|
|||
|
<p><strong>管道命令符把前一个命令原本要输出到屏幕的信息当作后一个命令的标准输入</strong>,其执行格式为「命令 A | 命令 B」。按下键盘上的 Shift+ 反斜杠(\)键即可输入管道符,</p>
|
|||
|
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="shell" data-theme="default"><code data-language="shell" data-theme="default"><span data-line><span style="color:var(--shiki-token-comment);"># 输出禁止登录用户行数</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">grep</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">/sbin/nologin</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">/etc/passwd</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-keyword);">|</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-function);">wc</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">-l</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 搜索与 bash 有关的进程</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">ps</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">aux</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-keyword);">|</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-function);">grep</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">bash</span></span></code></pre></div>
|
|||
|
<p><strong>命令符可以无限组合,例如:命令 A | 命令 B | 命令 C |……</strong></p>
|
|||
|
<blockquote>
|
|||
|
<p>曾经有位东北的同学做了一个特别贴切的类比:把管道符当做流水线作业,这跟吃顿烧烤是同一个道理,即第一个人负责切肉,第二个人负责串肉,第三个人负责烧烤,最后的处理结果交付给用户。</p>
|
|||
|
</blockquote>
|
|||
|
<h2 id="重定向">重定向<a aria-hidden="true" tabindex="-1" href="#重定向" class="internal"> §</a></h2>
|
|||
|
<p>输入重定向是指把文件导入到命令中,而输出重定向则是指把原本要输出到屏幕的数据信息写入到指定文件中。</p>
|
|||
|
<ul>
|
|||
|
<li>
|
|||
|
<p><strong>标准输入重定向(STDIN,文件描述符为 0)</strong>:默认从键盘输入,也可从其他文件或命令中输入。</p>
|
|||
|
</li>
|
|||
|
<li>
|
|||
|
<p><strong>标准输出重定向(STDOUT,文件描述符为 1)</strong>:默认输出到屏幕。</p>
|
|||
|
</li>
|
|||
|
<li>
|
|||
|
<p><strong>错误输出重定向(STDERR,文件描述符为 2)</strong>:默认输出到屏幕。</p>
|
|||
|
</li>
|
|||
|
</ul>
|
|||
|
<p><em>对于重定向中的标准输出模式,可以省略<strong>文件描述符 1 不写,而错误输出模式的文件描述符 2 是必须要写的。</strong></em></p>
|
|||
|
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="shell" data-theme="default"><code data-language="shell" data-theme="default"><span data-line><span style="color:var(--shiki-token-comment);"># 输入重定向:将 file1.txt 作为 sort 命令的标准输入</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">$</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">sort</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-keyword);"><</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">file1.txt</span><span style="color:var(--shiki-color-text);"> </span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 输入重定向并重定向输出:将 file1.txt 作为 sort 命令的输入,并将结果输出到 file2.txt 中</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">$</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">sort</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-keyword);"><</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">file1.txt</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-keyword);">></span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">file2.txt</span><span style="color:var(--shiki-color-text);"> </span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># Here 文档(<<):将两行 "This is a line." 作为 cat 的输入</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">$</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">cat</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-keyword);"><<</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-color-text);">EOF</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-string);">This is a line.</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-string);">This is a line.</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">EOF</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 输出重定向:将 "Hello, World!" 输出到 output.txt 文件中(覆盖文件原有内容)</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">$</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">echo</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">"Hello, World!"</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-keyword);">></span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">output.txt</span><span style="color:var(--shiki-color-text);"> </span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 错误输出重定向:将错误信息输出到 error.txt 文件中</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">$</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">ls</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">/nonexistentdirectory</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-keyword);">2></span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">error.txt</span><span style="color:var(--shiki-color-text);"> </span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 追加输出重定向:将 "Hello again!" 追加到 output.txt 文件中</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">$</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">echo</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">"Hello again!"</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-keyword);">>></span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">output.txt</span><span style="color:var(--shiki-color-text);"> </span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 追加错误输出重定向:将错误信息追加到 error.txt 文件中</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">$</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">ls</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">/anothernonexistentdirectory</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-keyword);">2>></span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">error.txt</span><span style="color:var(--shiki-color-text);"> </span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 同时重定向标准输出和错误输出,追加到同一文件</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">$</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">ls</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">/nonexistentdirectory</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-keyword);">>></span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">all_output.txt</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-keyword);">2>&1</span><span style="color:var(--shiki-color-text);"> </span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 同时重定向标准输出和错误输出,追加到同一文件(简化写法)</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">$</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">ls</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">/nonexistentdirectory</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-punctuation);">&</span><span style="color:var(--shiki-token-keyword);">>></span><span style="color:var(--shiki-color-text);"> all_output.txt </span></span>
|
|||
|
<span data-line> </span></code></pre></div>
|
|||
|
<h3 id="输入重定向">输入重定向<a aria-hidden="true" tabindex="-1" href="#输入重定向" class="internal"> §</a></h3>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
<table><thead><tr><th>符号</th><th>作用</th></tr></thead><tbody><tr><td>命令 < 文件</td><td>将文件作为命令的标准输入</td></tr><tr><td>命令 << 分界符</td><td>从标准输入中读入,直到遇见分界符才停止</td></tr><tr><td>命令 < 文件 1 > 文件 2</td><td>将文件 1 作为命令的标准输入并将标准输出到文件 2</td></tr></tbody></table>
|
|||
|
<h3 id="输出重定向">输出重定向<a aria-hidden="true" tabindex="-1" href="#输出重定向" class="internal"> §</a></h3>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
<table><thead><tr><th>符号</th><th>作用</th></tr></thead><tbody><tr><td>命令 > 文件</td><td>将标准输出重定向到一个文件中(清空原有文件的数据)</td></tr><tr><td>命令 2> 文件</td><td>将错误输出重定向到一个文件中(清空原有文件的数据)</td></tr><tr><td>命令 >> 文件</td><td>将标准输出重定向到一个文件中(追加到原有内容的后面)</td></tr><tr><td>命令 2>> 文件</td><td>将错误输出重定向到一个文件中(追加到原有内容的后面)</td></tr><tr><td>命令 >> 文件 2>&1、命令 &>> 文件</td><td>将标准输出与错误输出共同写入到文件中(追加到原有内容的后面)</td></tr></tbody></table>
|
|||
|
<h2 id="通配符">通配符<a aria-hidden="true" tabindex="-1" href="#通配符" class="internal"> §</a></h2>
|
|||
|
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="shell" data-theme="default"><code data-language="shell" data-theme="default"><span data-line><span style="color:var(--shiki-token-comment);"># 使用 * 通配符:列出所有 .txt 文件</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">$</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">ls</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">*.txt</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 使用 ? 通配符:列出所有只有一个字符的 .txt 文件</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">$</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">ls</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">?.txt</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 使用 [] 通配符:列出所有以小写字母开始的 .txt 文件</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">$</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">ls</span><span style="color:var(--shiki-color-text);"> [a-z]</span><span style="color:var(--shiki-token-keyword);">*</span><span style="color:var(--shiki-color-text);">.txt</span></span></code></pre></div>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
<table><thead><tr><th>通配符</th><th>含义</th></tr></thead><tbody><tr><td>*</td><td>任意字符</td></tr><tr><td>?</td><td>单个任意字符</td></tr><tr><td>[a-z]</td><td>单个小写字母</td></tr><tr><td>[A-Z]</td><td>单个大写字母</td></tr><tr><td>[a-Z]</td><td>单个字母</td></tr><tr><td>[0-9]</td><td>单个数字</td></tr><tr><td><a href="../../../../:alpha:" class="internal" data-slug=":alpha:">:alpha:</a></td><td>任意字母</td></tr><tr><td><a href="../../../../:upper:" class="internal" data-slug=":upper:">:upper:</a></td><td>任意大写字母</td></tr><tr><td><a href="../../../../:lower:" class="internal" data-slug=":lower:">:lower:</a></td><td>任意小写字母</td></tr><tr><td><a href="../../../../:digit:" class="internal" data-slug=":digit:">:digit:</a></td><td>所有数字</td></tr><tr><td><a href="../../../../:alnum:" class="internal" data-slug=":alnum:">:alnum:</a></td><td>任意字母加数字</td></tr><tr><td><a href="../../../../:punct:" class="internal" data-slug=":punct:">:punct:</a></td><td>标点符号</td></tr></tbody></table>
|
|||
|
<h2 id="转义字符">转义字符<a aria-hidden="true" tabindex="-1" href="#转义字符" class="internal"> §</a></h2>
|
|||
|
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="shell" data-theme="default"><code data-language="shell" data-theme="default"><span data-line><span style="color:var(--shiki-token-comment);"># 使用反斜杠(\)使反斜杠后面的一个变量变为单纯的字符。</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">$</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">echo</span><span style="color:var(--shiki-color-text);"> \$</span><span style="color:var(--shiki-token-string);">HOME</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">$HOME</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 使用单引号(' ')转义其中所有的变量为单纯的字符串。</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">$</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">echo</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">'$HOME'</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">$HOME</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 使用双引号(" ")保留其中的变量属性,不进行转义处理。</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">$</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">echo</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">"$HOME"</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">/home/username</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 使用反引号(` `)把其中的命令执行后返回结果。</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">$</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">echo</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">`</span><span style="color:var(--shiki-token-function);">date</span><span style="color:var(--shiki-token-string-expression);">`</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">Sun</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">Sep</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">4</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">22</span><span style="color:var(--shiki-token-string);">:38:10</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">PDT</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">2023</span></span></code></pre></div>
|
|||
|
<h2 id="环境变量">环境变量<a aria-hidden="true" tabindex="-1" href="#环境变量" class="internal"> §</a></h2>
|
|||
|
<p><strong>在 Linux 系统中,变量名称一般都是大写的,命令则都是小写的,这是一种约定俗成的规范。</strong></p>
|
|||
|
<p>Linux 系统中最重要的 10 个环境变量</p>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
<table><thead><tr><th>变量名称</th><th>作用</th></tr></thead><tbody><tr><td>HOME</td><td>用户的主目录(即家目录)</td></tr><tr><td>SHELL</td><td>用户在使用的 Shell 解释器名称</td></tr><tr><td>HISTSIZE</td><td>输出的历史命令记录条数</td></tr><tr><td>HISTFILESIZE</td><td>保存的历史命令记录条数</td></tr><tr><td>MAIL</td><td>邮件保存路径</td></tr><tr><td>LANG</td><td>系统语言、语系名称</td></tr><tr><td>RANDOM</td><td>生成一个随机数字</td></tr><tr><td>PS1</td><td>Bash 解释器的提示符</td></tr><tr><td>PATH</td><td>定义解释器搜索用户执行命令的路径</td></tr><tr><td>EDITOR</td><td>用户默认的文本编辑器</td></tr></tbody></table>
|
|||
|
<h2 id="帮助文档">帮助文档<a aria-hidden="true" tabindex="-1" href="#帮助文档" class="internal"> §</a></h2>
|
|||
|
<h3 id="man">Man<a aria-hidden="true" tabindex="-1" href="#man" class="internal"> §</a></h3>
|
|||
|
<p>使用 <strong>man</strong> 命令查看到的帮助内容信息</p>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
<table><thead><tr><th>按键</th><th>作用</th></tr></thead><tbody><tr><td>空格键</td><td>向下翻一页</td></tr><tr><td>PaGe down</td><td>向下翻一页</td></tr><tr><td>PaGe up</td><td>向上翻一页</td></tr><tr><td>home</td><td>直接前往首页</td></tr><tr><td>end</td><td>直接前往尾页</td></tr><tr><td>/</td><td>从上至下搜索某个关键词,如“/linux”</td></tr><tr><td>?</td><td>从下至上搜索某个关键词,如“?linux”</td></tr><tr><td>n</td><td>定位到下一个搜索到的关键词</td></tr><tr><td>N</td><td>定位到上一个搜索到的关键词</td></tr><tr><td>q</td><td>退出帮助文档</td></tr></tbody></table>
|
|||
|
<h4 id="示例">示例<a aria-hidden="true" tabindex="-1" href="#示例" class="internal"> §</a></h4>
|
|||
|
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="shell" data-theme="default"><code data-language="shell" data-theme="default"><span data-line><span style="color:var(--shiki-token-comment);"># 查看 man 的帮助文件</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">man</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">man</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 查看 ssh 的帮助文件</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">man</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">sh</span></span></code></pre></div></article></div><div class="right sidebar"><div class="graph "><h3>Graph View</h3><div class="graph-outer"><div id="graph-container" data-cfg="{"drag":true,"zoom":true,"depth":1,"scale":1.1,"repelForce":0.5,"centerForce":0.3,"linkDistance":30,"fontSize":0.6,"opacityScale":1,"showTags":true,"removeTags":[]}"></div><svg version="1.1" id="global-graph-icon" xmlns="http://www.w3.org/2000/svg" xmlnsXlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 55 55" fill="currentColor" xmlSpace="preserve"><path d="M49,0c-3.309,0-6,2.691-6,6c0,1.035,0.263,2.009,0.726,2.86l-9.829,9.829C32.542,17.634,30.846,17,29,17
|
|||
|
s-3.542,0.634-4.898,1.688l-7.669-7.669C16.785,10.424,17,9.74,17,9c0-2.206-1.794-4-4-4S9,6.794,9,9s1.794,4,4,4
|
|||
|
c0.74,0,1.424-0.215,2.019-0.567l7.669,7.669C21.634,21.458,21,23.154,21,25s0.634,3.542,1.688,4.897L10.024,42.562
|
|||
|
C8.958,41.595,7.549,41,6,41c-3.309,0-6,2.691-6,6s2.691,6,6,6s6-2.691,6-6c0-1.035-0.263-2.009-0.726-2.86l12.829-12.829
|
|||
|
c1.106,0.86,2.44,1.436,3.898,1.619v10.16c-2.833,0.478-5,2.942-5,5.91c0,3.309,2.691,6,6,6s6-2.691,6-6c0-2.967-2.167-5.431-5-5.91
|
|||
|
v-10.16c1.458-0.183,2.792-0.759,3.898-1.619l7.669,7.669C41.215,39.576,41,40.26,41,41c0,2.206,1.794,4,4,4s4-1.794,4-4
|
|||
|
s-1.794-4-4-4c-0.74,0-1.424,0.215-2.019,0.567l-7.669-7.669C36.366,28.542,37,26.846,37,25s-0.634-3.542-1.688-4.897l9.665-9.665
|
|||
|
C46.042,11.405,47.451,12,49,12c3.309,0,6-2.691,6-6S52.309,0,49,0z M11,9c0-1.103,0.897-2,2-2s2,0.897,2,2s-0.897,2-2,2
|
|||
|
S11,10.103,11,9z M6,51c-2.206,0-4-1.794-4-4s1.794-4,4-4s4,1.794,4,4S8.206,51,6,51z M33,49c0,2.206-1.794,4-4,4s-4-1.794-4-4
|
|||
|
s1.794-4,4-4S33,46.794,33,49z M29,31c-3.309,0-6-2.691-6-6s2.691-6,6-6s6,2.691,6,6S32.309,31,29,31z M47,41c0,1.103-0.897,2-2,2
|
|||
|
s-2-0.897-2-2s0.897-2,2-2S47,39.897,47,41z M49,10c-2.206,0-4-1.794-4-4s1.794-4,4-4s4,1.794,4,4S51.206,10,49,10z"></path></svg></div><div id="global-graph-outer"><div id="global-graph-container" data-cfg="{"drag":true,"zoom":true,"depth":-1,"scale":0.9,"repelForce":0.5,"centerForce":0.3,"linkDistance":30,"fontSize":0.6,"opacityScale":1,"showTags":true,"removeTags":[]}"></div></div></div><div class="toc desktop-only"><button type="button" id="toc" class><h3>Table of Contents</h3><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="fold"><polyline points="6 9 12 15 18 9"></polyline></svg></button><div id="toc-content"><ul class="overflow"><li class="depth-0"><a href="#执行命令" data-for="执行命令">执行命令</a></li><li class="depth-0"><a href="#目录移动" data-for="目录移动">目录移动</a></li><li class="depth-0"><a href="#快捷键" data-for="快捷键">快捷键</a></li><li class="depth-0"><a href="#特殊符号" data-for="特殊符号">特殊符号</a></li><li class="depth-0"><a href="#常用命令" data-for="常用命令">常用命令</a></li><li class="depth-1"><a href="#文件操作" data-for="文件操作">文件操作</a></li><li class="depth-1"><a href="#文件处理" data-for="文件处理">文件处理</a></li><li class="depth-1"><a href="#系统管理" data-for="系统管理">系统管理</a></li><li class="depth-1"><a href="#网络管理" data-for="网络管理">网络管理</a></li><li class="depth-1"><a href="#权限管理" data-for="权限管理">权限管理</a></li><li class="depth-1"><a href="#软件管理" data-for="软件管理">软件管理</a></li><li class="depth-1"><a href="#磁盘管理" data-for="磁盘管理">磁盘管理</a></li><li class="depth-1"><a href="#进程管理" data-for="进程管理">进程管理</a></li><li class="depth-0"><a href="#任务" data-for="任务">任务</a></li><li class="depth-1"><a href="#运行" data-for="运行">运行</a></li><li class="depth-1"><a href="#管理" data-for="管理">管理</a></li><li class="depth-0"><a href="#管道" data-for="管道">管道</a></li><li class="depth-0"><a href="#重定向" data-for="重定向">重定向</a></li><li class="depth-1"><a href="#输入重定向" data-for="输入重定向">输入重定向</a></li><li class="depth-1"><a href="#输出重定向" data-for="输出重定向">输出重定向</a></li><li class="depth-0"><a href="#通配符" data-for="通配符">通配符</a></li><li class="depth-0"><a href="#转义字符" data-for="转义字符">转义字符</a></li><li class="depth-0"><a href="#环境变量" data-for="环境变量">环境变量</a></li><li class="depth-0"><a href="#帮助文档" data-for="帮助文档">帮助文档</a></li><li class="depth-1"><a href="#man" data-for="man">Man</a></li></ul></div></div><div class="backlinks "><h3>Backlinks</h3><ul class="overflow"><li>No backlinks found</li></ul></div><div class="explorer mobile-only"><button type="button" id="explorer" data-behavior="collapse" data-collapsed="collapsed" data-savestate="true" data-tree="[{"path":"Basics","collapsed":true},{"path":"Basics/english","collapsed":true},{"path":"Blog","collapsed":true},{"path":"Book","collapsed":true},{"path":"Book/个人成长","collapsed":true},{"path":"Book/医学健康","collapsed":true},{"path":"Book/历史","collapsed":true},{"path":"Book/哲学宗教","collapsed":true},{"path":"Book/心理","collapsed":true},{"path":"Book/政治军事","collapsed":true},{"path":"Book/教育学习","collapsed":true},{"path":"Book/<2F>
|
|||
|
function toggleCallout() {
|
|||
|
const outerBlock = this.parentElement;
|
|||
|
outerBlock.classList.toggle(`is-collapsed`);
|
|||
|
const collapsed = outerBlock.classList.contains(`is-collapsed`);
|
|||
|
const height = collapsed ? this.scrollHeight : outerBlock.scrollHeight;
|
|||
|
outerBlock.style.maxHeight = height + `px`;
|
|||
|
let current = outerBlock;
|
|||
|
let parent = outerBlock.parentElement;
|
|||
|
while (parent) {
|
|||
|
if (!parent.classList.contains(`callout`)) {
|
|||
|
return;
|
|||
|
}
|
|||
|
const collapsed2 = parent.classList.contains(`is-collapsed`);
|
|||
|
const height2 = collapsed2 ? parent.scrollHeight : parent.scrollHeight + current.scrollHeight;
|
|||
|
parent.style.maxHeight = height2 + `px`;
|
|||
|
current = parent;
|
|||
|
parent = parent.parentElement;
|
|||
|
}
|
|||
|
}
|
|||
|
function setupCallout() {
|
|||
|
const collapsible = document.getElementsByClassName(
|
|||
|
`callout is-collapsible`
|
|||
|
);
|
|||
|
for (const div of collapsible) {
|
|||
|
const title = div.firstElementChild;
|
|||
|
if (title) {
|
|||
|
title.removeEventListener(`click`, toggleCallout);
|
|||
|
title.addEventListener(`click`, toggleCallout);
|
|||
|
const collapsed = div.classList.contains(`is-collapsed`);
|
|||
|
const height = collapsed ? title.scrollHeight : div.scrollHeight;
|
|||
|
div.style.maxHeight = height + `px`;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
document.addEventListener(`nav`, setupCallout);
|
|||
|
window.addEventListener(`resize`, setupCallout);
|
|||
|
</script><script type="module">
|
|||
|
import mermaid from 'https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.esm.min.mjs';
|
|||
|
const darkMode = document.documentElement.getAttribute('saved-theme') === 'dark'
|
|||
|
mermaid.initialize({
|
|||
|
startOnLoad: false,
|
|||
|
securityLevel: 'loose',
|
|||
|
theme: darkMode ? 'dark' : 'default'
|
|||
|
});
|
|||
|
document.addEventListener('nav', async () => {
|
|||
|
await mermaid.run({
|
|||
|
querySelector: '.mermaid'
|
|||
|
})
|
|||
|
});
|
|||
|
</script><script src="https://cdn.jsdelivr.net/npm/katex@0.16.7/dist/contrib/copy-tex.min.js" type="application/javascript"></script><script src="../../../../postscript.js" type="module"></script></html>
|