Linux Bash 命令101 个 Bash 命令和技巧,适合初学者到专家
基础知识
中间的
先进的
奖励:有趣但大多无用的东西
2019年9月25日更新:感谢ラナ・クアール的辛勤付出,本文现已提供日语版本。请点击下方链接查看他们的作品。如果您知道本文的其他语言版本,请告知我,我会在此处发布。
2019年7月8日更新:我最近在一个法语留言板上发现了一篇非常类似的文章,大约两年前就有人发过。如果你有兴趣学习一些shell命令——并且你会说法语——那么这篇文章可以作为我下面这篇文章的一个很好的补充。
直到大约一年前,我几乎只在 macOS 和 Ubuntu 操作系统上工作。在这两个操作系统上,bash都是我的默认 shell。在过去的六七年里,我对它的工作原理有了大致的了解bash,并想为刚入门的读者概述一些更常用/有用的命令。如果您认为自己已经了解了 的所有知识bash,请继续阅读下文——我提供了一些您可能忘记的提示和标志提醒,这可能会让您的工作更轻松一些。
以下命令以或多或少叙述的方式排列,因此如果您刚开始使用bash,可以从头到尾逐步完成。通常情况下,越到最后,常见命令就越少,难度也越大。
目录
基础知识
第一个命令,浏览文件系统
现代文件系统具有目录(文件夹)树,其中目录要么是根目录(没有父目录),要么是子目录(包含在一个单独的其他目录中,我们称之为其“父目录”)。反向遍历文件树(从子目录到父目录)始终会到达根目录。某些文件系统有多个根目录(例如 Windows 的驱动器:C:\、A:\等),但 Unix 和类 Unix 系统只有一个名为 的根目录\。
pwd / ls / cd
在文件系统中工作时,用户始终在某个目录中工作,我们称之为 当前目录 或工作目录。使用以下命令打印用户的工作目录pwd:
[ andrew@pc01 ~ ]$ pwd
/home/andrew
使用以下命令列出此目录的内容(文件和/或子目录等)ls:
[ andrew@pc01 ~ ]$ ls
Git TEST jdoc test test.file
奖金:
使用以下方式显示隐藏(“点”)文件
ls -a显示文件详细信息
ls -l组合多个标志,例如
ls -l -a有时你可以像
ls -la下面这样链接标志ls -l -a
cd使用(更改目录)更改为不同的目录:
[ andrew@pc01 ~ ]$ cd TEST/
[ andrew@pc01 TEST ]$ pwd
/home/andrew/TEST
[ andrew@pc01 TEST ]$ cd A
[ andrew@pc01 A ]$ pwd
/home/andrew/TEST/A
cd ..cd是“到父目录”的简写:
[ andrew@pc01 A ]$ cd ..
[ andrew@pc01 TEST ]$ pwd
/home/andrew/TEST
cd ~或者只是“到我的主目录”cd的简写(通常或类似的东西):cd/home/username
[ andrew@pc01 TEST ]$ cd
[ andrew@pc01 ~ ]$ pwd
/home/andrew
奖金:
cd ~user意思是“cd到user的主目录您可以使用
cd ../..等跳转多个目录级别。返回最近的目录
cd -
.是“此目录”的简写,因此cd .不会做太多的事情
; / && / &
我们在命令行中输入的内容称为命令,它们总是执行存储在计算机某处的一些机器码。有时,这些机器码是 Linux 内置命令,有时是应用程序,有时是你自己编写的代码。有时,我们会想连续运行一个命令。为此,我们可以使用;(分号):
[ andrew@pc01 ~ ]$ ls; pwd
Git TEST jdoc test test.file
/home/andrew
上面的分号表示我首先 ( ls) 列出工作目录的内容,然后 ( pwd) 打印其位置。另一个用于链接命令的有用工具是&&。使用&&,如果左侧命令失败,右侧命令将不会运行。;和&&可以在同一行中多次使用:
# whoops! I made a typo here!
[ andrew@pc01 ~ ]$ cd /Giit/Parser && pwd && ls && cd
-bash: cd: /Giit/Parser: No such file or directory
# the first command passes now, so the following commands are run
[ andrew@pc01 ~ ]$ cd Git/Parser/ && pwd && ls && cd
/home/andrew/Git/Parser
README.md doc.sh pom.xml resource run.sh shell.sh source src target
...但是;,即使第一个命令失败,第二个命令也会运行:
# pwd and ls still run, even though the cd command failed
[ andrew@pc01 ~ ]$ cd /Giit/Parser ; pwd ; ls
-bash: cd: /Giit/Parser: No such file or directory
/home/andrew
Git TEST jdoc test test.file
&看起来类似,&&但实际上功能完全不同。通常,当你执行一个长时间运行的命令时,命令行会等待该命令完成后才允许你输入另一个命令。将 放在&命令后面可以避免这种情况,并允许你在旧命令仍在执行时执行新命令:
[ andrew@pc01 ~ ]$ cd Git/Parser && mvn package & cd
[1] 9263
补充:当我们
&在命令后使用 来“隐藏”它时,我们说这个作业(或“进程”;这两个术语或多或少可以互换)是“后台运行”的。要查看当前正在运行的后台作业,请使用以下jobs命令:[ andrew@pc01 ~ ]$ jobs [1]+ Running cd Git/Parser/ && mvn package &
获取帮助
-h
在几乎任何命令后键入-h或--help即可调出该命令的帮助菜单:
[ andrew@pc01 ~ ]$ du --help
Usage: du [OPTION]... [FILE]...
or: du [OPTION]... --files0-from=F
Summarize disk usage of the set of FILEs, recursively for directories.
Mandatory arguments to long options are mandatory for short options too.
-0, --null end each output line with NUL, not newline
-a, --all write counts for all files, not just directories
--apparent-size print apparent sizes, rather than disk usage; although
the apparent size is usually smaller, it may be
larger due to holes in ('sparse') files, internal
fragmentation, indirect blocks, and the like
-B, --block-size=SIZE scale sizes by SIZE before printing them; e.g.,
'-BM' prints sizes in units of 1,048,576 bytes;
see SIZE format below
...
man
在几乎任何命令之前键入man以调出该命令的手册(使用 退出man)q:
LS(1) User Commands LS(1)
NAME
ls - list directory contents
SYNOPSIS
ls [OPTION]... [FILE]...
DESCRIPTION
List information about the FILEs (the current directory by default).
Sort entries alphabetically if none of -cftuvSUX nor --sort is speci-
fied.
Mandatory arguments to long options are mandatory for short options
too.
...
查看和编辑文件
head / tail / cat / less
head输出文件的前几行。-n标志指定显示的行数(默认为 10):
# prints the first three lines
[ andrew@pc01 ~ ]$ head -n 3 c
this
file
has
tail输出文件的最后几行。你可以像上面一样获取最后几行,也可以使用以下命令从第行n开始获取文件末尾的内容:Ntail -n +N
# prints the end of the file, beginning with the 4th line
[ andrew@pc01 ~ ]$ tail -n +4 c
exactly
six
lines
cat连接文件列表并将其发送到标准输出流(通常是终端)。cat它可以用于单个文件,也可以用于多个文件,并且常用于快速查看文件。(请注意:如果您cat以这种方式使用,您可能会被指控为“无用使用 Cat”(UUOC),但这不是什么大问题,所以不必太担心。)
[ andrew@pc01 ~ ]$ cat a
file a
[ andrew@pc01 ~ ]$ cat a b
file a
file b
less是另一个快速查看文件的工具——它会打开一个vim类似 的只读窗口。(没错,有一个名为 的命令more,但less——与直觉相反——它提供了 的功能超集,并且更推荐使用 。)请访问 less 和 more的页面more,了解更多关于 less 和 more 的信息(或者更少?)man。
nano / nedit
nano是一款极简的命令行文本编辑器。对于初学者或不想学习大量快捷键的人来说,它是一款非常棒的编辑器。在我编程生涯的最初几年里,它对我来说已经足够了(我现在才开始研究更强大的编辑器,主要是因为自定义语法高亮功能nano有点麻烦)。
nedit是一个小型的图形编辑器,它会打开一个 X Window,并支持点击编辑、拖放、语法高亮等功能。我nedit有时会用它来对脚本进行一些小的修改,然后反复运行。
其他常见的 CLI(命令行界面)/GUI(图形用户界面)编辑器包括、、、、emacsNotepad ++、Atom 等等。我试用过(并推荐)的一些好用的编辑器包括 Micro、Light Table 和 VS Code vi。vimgedit
所有现代编辑器都提供基本的便捷功能,例如搜索和替换、语法高亮等等。vi(m)和比和emacs拥有更多功能,但它们的学习曲线要陡峭得多。多尝试几款不同的编辑器,找到一款适合您的!nanonedit
创建和删除文件和目录
touch
touch它用于修改文件时间戳,但也可用于快速创建空文件。您可以使用文本编辑器打开它来创建新文件,例如nano:
[ andrew@pc01 ex ]$ ls
[ andrew@pc01 ex ]$ nano a
...编辑文件...
[ andrew@pc01 ex ]$ ls
a
...或者简单地使用touch:
[ andrew@pc01 ex ]$ touch b && ls
a b
奖金:
使用 ^z (Ctrl+z) 在后台运行进程
[ andrew@pc01 ex ]$ nano a...编辑文件,然后点击 ^z...
Use fg to return to nano [1]+ Stopped nano a [ andrew@pc01 ex ]$ fg...再次编辑文件...
双倍奖金:
在当前(前台)进程运行时按 ^c(Ctrl+c)终止它
使用命令显示的作业索引在
kill %N哪里终止后台进程Njobs
mkdir / rm / rmdir
mkdir用于创建新的空目录:
[ andrew@pc01 ex ]$ ls && mkdir c && ls
a b
a b c
您可以删除任何文件rm- 但请小心,这是不可恢复的!
[ andrew@pc01 ex ]$ rm a && ls
b c
您可以添加“您确定吗?”带有-i标志的提示:
[ andrew@pc01 ex ]$ rm -i b
rm: remove regular empty file 'b'? y
使用 删除空目录rmdir。如果你ls -a位于空目录中,你应该只会看到对目录本身的引用 ( .) 及其父目录的引用 ( ..):
[ andrew@pc01 ex ]$ rmdir c && ls -a
. ..
rmdir仅删除空目录:
[ andrew@pc01 ex ]$ cd .. && ls test/
*.txt 0.txt 1.txt a a.txt b c
[ andrew@pc01 ~ ]$ rmdir test/
rmdir: failed to remove 'test/': Directory not empty
rm -rf...但是您可以使用(-r= recursive, = force)删除目录及其所有内容-f:
[ andrew@pc01 ~ ]$ rm –rf test
移动和复制文件、创建链接、命令历史
mv / cp / ln
mv移动/重命名文件。您可以将mv文件移动到新目录并保持原文件名不变,或者将mv文件移动到“新文件”(重命名):
[ andrew@pc01 ex ]$ ls && mv a e && ls
a b c d
b c d e
cp复制文件:
[ andrew@pc01 ex ]$ cp e e2 && ls
b c d e e2
ln创建文件的硬链接:
# first argument to ln is TARGET, second is NEW LINK
[ andrew@pc01 ex ]$ ln b f && ls
b c d e e2 f
ln -s创建文件的软链接:
[ andrew@pc01 ex ]$ ln -s b g && ls
b c d e e2 f g
硬链接引用的是文件所在内存的实际字节,而软链接则引用原始文件名,而原始文件名本身指向这些字节。您可以在此处阅读有关软链接和硬链接的更多信息。
命令历史
bash有两个主要功能可以帮助你完成并重新运行命令,第一个是Tab 自动补全。只需输入命令的第一部分,按下 <tab> 键,终端就会猜测你要做什么:
[ andrew@pc01 dir ]$ ls <ENTER>
anotherlongfilename thisisalongfilename anewfilename
[ andrew@pc01 dir ]$ ls t <TAB>
...输入后按 TAB 键ls t,命令就完成了...
[ andrew@pc01 dir ]$ ls thisisalongfilename <ENTER>
thisisalongfilename
如果存在歧义,您可能需要多次按下 <TAB> :
[ andrew@pc01 dir ]$ ls a <TAB>
[ andrew@pc01 dir ]$ ls an <TAB>
anewfilename anotherlongfilename
bash保留您之前输入的命令的简短历史记录,并允许您通过输入 ^r (Ctrl+r) 来搜索这些命令:
[ andrew@pc01 dir ]
...按 ^r (Ctrl+r) 搜索命令历史记录...
(reverse-i-search)`':
...输入“anew”并找到包含此内容的最后一个命令...
(reverse-i-search)`anew': touch anewfilename
目录树、磁盘使用情况和进程
mkdir –p / tree
mkdir默认情况下,只会创建一个目录。这意味着,例如,如果目录d/e不存在,则d/e/f无法mkdir单独使用 来创建:
[ andrew@pc01 ex ]$ ls && mkdir d/e/f
a b c
mkdir: cannot create directory 'd/e/f': No such file or directory
但是如果我们将-p标志传递给mkdir,它将创建路径中的所有目录(如果它们尚不存在):
[ andrew@pc01 ex ]$ mkdir -p d/e/f && ls
a b c d
tree可以通过打印格式良好的目录树来帮助你更好地可视化目录结构。默认情况下,它会打印整个树结构(从指定的目录开始),但你可以使用以下标志将其限制为一定数量的层级-L:
[ andrew@pc01 ex ]$ tree -L 2
.
|-- a
|-- b
|-- c
`-- d
`--e
3 directories, 2 files
tree你可以使用 隐藏输出中的空目录。注意,这还会删除“递归空”目录,即本身--prune不为空但仅包含其他空目录或其他递归空目录的目录:
[ andrew@pc01 ex ]$ tree --prune
.
|-- a
`-- b
df / du / ps
df用于显示文件占用了磁盘或系统(硬盘等)多少空间。
[ andrew@pc01 ex ]$ df -h
Filesystem Size Used Avail Use% Mounted on
udev 126G 0 126G 0% /dev
tmpfs 26G 2.0G 24G 8% /run
/dev/mapper/ubuntu--vg-root 1.6T 1.3T 252G 84% /
...
在上面的命令中,-h并不意味着“帮助”,而是指“人类可读”。一些命令使用此约定来以K千字节、G千兆字节等为单位显示文件/磁盘大小,而不是写出一个巨大的整数字节数。
du显示特定目录及其子目录的文件空间使用情况。如果您想知道特定硬盘驱动器上有多少可用空间,请使用df;如果您想知道某个目录占用了多少空间,请使用du:
[ andrew@pc01 ex ]$ du
4 ./d/e/f
8 ./d/e
12 ./d
4 ./c
20 .
du带有一个--max-depth=N标志,该标志仅显示N指定目录以下(或更少)的目录级别:
[ andrew@pc01 ex ]$ du -h --max-depth=1
12K ./d
4.0K ./c
20K .
ps显示用户当前正在运行的所有进程(又称作业):
[ andrew@pc01 ex ]$ ps
PID TTY TIME CMD
16642 pts/15 00:00:00 ps
25409 pts/15 00:00:00 bash
各种各样的
passwd / logout / exit
使用 更改你的账户密码passwd。它会要求你输入当前密码进行验证,然后要求你输入两次新密码,以确保你不会输入任何错误:
[ andrew@pc01 dir ]$ passwd
Changing password for andrew.
(current) UNIX password: <type current password>
Enter new UNIX password: <type new password>
Retype new UNIX password: <type new password again>
passwd: password updated successfully
logout退出您已登录的 shell(您在其中拥有用户帐户):
[ andrew@pc01 dir ]$ logout
──────────────────────────────────────────────────────────────────────────────
Session stopped
- Press <return> to exit tab
- Press R to restart session
- Press S to save terminal output to file
exit退出任何类型的 shell:
[ andrew@pc01 ~ ]$ exit
logout
──────────────────────────────────────────────────────────────────────────────
Session stopped
- Press <return> to exit tab
- Press R to restart session
- Press S to save terminal output to file
clear / *
运行clear此命令可将当前终端行移至屏幕顶部。此命令只会在当前提示行下方添加空行。这有助于清理你的工作空间。
查找文件时使用 glob(*,又称 Kleene Star,又称通配符)。注意以下两个命令的区别:
[ andrew@pc01 ~ ]$ ls Git/Parser/source/
PArrayUtils.java PFile.java PSQLFile.java PWatchman.java
PDateTimeUtils.java PFixedWidthFile.java PStringUtils.java PXSVFile.java
PDelimitedFile.java PNode.java PTextFile.java Parser.java
[ andrew@pc01 ~ ]$ ls Git/Parser/source/PD*
Git/Parser/source/PDateTimeUtils.java Git/Parser/source/PDelimitedFile.java
glob 可以在一个命令中多次使用,并且匹配零个或多个字符:
[ andrew@pc01 ~ ]$ ls Git/Parser/source/P*D*m*
Git/Parser/source/PDateTimeUtils.java Git/Parser/source/PDelimitedFile.java
中间的
磁盘、内存和处理器使用情况
ncdu
ncdu(NCurses 磁盘使用情况)提供了文件空间使用情况的可导航概览,类似于改进的du。它会打开一个类似 的只读vim窗口(按下q退出):
[ andrew@pc01 ~ ]$ ncdu
ncdu 1.11 ~ Use the arrow keys to navigate, press ? for help
--- /home/andrew -------------------------------------------------------------
148.2 MiB [##########] /.m2
91.5 MiB [###### ] /.sbt
79.8 MiB [##### ] /.cache
64.9 MiB [#### ] /.ivy2
40.6 MiB [## ] /.sdkman
30.2 MiB [## ] /.local
27.4 MiB [# ] /.mozilla
24.4 MiB [# ] /.nanobackups
10.2 MiB [ ] .confout3.txt
8.4 MiB [ ] /.config
5.9 MiB [ ] /.nbi
5.8 MiB [ ] /.oh-my-zsh
4.3 MiB [ ] /Git
3.7 MiB [ ] /.myshell
1.7 MiB [ ] /jdoc
1.5 MiB [ ] .confout2.txt
1.5 MiB [ ] /.netbeans
1.1 MiB [ ] /.jenv
564.0 KiB [ ] /.rstudio-desktop
Total disk usage: 552.7 MiB Apparent size: 523.6 MiB Items: 14618
top / htop
top显示所有当前正在运行的进程及其所有者、内存使用情况等。htop是一个改进的交互式top。 (注意:您可以传递-u username标志以将显示的进程限制为仅所有者的进程username。)
[ andrew@pc01 ~ ]$ htop
1 [ 0.0%] 9 [ 0.0%] 17 [ 0.0%] 25 [ 0.0%]
2 [ 0.0%] 10 [ 0.0%] 18 [ 0.0%] 26 [ 0.0%]
3 [ 0.0%] 11 [ 0.0%] 19 [ 0.0%] 27 [ 0.0%]
4 [ 0.0%] 12 [ 0.0%] 20 [ 0.0%] 28 [ 0.0%]
5 [ 0.0%] 13 [ 0.0%] 21 [| 1.3%] 29 [ 0.0%]
6 [ 0.0%] 14 [ 0.0%] 22 [ 0.0%] 30 [| 0.6%]
7 [ 0.0%] 15 [ 0.0%] 23 [ 0.0%] 31 [ 0.0%]
8 [ 0.0%] 16 [ 0.0%] 24 [ 0.0%] 32 [ 0.0%]
Mem[||||||||||||||||||||1.42G/252G] Tasks: 188, 366 thr; 1 running
Swp[| 2.47G/256G] Load average: 0.00 0.00 0.00
Uptime: 432 days(!), 00:03:55
PID USER PRI NI VIRT RES SHR S CPU% MEM% TIME+ Command
9389 andrew 20 0 23344 3848 2848 R 1.3 0.0 0:00.10 htop
10103 root 20 0 3216M 17896 2444 S 0.7 0.0 5h48:56 /usr/bin/dockerd
1 root 20 0 181M 4604 2972 S 0.0 0.0 15:29.66 /lib/systemd/syst
533 root 20 0 44676 6908 6716 S 0.0 0.0 11:19.77 /lib/systemd/syst
546 root 20 0 244M 0 0 S 0.0 0.0 0:01.39 /sbin/lvmetad -f
1526 root 20 0 329M 2252 1916 S 0.0 0.0 0:00.00 /usr/sbin/ModemMa
1544 root 20 0 329M 2252 1916 S 0.0 0.0 0:00.06 /usr/sbin/ModemMa
F1Help F2Setup F3SearchF4FilterF5Tree F6SortByF7Nice -F8Nice +F9Kill F10Quit
REPL 和软件版本
REPL
REPL是一个读取-评估-打印循环,类似于命令行,但通常用于特定的编程语言。
您可以使用以下命令打开 Python REPL python(并使用以下命令退出quit()):
[ andrew@pc01 ~ ]$ python
Python 3.5.2 (default, Nov 12 2018, 13:43:14) ...
>>> quit()
使用以下命令打开 R REPL R(并使用以下命令退出q()):
[ andrew@pc01 ~ ]$ R
R version 3.5.2 (2018-12-20) --"Eggshell Igloo" ...
> q()
Save workspace image? [y/n/c]: n
使用以下命令打开 Scala REPL scala(并使用以下命令退出:quit):
[ andrew@pc01 ~ ]$ scala
Welcome to Scala 2.11.12 ...
scala> :quit
使用以下命令打开 Java REPL jshell(并使用以下命令退出/exit):
[ andrew@pc01 ~ ]$ jshell
| Welcome to JShell--Version 11.0.1 ...
jshell> /exit
或者,您可以使用 ^d (Ctrl+d) 退出任何这些 REPL。^d 是 Unix 上的 EOF(文件结束)标记,表示输入结束。
-version / --version / -v
大多数命令和程序都有一个-version或--version标志,用于显示该命令或程序的软件版本。大多数应用程序都可以轻松获取此信息:
[ andrew@pc01 ~ ]$ ls --version
ls (GNU coreutils) 8.25 ...
[ andrew@pc01 ~ ]$ ncdu -version
ncdu 1.11
[ andrew@pc01 ~ ]$ python --version
Python 3.5.2
...但有些不太直观:
[ andrew@pc01 ~ ]$ sbt scalaVersion
...
[info] 2.12.4
请注意,某些程序使用它-v作为版本标志,而其他程序使用-v它来表示“详细”,这将在运行应用程序的同时打印大量诊断或调试信息:
SCP(1) BSD General Commands Manual SCP(1)
NAME
scp -- secure copy (remote file copy program)
...
-v Verbose mode. Causes scp and ssh(1) to print debugging messages
about their progress. This is helpful in debugging connection,
authentication, and configuration problems.
...
环境变量和别名
环境变量
环境变量(有时缩写为“env vars”)是可以在 shell 中创建和使用的持久变量bash。它们用等号 ( =) 定义,用美元符号 ( $) 使用。您可以使用以下命令查看所有当前定义的环境变量printenv:
[ andrew@pc01 ~ ]$ printenv
SPARK_HOME=/usr/local/spark
TERM=xterm
...
使用符号设置一个新的环境变量=(但不要在 之前或之后添加任何空格=!):
[ andrew@pc01 ~ ]$ myvar=hello
echo使用和前缀符号将特定的环境变量打印到终端$:
[ andrew@pc01 ~ ]$ echo $myvar
hello
包含空格或其他空白符的环境变量应使用引号 ( "...") 括起来。请注意,重新赋值给环境变量会覆盖原有的值,且不会发出任何警告:
[ andrew@pc01 ~ ]$ myvar="hello, world!" && echo $myvar
hello, world!
环境变量也可以使用以下export命令定义。这样定义后,它们也可供子进程(从此shell调用的命令)使用:
[ andrew@pc01 ~ ]$ export myvar="another one" && echo $myvar
another one
您可以通过保留右侧空白=或使用以下unset命令来取消设置环境变量:
[ andrew@pc01 ~ ]$ unset mynewvar
[ andrew@pc01 ~ ]$ echo $mynewvar
别名
别名类似于环境变量,但通常以不同的方式使用——用较短的命令替换长命令:
[ andrew@pc01 apidocs ]$ ls -l -a -h -t
total 220K
drwxr-xr-x 5 andrew andrew 4.0K Dec 21 12:37 .
-rw-r--r-- 1 andrew andrew 9.9K Dec 21 12:37 help-doc.html
-rw-r--r-- 1 andrew andrew 4.5K Dec 21 12:37 script.js
...
[ andrew@pc01 apidocs ]$ alias lc="ls -l -a -h -t"
[ andrew@pc01 apidocs ]$ lc
total 220K
drwxr-xr-x 5 andrew andrew 4.0K Dec 21 12:37 .
-rw-r--r-- 1 andrew andrew 9.9K Dec 21 12:37 help-doc.html
-rw-r--r-- 1 andrew andrew 4.5K Dec 21 12:37 script.js
...
您可以使用以下命令删除别名unalias:
[ andrew@pc01 apidocs ]$ unalias lc
[ andrew@pc01 apidocs ]$ lc
The program 'lc' is currently not installed. ...
奖金:
基本bash脚本
bash脚本
bash脚本(通常以 结尾.sh)允许您自动化复杂的流程,并将它们打包成可重用的函数。bash脚本可以包含任意数量的常规 Shell 命令:
[ andrew@pc01 ~ ]$ echo "ls && touch file && ls" > ex.sh
source可以使用以下命令或以下命令执行shell脚本sh:
[ andrew@pc01 ~ ]$ source ex.sh
Desktop Git TEST c ex.sh project test
Desktop Git TEST c ex.sh file project test
可以使用以下命令使 Shell 脚本可执行chmod(稍后将详细介绍):
[ andrew@pc01 ~ ]$ echo "ls && touch file2 && ls" > ex2.sh
[ andrew@pc01 ~ ]$ chmod +x ex2.sh
可以通过在可执行 shell 脚本前添加以下内容来运行该脚本./:
[ andrew@pc01 ~ ]$ ./ex2.sh
Desktop Git TEST c ex.sh ex2.sh file project test
Desktop Git TEST c ex.sh ex2.sh file file2 project test
可以通过在命令末尾添加以下内容来拆分长代码行\:
[ andrew@pc01 ~ ]$ echo "for i in {1..3}; do echo \
> \"Welcome \$i times\"; done" > ex3.sh
Bash 脚本可以包含循环、函数等等!
[ andrew@pc01 ~ ]$ source ex3.sh
Welcome 1 times
Welcome 2 times
Welcome 3 times
自定义提示和ls
Bash 脚本可以让你的生活更加轻松,更加丰富多彩。查看这份很棒的 Bash 脚本速查表。
$PS1(提示字符串 1)是定义主 shell 提示符的环境变量(在此处了解其他提示符):
[ andrew@pc01 ~ ]$ printf "%q" $PS1
$'\\n\\[\E[1m\\]\\[\E[30m\\]\\A'$'\\[\E[37m\\]|\\[\E[36m\\]\\u\\[\E[37m\\]@\\[\E[34m\\]\\h'$'\\[\E[32m\\]\\W\\[\E[37m\\]|'$'\\[\E(B\E[m\\]‘
您可以使用以下命令更改默认提示export:
[ andrew@pc01 ~ ]$ export PS1="\ncommand here> "
command here> echo $PS1
\ncommand here>
...您也可以添加颜色!:
command here> export PS1="\e[1;31m\nCODE: \e[39m"
# (this should be red, but it may not show up that way in Markdown)
CODE: echo $PS1
\e[1;31m\nCODE: \e[39m
ls您还可以通过编辑环境变量来更改显示的颜色$LS_COLORS:
# (again, these colours might not show up in Markdown)
CODE: ls
Desktop Git TEST c ex.sh ex2.sh ex3.sh file file2 project test
CODE: export LS_COLORS='di=31:fi=0:ln=96:or=31:mi=31:ex=92‘
CODE: ls
Desktop Git TEST c ex.sh ex2.sh ex3.sh file file2 project test
配置文件
配置文件 /.bashrc
如果您尝试了上一节中的命令,然后注销并重新登录,您可能会注意到您的更改消失了。config (配置)文件允许您在每次登录(或运行该程序)时维护 Shell 或特定程序的设置。Shell 的主要配置文件bash是 .config~/.bashrc文件。添加到 .config 中的别名、环境变量和函数~/.bashrc在您每次登录时都可用。 .config 中的命令~/.bashrc将在每次登录时运行。
如果您编辑了~/.bashrc文件,则可以使用以下命令重新加载它而无需注销source:
[ andrew@pc01 ~ ]$ nano ~/.bashrc
...将该行添加echo “~/.bashrc loaded!”到文件顶部...
[ andrew@pc01 ~ ]$ source ~/.bashrc
~/.bashrc loaded!
...注销并重新登录...
Last login: Fri Jan 11 10:29:07 2019 from 111.11.11.111
~/.bashrc loaded!
[ andrew@pc01 ~ ]
贝壳的种类
登录Shell 是指需要登录(即拥有用户名)的 Shell。交互式Shell 是指接受命令的 Shell。Shell 可以是登录式交互式 Shell、非登录式非交互式 Shell,或者其他任何组合。
除了 之外~/.bashrc,还有一些其他脚本会sourced在您登录或注销时自动由 Shell 运行。这些脚本包括:
/etc/profile~/.bash_profile~/.bash_login~/.profile~/.bash_logout/etc/bash.bash_logout
这些脚本的执行顺序以及执行哪个脚本取决于所打开的 shell 类型。有关更多信息,请参阅bash 手册页和Stack Overflow 上的帖子。
请注意,bash脚本可以引用source其他脚本。例如,在你的 中~/.bashrc,你可以包含以下行:
source ~/.bashrc_addl
…这也将包含source该.bashrc_addl脚本。该文件可以包含其自身的别名、函数、环境变量等等。反过来,它source也可以包含其他脚本。(小心避免无限循环地获取脚本资源!)
根据功能或机器类型(Ubuntu、Red Hat 和 macOS)将命令拆分为不同的 shell 脚本可能会有所帮助,例如:
~/.bash_ubuntu-- 特定于基于 Ubuntu 的机器的配置~/.bashrc_styles-- 美学设置,例如PS1和LS_COLORS~/.bash_java-- Java 语言特有的配置
我尝试将bash美观的配置和特定于操作系统或机器的代码保存在单独的文件中,然后我有一个大bash文件,其中包含我在每台机器和每个操作系统上使用的快捷方式等。
请注意, shell也有很多种。bash只是其中一种 shell(“Bourne Again Shell”)。其他常见的 shell 包括zsh、csh、fish等等。您可以尝试不同的 shell,找到适合您的 shell。但请注意,本教程bash仅包含 shell 命令,并非此处列出的所有内容(甚至可能全部都不适用)都适用于 以外的 shell bash。
寻找事物
whereis / which / whatis
whereis搜索与特定命令相关的“可能有用”的文件。它将尝试返回该man命令的二进制文件(可执行机器码)、源代码(代码源文件)和页面的位置:
[ andrew@pc01 ~ ]$ whereis ls
ls: /bin/ls /usr/share/man/man1/ls.1.gz
which只会返回二进制文件的位置(命令本身):
[ andrew@pc01 ~ ]$ which ls
/bin/ls
whatis从页面打印出命令的单行描述man:
[ andrew@pc01 ~ ]$ whatis whereis which whatis
whereis (1) - locate the binary, source, and manual page files for a command
which (1) - locate a command
whatis (1) - display one-line manual page descriptions
which对于查找可能被别名隐藏的命令的“原始版本”很有用:
[ andrew@pc01 ~ ]$ alias ls="ls -l"
# “original” ls has been “hidden” by the alias defined above
[ andrew@pc01 ~ ]$ ls
total 36
drwxr-xr-x 2 andrew andrew 4096 Jan 9 14:47 Desktop
drwxr-xr-x 4 andrew andrew 4096 Dec 6 10:43 Git
...
# but we can still call “original” ls by using the location returned by which
[ andrew@pc01 ~ ]$ /bin/ls
Desktop Git TEST c ex.sh ex2.sh ex3.sh file file2 project test
locate / find
locate通过参考半定期更新的缓存文件列表,在系统的任何位置查找文件:
[ andrew@pc01 ~ ]$ locate README.md
/home/andrew/.config/micro/plugins/gotham-colors/README.md
/home/andrew/.jenv/README.md
/home/andrew/.myshell/README.md
...
由于它只是搜索列表,locate因此通常比替代方案更快,find.find会遍历文件系统来查找您要查找的文件。然而,由于它实际上查看的是系统上当前存在的文件,因此它始终会返回最新的文件列表,而 . 则不一定如此locate。
[ andrew@pc01 ~ ]$ find ~/ -iname "README.md"
/home/andrew/.jenv/README.md
/home/andrew/.config/micro/plugins/gotham-colors/README.md
/home/andrew/.oh-my-zsh/plugins/ant/README.md
...
find是为 1971 年的第一个 Unix 版本编写的,因此比locate1994 年添加到 GNU 的 更为广泛地普及。
find具有比更多的功能locate,并且可以按文件年龄、大小、所有权、类型、时间戳、权限、文件系统内的深度进行搜索;find可以使用正则表达式进行搜索,对找到的文件执行命令等等。
当你需要快速(但可能已过时)的文件列表,或者你不确定特定文件位于哪个目录时,请使用locate。当你需要准确的文件列表(可能基于文件名以外的其他内容),并且需要对这些文件执行某些操作时,请使用find。
下载内容
ping / wget / curl
ping尝试与网络主机建立通信线路。主要用于检查网络连接是否中断:
[ andrew@pc01 ~ ]$ ping google.com
PING google.com (74.125.193.100) 56(84) bytes of data.
Pinging 74.125.193.100 with 32 bytes of data:
Reply from 74.125.193.100: bytes=32 time<1ms TTL=64
...
wget用于轻松地从互联网上下载文件:
[ andrew@pc01 ~ ]$ wget \
> http://releases.ubuntu.com/18.10/ubuntu-18.10-desktop-amd64.iso
curl可以像这样使用wget(不要忘记--output标志):
[ andrew@pc01 ~ ]$ curl \
> http://releases.ubuntu.com/18.10/ubuntu-18.10-desktop-amd64.iso \
> --output ubuntu.iso
curl并且wget各有优缺点。curl支持更多的协议并且比更广泛地可用wget;curl还可以发送数据,而wget只能接收数据。wget可以递归下载文件,而curl不能。
一般来说,wget当我需要从互联网下载东西时,我会使用它。我并不经常使用 发送数据curl,但偶尔需要用到它时,最好还是注意一下。
apt / gunzip / tar / gzip
Debian 家族的 Linux 发行版有一个非常棒的软件包管理工具,叫做apt。它可以用来在你的机器上安装、升级或删除软件。要搜索apt某个特定的软件,请使用apt search,然后使用 进行安装apt install:
[ andrew@pc01 ~ ]$ apt search bleachbit
...bleachbit/bionic,bionic 2.0-2 all
delete unnecessary files from the system
# you need to 'sudo' to install software
[ andrew@pc01 ~ ]$ sudo apt install bleachbit
.tar.gzLinux 软件通常以tarball 文件的形式打包:
[ andrew@pc01 ~ ]$ wget \
> https://github.com/atom/atom/releases/download/v1.35.0-beta0/atom-amd64.tar.gz
...这些类型的文件可以使用以下命令解压缩gunzip:
[ andrew@pc01 ~ ]$ gunzip atom-amd64.tar.gz && ls
atom-amd64.tar
一个.tar.gz文件将被gunzip-ped 到一个.tar文件,可以使用tar -xf(-x“extract” 表示-f“untar” 指定文件)将其提取到文件目录中:
[ andrew@pc01 ~ ]$ tar -xf atom-amd64.tar && mv \
atom-beta-1.35.0-beta0-amd64 atom && ls
atom atom-amd64.tar
要反向操作,您可以-c从目录创建()一个 tar 文件,然后使用以下命令对其进行压缩(或根据需要对其进行解压缩)-z:
[ andrew@pc01 ~ ]$ tar -zcf compressed.tar.gz atom && ls
atom atom-amd64.tar compressed.tar.gz
.tar文件也可以使用以下方式压缩gzip:
[ andrew@pc01 ~ ]$ gzip atom-amd64.tar && ls
atom atom-amd64.tar.gz compressed.tar.gz
重定向输入和输出
| / > / < / echo / printf
默认情况下,shell 命令从标准输入流(又名 stdin 或 0)读取其输入并写入标准输出流(又名 stdout 或 1),除非出现错误,否则会写入标准错误流(又名 stderr 或 2)。
echo默认情况下将文本写入标准输出,大多数情况下只会将其打印到终端:
[ andrew@pc01 ~ ]$ echo "hello"
hello
管道运算符|将第一个命令的输出重定向到第二个命令的输入:
# 'wc' (word count) returns the number of lines, words, bytes in a file
[ andrew@pc01 ~ ]$ echo "example document" | wc
1 2 17
>将输出从 stdout 重定向到特定位置
[ andrew@pc01 ~ ]$ echo "test" > file && head file
test
printf是一种改进的echo,允许格式化和转义序列:
[ andrew@pc01 ~ ]$ printf "1\n3\n2"
1
3
2
<从特定位置而不是标准输入获取输入:
# 'sort' sorts the lines of a file alphabetically / numerically
[ andrew@pc01 ~ ]$ sort <(printf "1\n3\n2")
1
2
3
与UUOC相比,推荐使用 来将文件内容发送给命令<。注意,这会导致数据在命令行上从右到左“流动”,而不是(对英语使用者来说,这可能更自然)从左到右:
[ andrew@pc01 ~ ]$ printf "1\n3\n2" > file && sort < file
1
2
3
0 / 1 / 2 / tee
0、1 和 2 分别是标准输入、标准输出和标准错误流。输入和输出流可以使用前面提到的 、 和 操作符进行重定向|,>但<stdin、stdout 和 stderr 也可以直接使用它们的数字标识符进行操作:
>&1使用或写入 stdout 或 stderr >&2:
[ andrew@pc01 ~ ]$ cat test
echo "stdout" >&1
echo "stderr" >&2
默认情况下,stdout 和 stderr 都会将输出打印到终端:
[ andrew@pc01 ~ ]$ ./test
stderr
stdout
将 stdout 重定向到/dev/null(仅打印发送到 stderr 的输出):
[ andrew@pc01 ~ ]$ ./test 1>/dev/null
stderr
将 stderr 重定向到/dev/null(仅打印发送到 stdout 的输出):
[ andrew@pc01 ~ ]$ ./test 2>/dev/null
stdout
重定向所有输出至/dev/null(不打印任何内容):
[ andrew@pc01 ~ ]$ ./test &>/dev/null
使用以下命令将输出发送到 stdout 和任意数量的其他位置tee:
[ andrew@pc01 ~ ]$ ls && echo "test" | tee file1 file2 file3 && ls
file0
test
file0 file1 file2 file3
先进的
超级用户
sudo / su
您可以使用以下命令检查您的用户名whoami:
[ andrew@pc01 abc ]$ whoami
andrew
...并以另一个用户身份运行命令sudo -u username(您将需要该用户的密码):
[ andrew@pc01 abc ]$ sudo -u test touch def && ls -l
total 0
-rw-r--r-- 1 test test 0 Jan 11 20:05 def
如果–u没有提供,则默认用户是超级用户(通常称为“root”),具有无限权限:
[ andrew@pc01 abc ]$ sudo touch ghi && ls -l
total 0
-rw-r--r-- 1 test test 0 Jan 11 20:05 def
-rw-r--r-- 1 root root 0 Jan 11 20:14 ghi
使用su暂时成为另一个用户(并exit切换回来):
[ andrew@pc01 abc ]$ su test
Password:
test@pc01:/home/andrew/abc$ whoami
test
test@pc01:/home/andrew/abc$ exit
exit
[ andrew@pc01 abc ]$ whoami
andrew
!!
超级用户(通常是“root”)是唯一可以安装软件、创建用户等权限的人。有时很容易忘记这一点,从而导致出现错误:
[ andrew@pc01 ~ ]$ apt install ruby
E: Could not open lock file /var/lib/dpkg/lock-frontend - open (13: Permission denied)
E: Unable to acquire the dpkg frontend lock (/var/lib/dpkg/lock-frontend), are you root?
您可以重新输入该命令并sudo在其前面添加(以超级用户身份运行):
[ andrew@pc01 ~ ]$ sudo apt install ruby
Reading package lists...
或者,您可以使用!!快捷方式,它保留上一个命令:
[ andrew@pc01 ~ ]$ apt install ruby
E: Could not open lock file /var/lib/dpkg/lock-frontend - open (13: Permission denied)
E: Unable to acquire the dpkg frontend lock (/var/lib/dpkg/lock-frontend), are you root?
[ andrew@pc01 ~ ]$ sudo !!
sudo apt install ruby
Reading package lists...
默认情况下,使用 运行命令sudo(并正确输入密码)允许用户在接下来的 15 分钟内运行超级用户命令。15 分钟后,如果用户尝试运行受限制的命令,系统将再次提示用户输入超级用户密码。
文件权限
文件权限
文件可能被不同的用户或用户组读取 ( r)、写入 ( w) 和/或执行 ( x),或者完全不被执行。文件权限可以通过以下ls -l命令查看,它们由 10 个字符表示:
[ andrew@pc01 ~ ]$ ls -lh
total 8
drwxr-xr-x 4 andrew andrew 4.0K Jan 4 19:37 tast
-rwxr-xr-x 1 andrew andrew 40 Jan 11 16:16 test
-rw-r--r-- 1 andrew andrew 0 Jan 11 16:34 tist
每行的第一个字符代表文件的类型(d= 目录、l= 链接、-= 常规文件,等等);接下来是三组字符,每组三个字符,分别代表文件所有者 (u) 的权限、文件所有者组 (g) 的权限,以及其他用户 (o) 的权限。(这串字符后面的数字表示文件系统中指向该文件的链接数(上文中为 4 或 1)。)
r表示该人/那些人具有读取权限,w表示具有写入权限,x表示具有执行权限。如果目录是“可执行的”,则表示可以打开该目录并列出其内容。这三个权限通常用一个三位数字表示,如果x启用了 ,则数字加 1;如果w启用了 ,则数字加 2;如果r启用了 ,则数字加 4。请注意,这些数字相当于二进制数字(例如r-x-> 101-> 5)。因此,上述三个文件的权限分别为 755、755 和 644。
每个列表中接下来的两个字符串分别是所有者的名称(andrew在本例中为 )和所有者的组(andrew在本例中也为 )。然后是文件的大小、最近修改时间和名称。该–h标志使输出更易于阅读(即打印4.0K而不是4096字节)。
chmod / chown
可以chmod通过设置访问位来修改文件权限:
[ andrew@pc01 ~ ]$ chmod 777 test && chmod 000 tist && ls -lh
total 8.0K
drwxr-xr-x 4 andrew andrew 4.0K Jan 4 19:37 tast
-rwxrwxrwx 1 andrew andrew 40 Jan 11 16:16 test
---------- 1 andrew andrew 0 Jan 11 16:34 tist
...或者通过添加(+)或删除(-)r,w和x带有标志的权限:
[ andrew@pc01 ~ ]$ chmod +rwx tist && chmod -w test && ls -lh
chmod: test: new permissions are r-xrwxrwx, not r-xr-xr-x
total 8.0K
drwxr-xr-x 4 andrew andrew 4.0K Jan 4 19:37 tast
-r-xrwxrwx 1 andrew andrew 40 Jan 11 16:16 test
-rwxr-xr-x 1 andrew andrew 0 Jan 11 16:34 tist
可以使用以下命令更改拥有文件的用户chown:
[ andrew@pc01 ~ ]$ sudo chown marina test
可以使用以下命令更改拥有文件的组chgrp:
[ andrew@pc01 ~ ]$ sudo chgrp hadoop tist && ls -lh
total 8.0K
drwxr-xr-x 4 andrew andrew 4.0K Jan 4 19:37 tast
-----w--w- 1 marina andrew 40 Jan 11 16:16 test
-rwxr-xr-x 1 andrew hadoop 0 Jan 11 16:34 tist
用户和组管理
用户
users显示当前登录的所有用户。请注意,如果用户通过多个会话连接,则可以多次登录ssh。
[ andrew@pc01 ~ ]$ users
andrew colin colin colin colin colin krishna krishna
要查看所有用户(甚至未登录的用户),请选中/etc/passwd。(警告:请勿修改此文件!您可能会损坏您的用户帐户并导致无法登录系统。)
[ andrew@pc01 ~ ]$ alias au="cut -d: -f1 /etc/passwd \
> | sort | uniq" && au
_apt
anaid
andrew...
添加用户useradd:
[ andrew@pc01 ~ ]$ sudo useradd aardvark && au
_apt
aardvark
anaid...
使用以下命令删除用户userdel:
[ andrew@pc01 ~ ]$ sudo userdel aardvark && au
_apt
anaid
andrew...
使用 更改用户的默认 shell、用户名、密码或组成员身份usermod。
团体
groups显示当前用户所属的所有组:
[ andrew@pc01 ~ ]$ groups
andrew adm cdrom sudo dip plugdev lpadmin sambashare hadoop
要查看系统上的所有组,请检查/etc/group。(除非您知道自己在做什么,否则请不要修改此文件。)
[ andrew@pc01 ~ ]$ alias ag=“cut -d: -f1 /etc/group \
> | sort” && ag
adm
anaid
andrew...
添加一个组groupadd:
[ andrew@pc01 ~ ]$ sudo groupadd aardvark && ag
aardvark
adm
anaid...
使用以下命令删除组groupdel:
[ andrew@pc01 ~ ]$ sudo groupdel aardvark && ag
adm
anaid
andrew...
文本处理
uniq / sort / diff / cmp
uniq可以打印唯一的行(默认)或重复的行:
[ andrew@pc01 man ]$ printf "1\n2\n2" > a && \> printf "1\n3\n2" > b
[ andrew@pc01 man ]$ uniq a
1
2
sort将按字母/数字顺序对行进行排序:
[ andrew@pc01 man ]$ sort b
1
2
3
diff将报告两个文件之间的哪些行不同:
[ andrew@pc01 man ]$ diff a b
2c2
< 2
---
> 3
cmp报告两个文件之间的字节差异:
[ andrew@pc01 man ]$ cmp a b
a b differ: char 3, line 2
cut / sed
cut通常用于根据某些分隔符将一行拆分成几段(适合 CSV 处理)。-d指定分隔符并-f指定要打印的字段索引(第一个字段从 1 开始):
[ andrew@pc01 man ]$ printf "137.99.234.23" > c
[ andrew@pc01 man ]$ cut -d'.' c -f1
137
sed通常用于将文件中的某个字符串替换为另一个字符串:
[ andrew@pc01 man ]$ echo "old" | sed s/old/new/
new
……但它sed是一个非常强大的实用程序,无法在此进行恰当的概括。它实际上是图灵完备的,因此其他编程语言能做到的它都能做到。它sed可以根据正则表达式进行查找和替换,选择性地打印文件中匹配或包含特定模式的行,以非交互方式就地编辑文本文件等等。
一些不错的教程sed包括:
- https://www.tutorialspoint.com/sed/
- http://www.grymoire.com/Unix/Sed.html
- https://www.computerhope.com/unix/used.htm
模式匹配
grep
该名称grep来自g// (全局搜索常规表达式并打印它re);它用于在文件中查找文本。pgrep
grep用于查找文件中与某些模式匹配的行:
[ andrew@pc01 ~ ]$ grep -e ".*fi.*" /etc/profile
# /etc/profile: system-wide .profile file for the Bourne shell (sh(1))
# The file bash.bashrc already sets the default PS1.
fi
fi
...
...或包含一些词:
[ andrew@pc01 ~ ]$ grep "andrew" /etc/passwd
andrew:x:1000:1000:andrew,,,:/home/andrew:/bin/bash
grep如果您打算允许其他程序处理这些行(或者您只是想查看它们),通常是简单地在文件中查找匹配行的首选。
grep允许 ( -E) 使用扩展正则表达式、( -F) 一次匹配多个字符串中的任意一个,以及 ( -r) 以递归方式搜索目录中的文件。这些标志过去曾作为单独的命令实现(分别为egrep、fgrep和rgrep),但现在这些命令已被弃用。
awk
awk是一种围绕读取和操作分隔数据文件(如 CSV 文件)构建的模式匹配语言。
根据经验法则,grep适合在文件中查找字符串和模式,sed适合在文件中一对一替换字符串,awk适合从文件中提取字符串和模式并进行分析。
作为可以执行的操作的一个例子awk,这里有一个包含两列数据的文件:
[ andrew@pc01 ~ ]$ printf "A 10\nB 20\nC 60" > file
循环遍历各行,将数字添加到总和中,增加计数,打印平均值:
[ andrew@pc01 ~ ]$ awk 'BEGIN {sum=0; count=0; OFS=" "} {sum+=$2; count++} END {print "Average:", sum/count}' file
Average: 30
sed和awk都是图灵完备的语言。关于它们都有不少书籍。它们在模式匹配和文本处理方面非常有用。我实在没有足够的篇幅来全面介绍它们。去阅读更多关于它们的内容吧!
奖励:了解、和之间的一些区别
sedgrepawk
复制文件ssh
ssh / scp
ssh基于 Unix 的机器如何通过网络相互连接:
[ andrew@pc01 ~ ]$ ssh –p <port> andrew@137.xxx.xxx.89
Last login: Fri Jan 11 12:30:52 2019 from 137.xxx.xxx.199
请注意,由于我现在在另一台机器上,因此我的提示已经发生了变化:
[ andrew@pc02 ~ ]$ exit
logout
Connection to 137.xxx.xxx.89 closed.
在机器1上创建文件:
[ andrew@pc01 ~ ]$ echo "hello" > hello
将其复制到机器 2,使用scp(安全复制;注意,scp使用–P端口号,ssh使用–p)
[ andrew@pc01 ~ ]$ scp –P <port> hello andrew@137.xxx.xxx.89:~
hello 100% 0 0.0KB/s 00:00
ssh进入机器 2:
[ andrew@pc02 ~ ]$ ssh –p <port> andrew@137.xxx.xxx.89
Last login: Fri Jan 11 22:47:37 2019 from 137.xxx.xxx.79
文件就在那里!
[ andrew@pc02 ~ ]$ ls
hello multi xargs
[ andrew@pc02 ~ ]$ cat hello
hello
rsync
rsync是一种文件复制工具,它通过查找文件之间的增量(更改)来最大限度地减少复制的数据量。
假设我们有两个目录:d,其中包含一个文件,以及s,其中包含两个文件:
[ andrew@pc01 d ]$ ls && ls ../s
f0
f0 f1
使用以下方式同步目录(仅复制丢失的数据)rsync:
[ andrew@pc01 d ]$ rsync -av ../s/* .
sending incremental file list...
d现在包含以下所有文件s:
[ andrew@pc01 d ]$ ls
f0 f1
rsyncssh也可以执行:
[ andrew@pc02 r ]$ ls
[ andrew@pc02 r ]$ rsync -avz -e "ssh -p <port>" andrew@137.xxx.xxx.79:~/s/* .
receiving incremental file list
f0
f1
sent 62 bytes received 150 bytes 141.33 bytes/sec
total size is 0 speedup is 0.00
[ andrew@pc02 r ]$ ls
f0 f1
长时间运行的流程
yes / nohup / ps / kill
有时,ssh连接可能会由于网络或硬件问题而断开。通过该连接初始化的任何进程都将被“挂起”并终止。使用 运行命令可以nohup确保在 shell 关闭或网络连接失败时,该命令不会被挂起。
运行yes(持续输出“y”直到被杀死)nohup:
[ andrew@pc01 ~ ]$ nohup yes &
[1] 13173
ps显示当前用户的进程列表(注意 PID 号 13173):
[ andrew@pc01 ~ ]$ ps | sed -n '/yes/p'
13173 pts/10 00:00:12 yes
...注销并重新登录此 shell...
该进程已从中消失ps!
[ andrew@pc01 ~ ]$ ps | sed -n '/yes/p'
但它仍然出现在top并htop输出中:
[ andrew@pc01 ~ ]$ top -bn 1 | sed -n '/yes/p'
13173 andrew 20 0 4372 704 636 D 25.0 0.0 0:35.99 yes
-9使用以下方式终止该进程:
[ andrew@pc01 ~ ]$ kill -9 13173
它不再出现在中top,因为它已被杀死:
[ andrew@pc01 ~ ]$ top -bn 1 | sed -n '/yes/p'
cron / crontab / >>
cron提供一种自动执行常规、计划任务的简便方法。
您可以cron使用(打开文本编辑器)编辑您的作业crontab –e。附加以下行:
* * * * * date >> ~/datefile.txt
这将date每分钟运行一次命令,并将输出(使用>>操作员)附加到文件:
[ andrew@pc02 ~ ]$ head ~/datefile.txt
Sat Jan 12 14:37:01 GMT 2019
Sat Jan 12 14:38:01 GMT 2019
Sat Jan 12 14:39:01 GMT 2019...
只需从crontab文件中删除该行即可停止作业运行。cron作业可以设置为在每小时的特定分钟(0-59)、每天的特定小时(0-23)、每月的特定日期(1-31)、每年的特定月份(1-12)或每周的特定日期(0-6,周日至周六)运行。以上命令开头的五颗星分别代表了这些值。将它们替换为具体的数字,即可在特定日期或时间运行它们。
如果要运行某个作业,例如不受星期几的影响,那么表示星期几的位置(第 5 位)应该包含一个星号 ( *)。这就是为什么上面的命令每分钟(可用的最小间隔)运行一次。cron可以通过替换星号/数字,将作业设置为仅在系统重启时运行@reboot。作业还可以设置为每小时或每天运行特定次数,或者每小时/每天/每周/每月等多个特定时间运行。
各种各样的
pushd / popd
使用pushd和popd来维护目录堆栈,而不是cd到处使用-ing。
从home目录开始——这将是我们的“堆栈”中的底部目录:
[ andrew@pc01 ~ ]$ pwd
/home/andrew
移动到这个具有长名称的目录,使用以下命令将其“推送”到堆栈上pushd:
[ andrew@pc01 ~ ]$ pushd /etc/java/security/security.d/
/etc/java/security/security.d ~
移动到第三个目录并将其添加到堆栈:
[ andrew@pc01 security.d ]$ pushd ~/test/
~/test /etc/java/security/security.d ~
当一个新目录添加到堆栈时,它会被添加到 打印列表的左侧pushd。要“弹出”顶部目录(返回到我们添加的最新目录),我们可以使用popd命令。
从顶层目录中弹出,移动到堆栈中的下一个目录popd:
[ andrew@pc01 test ]$ popd
/etc/java/security/security.d ~
[ andrew@pc01 security.d ]$ pwd
/etc/java/security/security.d
从堆栈中弹出另一个目录,我们回到开始的地方:
[ andrew@pc01 security.d ]$ popd
~
[ andrew@pc01 ~ ]$ pwd
/home/andrew
xdg-open
xdg-open使用默认应用程序(可能是 GUI 程序)打开文件。这是一个非常有用的工具,可以从命令行打开 HTML 文档。它相当于 macOS 的open命令:
[ andrew@pc01 security.d ]$ xdg-open index.html
xargs
xargs对命令进行矢量化,在循环中对任意数量的参数运行它们。
ls此目录、其父目录及其祖父目录:
[ andrew@pc01 ~ ]$ export lv=".\n..\n../.."
[ andrew@pc01 ~ ]$ printf $lv | xargs ls
.:
multi file
..:
anaid andrew colin...
../..:
bin dev index...
可以通过带有标志的命令链来运行参数–I。
pwdcd首先进入该目录、其父目录和其祖父目录:
[ andrew@pc01 ~ ]$ printf $lv | xargs -I % sh -c 'cd %; pwd %'
/home/andrew
/home
/
奖励:有趣但大多无用的东西
w / write / wall / lynx
w更详细地who显示谁登录了以及他们在做什么:
[ andrew@pc01 ~ ]$ w
17:32:42 up 434 days, 3:11, 8 users, load average: 2.32, 2.46, 2.57
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
colin pts/9 137.xx.xx.210 03Jan19 5:28m 1:12 0.00s sshd: colin [priv]
andrew pts/10 137.xx.xx.199 11:05 1.00s 0.15s 0.04s sshd: andrew [priv]
colin pts/12 137.xx.xx.210 03Jan19 34:32 1.59s 1.59s –bash
...
write向特定用户发送消息:
[ andrew@pc01 ~ ]$ echo "hello" | write andrew pts/10
Message from andrew@pc01 on pts/10 at 17:34 ...
hello
EOF
wall与类似write,但它向每个登录用户发送相同的消息。write并且wall在电子邮件、Twitter、WhatsApp 和即时通讯出现之前更有用。
lynx是一款功能齐全的基于文本的网络浏览器:
nautilus / date / cal / bc
nautilus初始化 GUI 远程桌面会话并打开文件浏览器。
date显示当前日期和时间:
[ andrew@pc01 ~ ]$ date
Fri Jan 11 17:40:30 GMT 2019
cal显示本月的 ASCII 日历,其中突出显示今天的日期:
[ andrew@pc01 ~ ]$ cal
January 2019
Su Mo Tu We Th Fr Sa
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
bc是一个基本的算术计算器(使用 Python 代替):
[ andrew@pc01 ~ ]$ bc
bc 1.06.95 ...
20/4
5
暂时就这些!如果您知道任何额外的功能或酷炫的命令,请告诉我,我应该添加到这个列表中。另外,如果您发现任何拼写错误或错误信息,也请告诉我。我已经尽力检查了所有内容,但这里还是有很多内容!
如果您喜欢这篇文章,请考虑给我买杯咖啡来支持我的工作!
文章来源:https://dev.to/awwsmm/101-bash-commands-and-tips-for-beginners-to-experts-30je
后端开发教程 - Java、Spring Boot 实战 - msg200.com

