发布于 2026-01-06 0 阅读
0

Linux Terminal: The Ultimate Cheat Sheet - Part 2 TL;DR Global regular expression search (grep command) Pipes Permissions: Change file mode bits command (chmod) Groups Ownership: Change file owner and group (chown) Shortcuts Working with long files

Linux 终端:终极速查表 - 第二部分

太长不看

全局正则表达式搜索(grep 命令)

管道

权限:更改文件模式位命令 (chmod)

团体

所有权:更改文件所有者和组(chown)

快捷方式

处理长文件

本文是“如何充分利用 Linux 终端”系列文章的第二部分。
如果您还没有看过第一篇文章,我强烈建议您在继续阅读本文之前先阅读一下:



💡 如果您觉得这篇文章有价值,可以关注我的推特Instagram账号

太长不看

  • 使用 Grep 搜索
    • 在文件中搜索字符串 ➜grep [term-to-search] [source-file-to-search]
    • 文件内不区分大小写的搜索 ➜grep -i [term-to-search] [source-file-to-search]
    • 在文件中查找不匹配的行 ➜grep -v [term-to-search] [source-file-to-search]
    • 目录递归搜索 ➜grep -r [term-to-search] [path-to-directory-to-search]
    • 文件内多次搜索 ➜grep -E "[first-term-to-search|second-term-to-search]" [source-file-to-search]
    • 统计搜索结果 ➜grep -c [term-to-search] [source-file-to-search]
    • 显示匹配文件的名称 ➜grep -l [term-to-search] [matching-files-to-search]
    • 了解更多关于 grep 的信息 ➜man grep

  • 管道
    • 管道命令 ➜[command 1] | [command 2] | [command n]
    • 将过滤后的搜索结果通过管道传输到新文件 ➜ls | grep [term-to-filter] | cat > [path-to-new-file]/[name-for-new-file]
    • 搜索您的命令历史记录 ➜history | grep "[term-to-search]"

  • 权限:更改文件模式位命令 (chmod)
    • 向所有人添加执行权限 ➜chmod a+x [name-of-the-file]chmod +x [name-of-the-file]
    • 移除所有人的执行权限 ➜chmod a-x [name-of-the-file]chmod -x [name-of-the-file]
    • 为所有者添加执行权限 ➜chmod u+x [name-of-the-file]
    • 移除其他用户的写入权限 ➜chmod o-w [name-of-the-file]
    • 为群组添加读取权限 ➜chmod g+r [name-of-the-file]
    • 移除所有人的读写权限 ➜chmod a-wr [name-of-the-file]
    • 移除当前目录中所有文件的读写权限 ➜chmod a-wr *.*

  • 团体
    • 列出所有可用组 ➜getent group
    • 列出我的帐户所属的所有群组 ➜groups
    • 搜索特定群组(使用竖线)➜getent group | grep [group-name-to-search]
    • 创建新群组 ➜sudo groupadd [name-for-the-new-group]
    • 将现有用户添加到辅助组 ➜usermod -a -G [group-you-want-to-add-the-user-to] [user-name-to-add]

  • 所有权:更改文件所有者和组(chown)
    • 更改文件的用户所有权 ➜sudo chown [new-owner-name] [file-to-change-ownership]
    • 更改多个文件的用户所有权 ➜sudo chown [new-owner-name] [file-1-to-change-ownership] [file-n-to-change-ownership]
    • 更改目录的用户所有权 ➜sudo chown [new-owner-name] [directory-to-change-ownership]
    • 递归地更改目录及其所有文件的用户所有权 ➜sudo chown -R [new-owner-name] [directory-to-change-ownership]
    • 更改文件的组所有权 ➜sudo chown :[new-group-name] [file-to-change-ownership]
    • 更改文件的用户和组所有权 ➜sudo chown [new-owner-name]:[new-group-name] [file-to-change-ownership]

  • 快捷方式
    • 搜索您的搜索历史记录 ➜ [CTRL] + r。然后输入几个字符以找到您的命令。
    • 粘贴前几行 ➜[CTRL] + p
    • 将光标移至行首。➜[CTRL] + a
    • 将光标移至行尾。➜[CTRL] + e
    • 将光标向前移动一个字符。➜[CTRL] + f
    • 将光标向后移动一个字符。➜[CTRL] + b
    • 删除整行。➜[CTRL] + u
    • 删除最后一个输入的单词。➜[CTRL] + w

  • 处理长文件
    • 打印文件的最后几行 ➜tail [name-of-the-file]
    • 打印文件的最后 n 行 ➜tail -n [number-of-lines] [name-of-the-file]
    • 打印文件的前几行 ➜head [name-of-the-file]
    • 打印文件的前 n 行 ➜head -n [number-of-lines] [name-of-the-file]
    • 翻阅文件 ➜less [name-of-the-file]

全局正则表达式搜索(grep 命令)

Linuxgrep命令是一个字符串和模式匹配实用程序,用于显示多个文件中匹配的行。

在文件中搜索字符串

输入grep [term-to-search] [source-file-to-search]关键词在文件中搜索。-n如果想打印每个匹配项的行号,可以添加参数。

## List the content for the working directory
mauro_codes@mauro-desktop:~/projects/landing-page$ ls
README.md  index.html  main.js  script.txt

## Print the content of the index.html file
mauro_codes@mauro-desktop:~/projects/landing-page$ cat index.html
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta http-equiv="x-ua-compatible" content="ie=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />

    <title>My Website</title>
  </head>

  <body>
    <script src="js/main.js"></script>
  </body>
</html>

## Search for "meta" within the index.html file using grep
mauro_codes@mauro-desktop:~/projects/landing-page$ grep meta index.html -n
4:    <meta charset="utf-8" />
5:    <meta http-equiv="x-ua-compatible" content="ie=edge" />
6:    <meta name="viewport" content="width=device-width, initial-scale=1" />
Enter fullscreen mode Exit fullscreen mode

文件内不区分大小写的搜索

输入以下内容grep -i [term-to-search] [source-file-to-search]以执行不区分大小写的搜索:

## Print the content of the main.js file
mauro_codes@mauro-desktop:~/projects/landing-page$ cat main.js
const sum = (num1, num2) => {
        return num1 + num2
}

// Call the Sum function
sum(10,4)

## Search for "sum" without ignoring case (we get 2 results)
mauro_codes@mauro-desktop:~/projects/landing-page$ grep sum main.js -n
1:const sum = (num1, num2) => {
6:sum(10,4)

## Search for "sum" using the ignore case argument (we get 3 results)
mauro_codes@mauro-desktop:~/projects/landing-page$ grep sum main.js -in
1:const sum = (num1, num2) => {
5:// Call the Sum function
6:sum(10,4)
Enter fullscreen mode Exit fullscreen mode

在文件中搜索不匹配的行

输入此命令grep -v [term-to-search] [source-file-to-search]可获取文件中所有与该术语不匹配的行。

## Print the content of the main.js file
mauro_codes@mauro-desktop:~/projects/landing-page$ cat main.js
const sum = (num1, num2) => {
        return num1 + num2
}

// Call the Sum function
sum(10,4)

## Search for each line that doesn't include "sum" (case sensitive)
mauro_codes@mauro-desktop:~/projects/landing-page$ grep sum main.js -v
        return num1 + num2
}

// Call the Sum function
Enter fullscreen mode Exit fullscreen mode

在目录中进行递归搜索

输入grep -r [term-to-search] [path-to-directory-to-search]以搜索嵌套目录和子目录。

## List the content for the working directory
mauro_codes@mauro-desktop:~/projects/landing-page$ ls
README.md  index.html  main.js  script.txt  temp

## Search recursively for "sum" within the current directory (including sub-directories)
mauro_codes@mauro-desktop:~/projects/landing-page$ grep -r sum .
./index.html:    <p>Calling the sum function:<p>
./index.html:    <!-- TODO: Call the sum function and display the value -->
./main.js:const sum = (num1, num2) => {
./main.js:sum(10,4)
./temp/index.html:    <p>Calling the sum function:<p>
./temp/index.html:    <!-- TODO: Call the sum function and display the value -->
Enter fullscreen mode Exit fullscreen mode

在文件中进行多次搜索

输入grep -E "[first-term-to-search|second-term-to-search]" [source-file-to-search]以在文件中搜索多个词语

## Print the content of the main.js file
mauro_codes@mauro-desktop:~/projects/landing-page$ cat main.js
const sum = (num1, num2) => {
        return num1 + num2
}

// Call the Sum function
sum(10,4)

const printMessage(message) {
        console.log(message)
}

// Call the printMessage function
printMessage("Hello world")

## Search for "sum" or "printMessage" within the main.js file
mauro_codes@mauro-desktop:~/projects/landing-page$ grep -E "sum|printMessage" main.js
const sum = (num1, num2) => {
sum(10,4)
const printMessage(message) {
// Call the printMessage function
printMessage("Hello world")
Enter fullscreen mode Exit fullscreen mode

统计搜索结果

输入grep -c [term-to-search] [source-file-to-search]以统计搜索词在文件中出现的次数

## List the content for the working directory
mauro_codes@mauro-desktop:~/projects/landing-page$ ls
README.md  index.html  main.js  script.txt  temp

## Count how many times "sum" appears on the main.js file
mauro_codes@mauro-desktop:~/projects/landing-page$ grep -c sum main.js
2

## Count how many times "sum" or "printMessage" appears on the main.js file 
mauro_codes@mauro-desktop:~/projects/landing-page$ grep -c -E "sum|printMessage" main.js
5

## Recursive count to get how many times "sum" or "printMessage" appears in the current working directory
mauro_codes@mauro-desktop:~/projects/landing-page$ grep -c -E -r "sum|printMessage" .
./index.html:2
./main.js:5
./README.md:0
./script.txt:0
./temp/index.html:2
Enter fullscreen mode Exit fullscreen mode

显示匹配文件的名称

输入搜索词grep -l [term-to-search] [matching-files-to-search]以获取包含该搜索词的文件列表

## List the content for the working directory
mauro_codes@mauro-desktop:~/projects/landing-page$ ls
README.md  about.html  index.html  main.js  script.txt  temp

## Get a list of html files that contains "<h1>"
mauro_codes@mauro-desktop:~/projects/landing-page$ grep -l "<h1>" *.html
index.html

## Get a list of html files that contains "<p>"
mauro_codes@mauro-desktop:~/projects/landing-page$ grep -l "<p>" *.html
about.html
index.html

## Recursively get a list of files that contains "<p>" within the current directory
mauro_codes@mauro-desktop:~/projects/landing-page$ grep -l -r "<p>" .
./about.html
./index.html
./temp/index.html
Enter fullscreen mode Exit fullscreen mode

管道

管道是 Linux 上最有用的命令行功能之一,它允许你通过“管道”将多个操作连接在一起来执行复杂的操作,这意味着管道中每个命令的输出将作为下一个命令的输入。

将过滤后的搜索结果通过管道传输到新文件中

假设我想获取所有标题中包含“svelte”一词的博客文章列表,并将完整列表写入一个名为 svelte-articles.txt 的新文件中。

我们可以使用管道列出我的所有文章,使用 grep 过滤掉包含“svelte”的文章,并将过滤后的列表保存到一个新文件中。

## Check that my current working directory is the "posts" folder that includes all my posts
mauro_codes@mauro-desktop:~/projects/maurogarcia.dev/posts$ pwd
/home/mauro_codes/projects/maurogarcia.dev/posts

## Piping the list of posts with grep to check how many posts include the term "svelte" (I get five items)
mauro_codes@mauro-desktop:~/projects/maurogarcia.dev/posts$ ls | grep svelte
5

## Piping the list of post --> Filter svelte posts --> move the content to a new file using cat
mauro_codes@mauro-desktop:~/projects/maurogarcia.dev/posts$ ls | grep svelte | cat > /home/mauro_codes/projects/svelte-articles.txt

## Print the content of our new file
mauro_codes@mauro-desktop:~/projects/maurogarcia.dev/posts$ cat /home/mauro_codes/projects/svelte-articles.txt
-rw-r--r-- 1 mauro_codes mauro_codes 4.2K Jan 27 18:29 -5-things-i-love-about-svelte-.md
-rw-r--r-- 1 mauro_codes mauro_codes  11K Jan 27 18:29 angular-vs-svelte-card-component.md
-rw-r--r-- 1 mauro_codes mauro_codes 5.3K Jan 27 18:29 component-driven-development-with-svelte.md
-rw-r--r-- 1 mauro_codes mauro_codes 5.0K Jan 27 18:29 how-to-build-your-next-chrome-extension-with-svelte.md
-rw-r--r-- 1 mauro_codes mauro_codes 4.7K Jan 27 18:29 sapper-svelte-tailwindcss-boilerplate.md
Enter fullscreen mode Exit fullscreen mode

搜索您的命令历史记录

grep您可以使用管道符通过键入以下命令来搜索命令历史记录。history | grep "[term-to-search]"

感谢@bradnichol在我之前的帖子中提出的这个建议!

## Searching for the term svelte on your command history
mauro_codes@mauro-desktop:~$ history | grep "svelte"
  415  ls | grep svelte
  416  ls | grep -C svelte
  417  ls | grep -c svelte
  418  ls | grep -c svelte |
  419  ls | grep -c svelte | cat
  420  ls | grep svelte | cat
  421  ls | grep svelte | cat > /home/mauro_codes/projects/svelte-articles.txt
  422  cat /home/mauro_codes/projects/svelte-articles.txt
  444  ls grep -c svelte
  445  ls | grep -c svelte
  446  cat /home/mauro_codes/projects/svelte-articles.txt
  448  history | grep "svelte"
Enter fullscreen mode Exit fullscreen mode

权限:更改文件模式位命令 (chmod)

Linux 系统中权限更改是一个非常复杂的话题,值得单独撰写一篇文章。但我决定先举几个可能用得上的例子。

您可以使用此chmod命令为一个或多个文件添加或删除不同用户的读取、写入和执行权限。

作为参考,以下示例展示了当我们使用时,每个文件或目录的权限是如何显示的。ls lah

Linux 权限

添加所有人的执行权限

输入chmod a+x [name-of-the-file]chmod +x [name-of-the-file]添加对特定文件的执行权限给所有人

## Check file permissions
mauro_codes@mauro-desktop:~/projects/landing-page$ ls -lah
total 0
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 27 17:49 .
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 27 20:00 ..
---------- 1 mauro_codes mauro_codes 206 Jan 27 16:36 main.js
---------- 1 mauro_codes mauro_codes  23 Jan 24 17:31 script.txt

## Add execute permission to everyone in the main.js file
mauro_codes@mauro-desktop:~/projects/landing-page$ chmod +x main.js

## Check permissions again
mauro_codes@mauro-desktop:~/projects/landing-page$ ls -lah
total 0
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 27 17:49 .
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 27 20:00 ..
---x--x--x 1 mauro_codes mauro_codes 206 Jan 27 16:36 main.js
---------- 1 mauro_codes mauro_codes  23 Jan 24 17:31 script.txt
Enter fullscreen mode Exit fullscreen mode

移除所有人的执行权限

输入chmod a-x [name-of-the-file]chmod -x [name-of-the-file]删除特定文件的所有人执行权限

## Check file permissions
mauro_codes@mauro-desktop:~/projects/landing-page$ ls -lah
total 0
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 27 17:49 .
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 27 20:00 ..
-rwxrwxrwx 1 mauro_codes mauro_codes 206 Jan 27 16:36 main.js
-rwxrwxrwx 1 mauro_codes mauro_codes  23 Jan 24 17:31 script.txt

## Remove the execute permissions to everyone in the main.js file
mauro_codes@mauro-desktop:~/projects/landing-page$ chmod a-x main.js

## Check permissions again
mauro_codes@mauro-desktop:~/projects/landing-page$ ls -lah
total 0
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 27 17:49 .
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 27 20:00 ..
-rw-rw-rw- 1 mauro_codes mauro_codes 206 Jan 27 16:36 main.js
-rwxrwxrwx 1 mauro_codes mauro_codes  23 Jan 24 17:31 script.txt
Enter fullscreen mode Exit fullscreen mode

为所有者添加执行权限

输入此命令chmod u+x [name-of-the-file],即可仅为特定文件的所有者添加执行权限。

## Check file permissions
mauro_codes@mauro-desktop:~/projects/landing-page$ ls -lah
total 0
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 27 17:49 .
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 27 20:00 ..
---------- 1 mauro_codes mauro_codes 206 Jan 27 16:36 main.js
---------- 1 mauro_codes mauro_codes  23 Jan 24 17:31 script.txt

## Add the execute permissions only to the owner in the main.js file
mauro_codes@mauro-desktop:~/projects/landing-page$ chmod u+x main.js

## Check permissions again
mauro_codes@mauro-desktop:~/projects/landing-page$ ls -lah
total 0
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 27 17:49 .
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 27 20:00 ..
---x------ 1 mauro_codes mauro_codes 206 Jan 27 16:36 main.js
---------- 1 mauro_codes mauro_codes  23 Jan 24 17:31 script.txt
Enter fullscreen mode Exit fullscreen mode

移除其他用户的写入权限

输入此命令chmod o-w [name-of-the-file]可仅移除其他用户(而非所有者或用户组)对特定文件的写入权限

## Check file permissions
mauro_codes@mauro-desktop:~/projects/landing-page$ ls -lah
total 0
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 27 17:49 .
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 27 20:00 ..
-rwxrwxrwx 1 mauro_codes mauro_codes 206 Jan 27 16:36 main.js
-rwxrwxrwx 1 mauro_codes mauro_codes  23 Jan 24 17:31 script.txt

## Remove the write permission only to other users (not owner or group) in the main.js file
mauro_codes@mauro-desktop:~/projects/landing-page$ chmod o-w main.js

## Check permissions again
mauro_codes@mauro-desktop:~/projects/landing-page$ ls -lah
total 0
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 27 17:49 .
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 27 20:00 ..
-rwxrwxr-x 1 mauro_codes mauro_codes 206 Jan 27 16:36 main.js
-rwxrwxrwx 1 mauro_codes mauro_codes  23 Jan 24 17:31 script.txt
Enter fullscreen mode Exit fullscreen mode

添加对该组的读取权限

输入命令chmod g+r [name-of-the-file],为特定文件向组添加只读权限

## Check file permissions
mauro_codes@mauro-desktop:~/projects/landing-page$ ls -lah
total 0
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 27 17:49 .
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 27 20:00 ..
---------- 1 mauro_codes mauro_codes 206 Jan 27 16:36 main.js
---------- 1 mauro_codes mauro_codes  23 Jan 24 17:31 script.txt

## Add the read permission only to the group in the main.js file
mauro_codes@mauro-desktop:~/projects/landing-page$ chmod g+r main.js

## Check permissions again
mauro_codes@mauro-desktop:~/projects/landing-page$ ls -lah
total 0
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 27 17:49 .
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 27 20:00 ..
----r----- 1 mauro_codes mauro_codes 206 Jan 27 16:36 main.js
---------- 1 mauro_codes mauro_codes  23 Jan 24 17:31 script.txt
Enter fullscreen mode Exit fullscreen mode

移除所有人的读写权限

输入此命令chmod a-wr [name-of-the-file]可移除所有人对特定文件的读写权限

## Check file permissions
mauro_codes@mauro-desktop:~/projects/landing-page$ ls -lah
total 0
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 27 17:49 .
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 27 20:00 ..
-rwxrwxrwx 1 mauro_codes mauro_codes 206 Jan 27 16:36 main.js
-rwxrwxrwx 1 mauro_codes mauro_codes  23 Jan 24 17:31 script.txt

## Remove the write and read permission to everyone in the main.js file
mauro_codes@mauro-desktop:~/projects/landing-page$ chmod a-wr main.js

## Check permissions again
mauro_codes@mauro-desktop:~/projects/landing-page$ ls -lah
total 0
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 27 17:49 .
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 27 20:00 ..
---x--x--x 1 mauro_codes mauro_codes 206 Jan 27 16:36 main.js
-rwxrwxrwx 1 mauro_codes mauro_codes  23 Jan 24 17:31 script.txt
Enter fullscreen mode Exit fullscreen mode

移除所有人对多个文件的读写权限

输入以下命令chmod a-wr *.*可移除当前工作目录中所有文件的所有人的读写权限。

## Check file permissions
mauro_codes@mauro-desktop:~/projects/landing-page$ ls -lah
total 0
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 27 17:49 .
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 27 20:00 ..
-rwxrwxrwx 1 mauro_codes mauro_codes 206 Jan 27 16:36 main.js
-rwxrwxrwx 1 mauro_codes mauro_codes  23 Jan 24 17:31 script.txt

## Remove the write and read permissions to everyone for all the files in the current working directory
mauro_codes@mauro-desktop:~/projects/landing-page$ chmod a-wr *.*

## Check permissions again
mauro_codes@mauro-desktop:~/projects/landing-page$ ls -lah
total 0
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 27 17:49 .
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 27 20:00 ..
---x--x--x 1 mauro_codes mauro_codes 206 Jan 27 16:36 main.js
---x--x--x 1 mauro_codes mauro_codes  23 Jan 24 17:31 script.txt
Enter fullscreen mode Exit fullscreen mode

团体

列出所有可用组

输入getent group以获取包含系统中所有可用组的列表。

mauro_codes@mauro-desktop:~/projects/landing-page$ getent group
root:x:0:
daemon:x:1:
bin:x:2:
sys:x:3:
mauro_codes:x:1000:
Enter fullscreen mode Exit fullscreen mode

列出我的帐户所属的所有组

输入内容groups以获取您所属的所有群组列表。

## List all my groups
mauro_codes@mauro-desktop:~/projects/landing-page$ groups
mauro_codes adm dialout cdrom floppy sudo audio dip video plugdev netdev
Enter fullscreen mode Exit fullscreen mode

使用竖线搜索特定群组

输入getent group | grep [group-name-to-search]竖线以搜索特定组。

## List all the groups that contains the term "mauro_codes"
mauro_codes@mauro-desktop:~/projects/landing-page$ getent group | grep "mauro_codes"
adm:x:4:syslog,mauro_codes
mauro_codes:x:1000:

## List all the groups that contains the term "root"
mauro_codes@mauro-desktop:~/projects/landing-page$ getent group | grep "root"
root:x:0:
Enter fullscreen mode Exit fullscreen mode

创建一个新组

输入sudo groupadd [name-for-the-new-group]以创建新群组。

## Create a new group called "development"
mauro_codes@mauro-desktop:~/projects/landing-page$ sudo groupadd development

## Check if the new group was created
mauro_codes@mauro-desktop:~/projects/landing-page$ getent group | grep development
development:x:1001:
Enter fullscreen mode Exit fullscreen mode

将现有用户添加到辅助组

输入usermod -a -G [group-you-want-to-add-the-user-to] [user-name-to-add]以将现有用户添加到辅助组。

## Add mauro_codes to the secondary group "development"
mauro_codes@mauro-desktop:~/projects$ sudo usermod -a -G development mauro_codes

## Check changes
mauro_codes@mauro-desktop:~/projects$ getent group | grep "mauro"
adm:x:4:syslog,mauro_codes
dialout:x:20:mauro_codes
mauro_codes:x:1000:
development:x:1001:mauro_codes
Enter fullscreen mode Exit fullscreen mode

所有权:更改文件所有者和组(chown)

更改文件的用户所有权

输入sudo chown [new-owner-name] [file-to-change-ownership]以更改文件的用户所有权。

## Check user ownership
mauro_codes@mauro-desktop:~/projects/landing-page$ ls -lah
total 0
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 27 17:49 .
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 27 20:00 ..
---x--x--x 1 mauro_codes mauro_codes 206 Jan 27 16:36 main.js
---x--x--x 1 mauro_codes mauro_codes  23 Jan 24 17:31 script.txt

## Change user ownership (the root user will be the owner of the main.js file)
mauro_codes@mauro-desktop:~/projects/landing-page$ sudo chown root main.js

## Check user ownership again
mauro_codes@mauro-desktop:~/projects/landing-page$ ls -lah
total 0
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 27 17:49 .
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 27 20:00 ..
---x--x--x 1 root        mauro_codes 206 Jan 27 16:36 main.js
---x--x--x 1 mauro_codes mauro_codes  23 Jan 24 17:31 script.txt
Enter fullscreen mode Exit fullscreen mode

更改多个文件的用户所有权

输入此命令sudo chown [new-owner-name] [file-1-to-change-ownership] [file-n-to-change-ownership]可同时更改多个文件的用户所有权。

## Check user ownership
mauro_codes@mauro-desktop:~/projects/landing-page$ ls -lah
total 0
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 27 17:49 .
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 27 20:00 ..
---x--x--x 1 mauro_codes mauro_codes 206 Jan 27 16:36 main.js
---x--x--x 1 mauro_codes mauro_codes  23 Jan 24 17:31 script.txt

## Change user ownership (the root user will be the owner of the main.js and script.txt files)
mauro_codes@mauro-desktop:~/projects/landing-page$ sudo chown root main.js script.txt

## Check user ownership again
mauro_codes@mauro-desktop:~/projects/landing-page$ ls -lah
total 0
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 27 17:49 .
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 27 20:00 ..
---x--x--x 1 root        mauro_codes 206 Jan 27 16:36 main.js
---x--x--x 1 root        mauro_codes  23 Jan 24 17:31 script.txt
Enter fullscreen mode Exit fullscreen mode

更改目录的用户所有权

输入sudo chown [new-owner-name] [directory-to-change-ownership]此命令可更改目录的用户所有权。

## Check directories user ownership
mauro_codes@mauro-desktop:~/projects$ ls -lah
total 4.0K
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 27 20:00 .
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 28 10:48 ..
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 27 17:49 landing-page
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 22 12:15 personal-blog

## Change user ownership (the root user will be the owner of the "landing-page" directory)
mauro_codes@mauro-desktop:~/projects$ sudo chown root ./landing-page/

## Check directories user ownership again
mauro_codes@mauro-desktop:~/projects$ ls -lah
total 4.0K
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 27 20:00 .
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 28 10:48 ..
drwxr-xr-x 1 root        mauro_codes 512 Jan 27 17:49 landing-page
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 22 12:15 personal-blog
Enter fullscreen mode Exit fullscreen mode

递归地更改目录及其所有文件的用户所有权

输入命令sudo chown -R [new-owner-name] [directory-to-change-ownership]以递归方式更改目录及其所有文件的用户所有权。

## Check files ownershop within the landing-page directory
mauro_codes@mauro-desktop:~/projects/landing-page$ ls -lah
total 0
drwxr-xr-x 1 root        mauro_codes 512 Jan 27 17:49 .
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 27 20:00 ..
---x--x--x 1 mauro_codes mauro_codes 206 Jan 27 16:36 main.js
---x--x--x 1 mauro_codes mauro_codes  23 Jan 24 17:31 script.txt
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 27 16:27 temp_dir

## Recursively change the user ownership (the root user will be the owner 
## of all the files and directories within the current directory)
mauro_codes@mauro-desktop:~/projects/landing-page$ sudo chown -R root .

## Check user ownership again
mauro_codes@mauro-desktop:~/projects/landing-page$ ls -lah
total 0
drwxr-xr-x 1 root        mauro_codes 512 Jan 27 17:49 .
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 27 20:00 ..
---x--x--x 1 root        mauro_codes 206 Jan 27 16:36 main.js
---x--x--x 1 root        mauro_codes  23 Jan 24 17:31 script.txt
drwxr-xr-x 1 root        mauro_codes 512 Jan 27 16:27 temp_dir
Enter fullscreen mode Exit fullscreen mode

更改文件的组所有权

输入此命令sudo chown :[new-group-name] [file-to-change-ownership]可更改文件的组所有权。

## Check group ownership
mauro_codes@mauro-desktop:~/projects/landing-page$ ls
total 0
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 27 17:49 .
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 27 20:00 ..
---x--x--x 1 mauro_codes mauro_codes 206 Jan 27 16:36 main.js
---x--x--x 1 mauro_codes mauro_codes  23 Jan 24 17:31 script.txt

## Change group ownership (the main.js file will now have "development" as a group)
mauro_codes@mauro-desktop:~/projects/landing-page$ sudo chown :development main.js

## Check group ownership again
mauro_codes@mauro-desktop:~/projects/landing-page$ ls
total 0
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 27 17:49 .
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 27 20:00 ..
---x--x--x 1 mauro_codes development 206 Jan 27 16:36 main.js
---x--x--x 1 mauro_codes mauro_codes  23 Jan 24 17:31 script.txt
Enter fullscreen mode Exit fullscreen mode

更改文件的用户和组所有权

输入此命令sudo chown [new-owner-name]:[new-group-name] [file-to-change-ownership]可同时更改文件的用户和组所有权。

## Check ownership
mauro_codes@mauro-desktop:~/projects/landing-page$ ls
total 0
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 27 17:49 .
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 27 20:00 ..
---x--x--x 1 mauro_codes mauro_codes 206 Jan 27 16:36 main.js
---x--x--x 1 mauro_codes mauro_codes  23 Jan 24 17:31 script.txt

## Change user and group ownership (the main.js file will now have "development" as a group and root as owner)
mauro_codes@mauro-desktop:~/projects/landing-page$ sudo chown root:development main.js

## Check ownership again
mauro_codes@mauro-desktop:~/projects/landing-page$ ls
total 0
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 27 17:49 .
drwxr-xr-x 1 mauro_codes mauro_codes 512 Jan 27 20:00 ..
---x--x--x 1 root        development 206 Jan 27 16:36 main.js
---x--x--x 1 mauro_codes mauro_codes  23 Jan 24 17:31 script.txt
Enter fullscreen mode Exit fullscreen mode

快捷方式

非常感谢@bogkonstantin@samuelabreu让我知道了这些我以前不知道的命令。

搜索您的命令历史记录

在上一篇文章中,我们看到可以通过输入命令来获取最近运行的所有命令的完整列表history。如果您不想查看完整列表,而是想快速搜索特定命令,请输入命令[CTRL] + r。系统会提示您输入几个字符以查找匹配的命令。您可以再次输入命令来遍历这些结果[CTRL] + r

浏览您的命令

您可以使用一些快捷方式来快速浏览命令:

  • 粘贴前几行 ➜[CTRL] + p
  • 将光标移至行首。➜[CTRL] + a
  • 将光标移至行尾。➜[CTRL] + e
  • 将光标向前移动一个字符。➜[CTRL] + f
  • 将光标向后移动一个字符。➜[CTRL] + b
  • 删除整行。➜[CTRL] + u
  • 删除最后一个输入的单词。➜[CTRL] + w

处理长文件

  • 打印文件的最后几行 ➜tail [name-of-the-file]
  • 打印文件的最后 n 行 ➜tail -n [number-of-lines] [name-of-the-file]
  • 打印文件的前几行 ➜head [name-of-the-file]
  • 打印文件的前 n 行 ➜head -n [number-of-lines] [name-of-the-file]
  • 翻阅文件 ➜less [name-of-the-file]

如果您希望我在最后一篇文章中介绍某个特定的命令,请告诉我!

文章来源:https://dev.to/mauro_codes/linux-terminal-the-ultimate-cheat-sheet-part-2-11ge