关于linux:LinuxThe-Command-Line-Handbook-读书笔记上半部分

10次阅读

共计 36519 个字符,预计需要花费 92 分钟才能阅读完成。

【Linux】《The Command Line Handbook》读书笔记(上半局部)

前言

这篇博客介绍的命令的确比拟多,并且受到平台字数影响,拆分为高低两个局部。

与其说是读书笔记,实际上更像是学学英语顺带学学 Linux 命令。这本书是通过每天挤一点工夫边看边学边敲实现的,每天日常翻翻所以程序和原始博客网站长处不一样。

集体起初看这个“书”的时候 PDF 还是收费凋谢的,不过当初博主曾经转为免费了,然而在线版本仍然收费对外公开。

此外英文博主还出过很多小而美的初级教程,英文用词和语法都比较简单,非常适合像我一样词汇量单薄的人看看。

这个教程对于一些简略的 Linux 入门是用是十分足够,话不多说咱们间接开始。

英文博客网址

网址:https://thevalleyofcode.com/linux/#54-traceroute

在最近的更新当中,作者改名并且把书名改为了《The Command Line Handbook》,在线网址如下:flaviocopes.com

介绍

Linux 是操作系统,和 MacOs 和 Windows 没什么区别。Linux“外围”(称为内核)于 1991 年在芬兰诞生,从其不起眼的开始经验了很长的路要走。它持续成为 GNU 操作系统的内核,创立了双核 GNU/Linux。

相比 Windows 和 MacOs 零碎不同的是,Linux 是永恒收费的。如果你应用 Mac,你须要晓得 macOS 是一个 UNIX 操作系统,它与 GNU/Linux 零碎有很多相独特的思维,Shell 命令也甚至局部软件能够间接兼容,因为 GNU/Linux 是基于 UNIX 开源的。

而如果应用 Windows,则提供了 Windows Subsystem for Linux 来实现运行 Linux 的相干内容。然而集体并不举荐是用 WSL 来学习 Linux 命令。(更举荐虚拟机)

绝大多数下应用 Linux 是在 VPS (Virtual Private Server) 等环境当中,Linux 提供了 shell 来执行脚本或者文原本执行一系列操作。同时 shell 容许以比 GUI(图形用户界面)更优化的形式执行操作。命令行工具能够提供许多不同的配置选项,并且应用过程中并不会特地简单。

最初留神,博主用的是 Mac 电脑编写的笔记,局部命令在 CenterOs 零碎和 Ubuntu 不肯定实用,请以集体试验成果为准。

man

第一个命令是查问命令的命令,man 帮忙咱们了解所有其余命令。不晓得如何应用命令时咱们能够键入 man <command> 来获取手册:

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 specified.

       Mandatory arguments to long options are mandatory for short options too.

       -a, --all
              do not ignore entries starting with .

       -A, --almost-all
              do not list implied . and ..

       --author
              with -l, print the author of each file

       -b, --escape
              print C-style escapes for nongraphic characters

       --block-size=SIZE
              scale sizes by SIZE before printing them; e.g., '--block-size=M' prints sizes in  units  of  1,048,576
              bytes; see SIZE format below

       -B, --ignore-backups
              do not list implied entries ending with ~

man 命令是学习 Linux 必不可少的工具,然而有时候为了疾速学习一个命令,并不是把参数列举就是好的,更多时候咱们会想要看到理论案例疾速上手。

为了疾速学习命令举荐应用 tldr(https://tldr.sh/)的网站。咱们能够装置的命令 tldr 疾速上手,而后像这样运行它:tldr <command>

它给你一个十分疾速的命令概述,并提供一些常见应用场景的不便示例:

ubuntu@VM-8-8-ubuntu:~$ sudo tldr ls
Cache is out of date. You should run "tldr --update"

  ls

  List directory contents.
  More information: https://www.gnu.org/software/coreutils/ls.

  - List files one per line:
    ls -1

  - List all files, including hidden files:
    ls -a

  - List all files, with trailing / added to directory names:
    ls -F

  - Long format list (permissions, ownership, size, and modification date) of all files:
    ls -la

  - Long format list with size displayed using human-readable units (KiB, MiB, GiB):
    ls -lh

  - Long format list sorted by size (descending):
    ls -lS

  - Long format list of all files, sorted by modification date (oldest first):
    ls -ltr

  - Only list directories:
    ls -d */

留神 tldr 这个命令须要 NodeJs 的环境,倡议先装置 NodeJs,这里了 跳过 NodeJs 的装置过程

[zxd@localhost ~]$ tldr
Usage: tldr command [options]

Simplified and community-driven man pages

Options:
  -v, --version            Display version
  -l, --list               List all commands for the chosen platform in the cache
  -a, --list-all           List all commands in the cache
  -1, --single-column      List single command per line (use with options -l or -a)
  -r, --random             Show a random command
  -e, --random-example     Show a random example
  -f, --render [file]      Render a specific markdown [file]
  -m, --markdown           Output in markdown format
  -o, --os [type]          Override the operating system [linux, osx, sunos, windows]
  --linux                  Override the operating system with Linux
  --osx                    Override the operating system with OSX
  --sunos                  Override the operating system with SunOS
  --windows                Override the operating system with Windows
  -t, --theme [theme]      Color theme (simple, base16, ocean)
  -s, --search [keywords]  Search pages using keywords
  -u, --update             Update the local cache
  -c, --clear-cache        Clear the local cache
  -h, --help               Show this help message

  Examples:

    $ tldr tar
    $ tldr du --os=linux
    $ tldr --search "create symbolic link to file"
    $ tldr --list
    $ tldr --list-all
    $ tldr --random
    $ tldr --random-example

  To control the cache:

    $ tldr --update
    $ tldr --clear-cache

  To render a local file (for testing):

    $ tldr --render /path/to/file.md

如果你感觉搞 NodeJs 环境和版本非常麻烦,还有个相似的替代品 cheat,它的应用非常简单,把官网文档介绍的脚本执行之后即可应用。

官网装置教程:cheat/INSTALLING.md at master · cheat/cheat (github.com)”)

当然 tldr 的 github 我的项目中也介绍了其余替代品:

Similar projects

  • Command Line Interface Pages allows you to write standardized help pages for CLI, directories and configs.
  • Cheat allows you to create and view interactive cheatsheets on the command-line. It was designed to help remind *nix system administrators of options for commands that they use frequently, but not frequently enough to remember.
  • cheat.sh Aggregates cheat sheets from multiple sources (including tldr-pages) into 1 unified interface.
  • devhints Rico’s cheatsheets are not just focused on the command-line and include a plethora of other cheatsheets related to programming.
  • eg provides detailed examples with explanations on the command-line. Examples come from the repository, but eg supports displaying custom examples and commands alongside the defaults.
  • kb is a minimalist command-line knowledge base manager. kb can be used to organize your notes and cheatsheets in a minimalist and clean way. It also supports non-text files.
  • navi is an interactive cheatsheet tool, which allows you to browse through specific examples or complete commands on the fly.
  • bropages (deprecated) are a highly readable supplement to man pages. It shows concise, common-case examples for Unix commands. The examples are submitted by the user base, and can be voted up or down; the best entries are what people see first when they look up a command.
  • Command Line Interface Pages allows you to write standardized help pages for CLI, directories and configs.
  • Cheat allows you to create and view interactive cheatsheets on the command-line. It was designed to help remind *nix system administrators of options for commands that they use frequently, but not frequently enough to remember.
  • cheat.sh Aggregates cheat sheets from multiple sources (including tldr-pages) into 1 unified interface.
  • devhints Rico’s cheatsheets are not just focused on the command-line and include a plethora of other cheatsheets related to programming.
  • eg provides detailed examples with explanations on the command-line. Examples come from the repository, but eg supports displaying custom examples and commands alongside the defaults.
  • kb is a minimalist command-line knowledge base manager. kb can be used to organize your notes and cheatsheets in a minimalist and clean way. It also supports non-text files.
  • navi is an interactive cheatsheet tool, which allows you to browse through specific examples or complete commands on the fly.
  • bropages (deprecated) are a highly readable supplement to man pages. It shows concise, common-case examples for Unix commands. The examples are submitted by the user base, and can be voted up or down; the best entries are what people see first when they look up a command.

printenv

这个命令能够发打印以后的环境变量参数,如果须要打印指定环境变量的值就须要指定对应的 key,留神 linux 零碎对于大小写敏感,变量名要辨别大小写。

ubuntu@VM-8-8-ubuntu:~$ printenv 
SHELL=/bin/bash
HISTSIZE=1000
HISTTIMEFORMAT=%F %T 
PWD=/home/ubuntu
LOGNAME=ubuntu
XDG_SESSION_TYPE=tty
MOTD_SHOWN=pam
HOME=/home/ubuntu
LANG=en_US.UTF-8

printenv命令的疾速指南,用于打印环境变量的值

在任何一个 shell 中,都有大量的环境变量,它们或者由零碎设置,或者由你本人的 shell 脚本和配置设置。

你能够应用 printenv 命令将它们全副打印到终端。你能够附加一个变量名作为参数,只显示该变量值,其输入后果将是这样的:

[zxd@localhost ~]$ printenv JAVA_HOME
/opt/jdk8
[zxd@localhost ~]$ printenv PATH
/opt/maven-3.8.6/bin:/opt/jdk8/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/zxd/.local/bin:/home/zxd/bin

ls

ls 是 list 的缩写,作用是打印某个文件夹上面的所有文件。

ls -al

ls -al 能够应用竖状的展现以后文件夹的所有内容。

xander@xander:~$ ls -al
total 76
drwxr-x--- 6 xander xander 4096 Mar 11 02:20 .
drwxr-xr-x 3 root   root   4096 Jan  8 03:12 ..
-rw------- 1 xander xander 1550 Feb 27 23:50 .bash_history
-rw-r--r-- 1 xander xander  220 Jan  6  2022 .bash_logout
-rw-r--r-- 1 xander xander 3771 Jan  6  2022 .bashrc
drwx------ 2 xander xander 4096 Jan  8 03:12 .cache
drwx------ 3 xander xander 4096 Mar 11 02:20 .config
-rwxrwxr-x 1 xander xander  174 Feb  3 13:37 demo2.sh
-rwxrwxr-x 1 xander xander  223 Feb  3 13:17 demo-arges.sh
-rw-rw-r-- 1 xander xander   30 Feb 24 23:25 error.txt
-rwxrwxr-x 1 xander xander  277 Feb  3 13:22 func-args.sh
-rw------- 1 xander xander   20 Mar 11 01:50 .lesshst
-rw-rw-r-- 1 xander xander   18 Feb 24 23:24 log.txt
-rw-r--r-- 1 xander xander  807 Jan  6  2022 .profile
drwx------ 2 xander xander 4096 Jan  8 03:14 .ssh
-rw-r--r-- 1 xander xander    0 Jan  8 03:26 .sudo_as_admin_successful
drwxrwxr-x 2 xander xander 4096 Mar 11 00:33 tesgt
-rw------- 1 xander xander 5795 Feb  3 13:37 .viminfo
-rw------- 1 xander xander  104 Mar 11 00:31 .Xauthority

从左到右的含意:

  • the file permissions (and if your system supports ACLs, you get an ACL flag as well)
  • the number of links to that file
  • the owner of the file
  • the group of the file
  • the file size in bytes
  • the file modified datetime
  • the file name

mkdir

应用 mkdir 用于创立一个新的文件夹:

mkdir test

mkdir 前面接多个参数,能够创立多个文件夹。

mkdir test1 test2 test3

还有一个非凡的门路指示器,它是 .,示意以后的文件夹。

你也能够应用绝对路径,从根文件夹 / 开始:

cd /etc

这个命令能够在 Linux、macOS、WSL 以及任何有 UNIX 环境的中央应用。

rmdir

和 mkdir 命令刚好相同,这个命令用于删除一个目录。

[zxd@localhost ~]$ mkdir test
[zxd@localhost ~]$ rmdir test

删除多个文件夹则在命令前面跟进多个文件夹名称即可。

[zxd@localhost ~]$ mkdir test test1 test2
[zxd@localhost ~]$ rmdir test test1 test2

留神默认状况下删除一个外部带文件的文件夹会报错:

[zxd@localhost ~]$ mkdir test
[zxd@localhost ~]$ touch test/test.txt
[zxd@localhost ~]$ rmdir test
rmdir: failed to remove‘test’: Directory not empty

要删除带有其余文件的文件夹,咱们将应用更通用的 rm命令,该命令应用 -rf 选项删除文件和文件夹:

rm -rf test

请审慎应用此命令!

[zxd@localhost ~]$ mkdir test 
mkdir: cannot create directory‘test’: File exists
[zxd@localhost ~]$ touch test/test.txt
[zxd@localhost ~]$ rm -r test

从命令行删除文件时,没有 bin 记录,复原失落的文件可能很艰难。

mv

mv的通常用处是用于把一个文件挪到另一个地位:

[zxd@localhost ~]$ mkdir test
[zxd@localhost ~]$ cd test
[zxd@localhost test]$ mkdir bemoved
[zxd@localhost test]$ touch a.txt aaa
[zxd@localhost test]$ cat a.txt 
[zxd@localhost test]$ mv a.txt bemoved/
[zxd@localhost test]$ ls bemoved/
a.txt

mv 还能够用于重命名:

[zxd@localhost test]$ mv bemoved/a.txt bemoved/b.txt
[zxd@localhost test]$ ls bemoved/
b.txt

如果想要把多个文件夹的内容挪动到指定的文件夹,在 mv 参数当中以 最初一个参数 为指标,具体的案例如下:

[zxd@localhost test]$ mkdir apple pear
[zxd@localhost test]$ mkdir fruits
[zxd@localhost test]$ mv apple pear fruits/
[zxd@localhost test]$ ls fruits/
apple  pear

mv 还有一个暗藏作用是“重命名”:

ubuntu@VM-8-8-ubuntu:~$ ls
''$'\033''[200~recipes.txt~'   compress   dif              error.txt   list2.txt.gz   newrecipes.txt   otherfile.txt   testdata.txt      test.txt
 archive.tar                   current    echoresult.txt   list2.txt   list.txt.gz    otherfile2.txt   testdata2.txt   testdata.txt.gz
 
ubuntu@VM-8-8-ubuntu:~$ mv dif difff


ubuntu@VM-8-8-ubuntu:~$ ls
''$'\033''[200~recipes.txt~'   compress   difff            error.txt   list2.txt.gz   newrecipes.txt   otherfile.txt   testdata.txt      test.txt
 archive.tar                   current    echoresult.txt   list2.txt   list.txt.gz    otherfile2.txt   testdata2.txt   testdata.txt.gz
ubuntu@VM-8-8-ubuntu:~$ 

touch

touch 命令通常用于构建一个新文件:

touch apple

留神如果该文件曾经存在,它将以 写模式 关上该文件并 更新文件的工夫戳

ubuntu@VM-8-8-ubuntu:~$ ls -l
-rw-rw-r-- 1 ubuntu ubuntu   0 Mar 14 13:18 list2.txt

ubuntu@VM-8-8-ubuntu:~$ touch list2.txt 
ubuntu@VM-8-8-ubuntu:~$ ls -l
-rw-rw-r-- 1 ubuntu ubuntu   0 Mar 15 13:24 list2.txt

那么如果 touch 一个有数据的文件呢?实际上也是更新工夫戳。

ubuntu@VM-8-8-ubuntu:~$ ls -l
total 24

-rw-rw-r-- 1 ubuntu ubuntu 226 Mar 14 13:22 list.txt

ubuntu@VM-8-8-ubuntu:~$ touch list.txt 

ubuntu@VM-8-8-ubuntu:~$ ls -l
total 24
-rw-rw-r-- 1 ubuntu ubuntu 226 Mar 15 13:25 list.txt

find

find 命令次要用于 Linux 的文件查找,能够借助此命令寻找合乎特定搜寻模式的文件或文件夹。它的搜寻是递归的。

比方上面的案例,查找当前目录所有的 txt 文件:

-rw-rw-r-- 1 ubuntu ubuntu  16 Mar 14 13:13 testdata.txt
ubuntu@VM-8-8-ubuntu:~$ find . -name '*.txt'
./testdata2.txt
./otherfile2.txt
./testdata.txt
./list2.txt
./otherfile.txt
./error.txt
./list.txt
./.config/cheat/cheatsheets/community/.github/LICENSE.txt

提醒:在特殊字符(如 *)四周须要应用“引号”,防止 shell 对其进行其余解释。

上面的案例是查找以后零碎所有门路带有 src 的文件夹,然而会发现这里会呈现“无权拜访”的提醒:

ubuntu@VM-8-8-ubuntu:~$ find / -type d -name src
find:‘/tmp/systemd-private-b1f69592ff0a423584359e16f90b4c29-ModemManager.service-yvVX9P’: Permission denied
find:‘/tmp/systemd-private-b1f69592ff0a423584359e16f90b4c29-ntp.service-VRK7Gs’: Permission denied
find:‘/tmp/tat_agent’: Permission denied
find:‘/tmp/systemd-private-b1f69592ff0a423584359e16f90b4c29-systemd-resolved.service-3IzTT4’: Permission denied
find:‘/tmp/8a6be2f9e39171ef24ae0f7757c5d095’: Permission denied
find:‘/tmp/systemd-private-b1f69592ff0a423584359e16f90b4c29-systemd-logind.service-92E7Xj’: Permission denied
find:‘/tmp/snap-private-tmp’: Permission denied
find:‘/var/cache/pollinate’: Permission denied

这些宜人的报错能够通过重定向 stderr/dev/null过滤掉:

ubuntu@VM-8-8-ubuntu:~$ find / -type d -name src 2> /dev/null
/usr/src
/usr/src/linux-headers-5.15.0-48/tools/usb/usbip/src
/usr/src/linux-headers-5.15.0-48/drivers/gpu/drm/amd/display/dmub/src
/usr/include/nodejs/src
/usr/local/n/versions/node/16.15.1/lib/node_modules/npm/node_modules/agent-base/src
/usr/local/n/versions/node/16.15.1/lib/node_modules/npm/node_modules/agent-base/dist/src
/usr/local/n/versions/node/16.15.1/lib/node_modules/npm/node_modules/tiny-relative-date/src
/usr/local/n/versions/node/16.15.1/lib/node_modules/npm/node_modules/node-gyp/src
/usr/local/n/versions/node/16.15.1/lib/node_modules/npm/node_modules/cli-table3/src
/usr/local/n/versions/node/16.15.1/lib/node_modules/npm/node_modules/debug/src
/usr/local/n/versions/node/16.15.1/lib/node_modules/npm/node_modules/has/src
/usr/local/n/versions/node/19.5.0/lib/node_modules/npm/node_modules/agent-base/src
/usr/local/n/versions/node/19.5.0/lib/node_modules/npm/node_modules/agent-base/dist/src
...

通过下面的命令能够看到所有蕴含 src 文件夹的门路被展现进去。

-type d 示意搜寻文件夹。

-type f:示意仅仅搜寻文件夹,

-type l 示意只搜寻符号连贯。

此外还须要留神,-name是辨别大小写的。应用 -iname 来执行不辨别大小写的搜寻。下面介绍的都是搜寻单个文件夹,搜寻多个文件门路能够应用上面的形式:

find folder1 folder2 -name filename.txt

再比方在多个文件夹上面搜寻须要的文件:

sudo find /usr  ~/’*.txt‘

如果是匹配多个文件名称,则能够应用 -or 拼接搜寻条件:

find . -type d -name node_modules -or -name otherfile.txt
./otherfile.txt

当然也能够增加排除的文件门路,搜寻的时候跳过他们:

find . -type d -name '*.md' -not -path 'node_modules/*'

还能够搜寻其中有超过 100 个字符(字节)的文件:

find . -type f -size +100c

查找文件大小大于 100KB 然而小于 1M 的文件:

find . -type f -size +100k -size -1M

查找批改工夫超过 3 天的文件

[zxd@localhost ~]$ find . -type f -mtime +3 -not -path './.*/*'
./.bash_logout
./.bash_profile
./.bashrc
./zookeeper-3.4.14.tar.gz.1
./.profile
./hello.c
./a.out
./.lesshst
./c.txt
./test.json
./sss.json
./zookeeper-3.4.14.tar_3.gz
./test.sh
./jdk-8u331-linux-x64.tar.gz
./apache-maven-3.8.6-bin.zip
./seata-server-1.6.1.tar.gz
./.mysql_history
./kafka_2.13-2.8.2.tgz
./.viminfo
./sys_dump.sql
./.node_repl_history
./.npmrc
./a.txt
./b.txt
./ss.json
./nohup.out
./test.txt

搜寻过来 24 小时内编辑的文件

[zxd@localhost ~]$ find . -type f -mtime -1
./test/bemoved/a.txt
./test/aaa
./test/test.txt
./test/test_copy.txt
./test/bemoved_copy/a.txt

你能够通过增加 -delete 选项来删除所有与搜寻匹配的文件,这将删除过来 24 小时内编辑的所有文件:

find . -type f -mtime -1 -delete

能够对每个搜寻后果执行一个命令。在这个例子中,咱们运行 cat 来打印文件内容:

上面的命令不要轻易尝试

find . -type f -exec cat {} \;

请留神,结尾处的 " .{} 在执行时被填上了文件名。

cd

cd 命令用于挪动到指定的文件夹当中,能够指定文件夹相对路径或者绝对路径,比方:

mkdir fruits
cd fruits

在类 Unix 零碎当中,..通常示意上一级目录,能够通过 cd .. 回到当前目录的上一级。

cd .. #back to the home folder

/:是 Linux 的最顶级目录,通常称为根目录。

. 示意 当前目录,当咱们进入到以后门路对应的文件夹,暗藏含意是 cd ./ 文件夹

日常工作学习应用过程中少数是通过cd /etc 的形式,从顶层门路往下找到对应想要跳转的门路。

ln

ln 命令是 Linux 文件系统命令的一部分。这个命令通常用于创立链接。链接相似指向其余文件的指针,一个文件指向另一个文件,它和 Windows 的快捷方式相似。

链接分为两种,软链接 硬链接。先介绍硬链接,硬链接很少应用。它们有一些限度:你不能链接到目录,也不能链接到内部文件系统(磁盘)。

硬链接应用上面的形式构建:

ln <original> <link>

比方想要给名为 recipes.txt 的文件构建硬链接,应用上面的命令:

ln recipes.txt newrecipes.txt

从外表上看,你创立的新硬链接与一般文件没有区别。然而在任何时候只有改变硬链接文件其中一个,那么两个文件的内容都会被同时更改。

如果你删除了原始文件,链接仍将蕴含原始文件内容,因为有一个硬链接之前指向它,然而这个 硬链接文件并不会被删除

上面介绍软链接,咱们能够应用 ln-s选项创立软链接,咱们再次援用硬链接的例子,应用软链接只须要改变参数即可:

ln -s recipes.txt newrecipes.txt

构建软链接之后,如果此时用ls -la 查看 recipes.txt 文件和 newrecipes.txt,会发现软链接的文件有一个非凡的 l 标记,而且文件名在最初有一个 @(原作者是 Mac 零碎),如果你启用了色彩,它的色彩是不同的:

lrwxrwxrwx 1 ubuntu ubuntu   11 Mar 25 11:16  newrecipes.txt -> recipes.txt

如果删除了原始文件,Name 软链接将被毁坏,如果咱们试图拜访它,shell 会通知咱们 “No such file or directory”

ubuntu@VM-8-8-ubuntu:~$ cat newrecipes.txt 
ubuntu@VM-8-8-ubuntu:~$ cat recipes.txt 
ubuntu@VM-8-8-ubuntu:~$ rm recipes.txt 
ubuntu@VM-8-8-ubuntu:~$ cat newrecipes.txt 

cat: newrecipes.txt: No such file or directory

cp

复制命令。

[zxd@localhost test]$ touch test.txt
[zxd@localhost test]$ cp test.txt test_copy.txt

[zxd@localhost test]$ ll
total 0
-rw-rw-r-- 1 zxd zxd  0 Mar 24 19:20 aaa
drwxrwxr-x 2 zxd zxd 19 Mar 24 21:10 bemoved
drwxrwxr-x 4 zxd zxd 31 Mar 24 21:16 fruits
-rw-rw-r-- 1 zxd zxd  0 Mar 24 21:17 test_copy.txt
-rw-rw-r-- 1 zxd zxd  0 Mar 24 21:17 test.txt

如果要拷贝文件夹须要增加 -r 参数,它会递归整个文件夹所有文件和目录一起拷贝到新地址

[zxd@localhost test]$ ls
aaa  bemoved  fruits  test_copy.txt  test.txt
[zxd@localhost test]$ ll bemoved/
total 0
-rw-rw-r-- 1 zxd zxd 0 Mar 24 19:20 a.txt

[zxd@localhost test]$ copy -r bemoved bemoved_copy

-bash: copy: command not found
[zxd@localhost test]$ cp -r bemoved bemoved_copy
[zxd@localhost test]$ ls
aaa  bemoved  bemoved_copy  fruits  test_copy.txt  test.txt

文件夹的外部文件也会一起拷贝:

[zxd@localhost test]$ ls bemoved_copy/
a.txt
[zxd@localhost test]$ ls bemoved
a.txt

拷贝如果没 -r 参数会报错:

[zxd@localhost test]$ cp  bemoved bemoved_copy2
cp: omitting directory‘bemoved’

gzip

你能够应用 gzip 命令,用名为 LZ77gzip压缩协定来压缩文件。最简略的用法如下:

gzip filename
ubuntu@VM-8-8-ubuntu:~$ gzip list.txt
ubuntu@VM-8-8-ubuntu:~$ ll
-rw-rw-r-- 1 ubuntu ubuntu  114 Mar 15 13:25 list.txt.gz

最简略的用法会把以后的文件删除,并且生成一个原始文件名 +.gz的后缀名称。如果想要 防止原文件被删除 ,能够通过重定向的形式,或者配合-c 的选项重定向输入到 filename.gz

gzip -c filename > filename.gz

当然也能够用 -k 的选项,成果和下面的命令等效:

ubuntu@VM-8-8-ubuntu:~$ gzip -k list2.txt 
ubuntu@VM-8-8-ubuntu:~$ ll
-rw-rw-r-- 1 ubuntu ubuntu    0 Mar 15 13:24  list2.txt
-rw-rw-r-- 1 ubuntu ubuntu   30 Mar 15 13:24  list2.txt.gz

留神 gzip 内置了压缩等级的概念,从 压缩等级 1(最快然而压缩比最小)压缩等级 9(最慢然而压缩比最大),默认状况下应用 gzip 压缩等级为 6。

咱们能够通过 -(数字 1 -9) 的形式的形式自定义压缩等级:

ubuntu@VM-8-8-ubuntu:~/compress$ touch aaa.txt
ubuntu@VM-8-8-ubuntu:~/compress$ vim aaa.txt 
ubuntu@VM-8-8-ubuntu:~/compress$ cat aaa.txt 
12345678910`12345678910`12345678910`12345678910`12345678910`12345678910`12345678910`12345678910`12345678910`12345678910`12345678910`12345678910`

ubuntu@VM-8-8-ubuntu:~/compress$ gzip -1k aaa.txt 
ubuntu@VM-8-8-ubuntu:~/compress$ ll
total 16
drwxrwxr-x 2 ubuntu ubuntu 4096 Mar 25 11:26 ./
drwxr-x--- 8 ubuntu ubuntu 4096 Mar 25 11:26 ../
-rw-rw-r-- 1 ubuntu ubuntu  145 Mar 25 11:26 aaa.txt
-rw-rw-r-- 1 ubuntu ubuntu   44 Mar 25 11:26 aaa.txt.gz

压缩多个文件只须要在命令开端指定多个文件,两头用空格分隔即可:

gzip filename1 filename2

你能够应用 -r 选项,递归地压缩一个目录中的所有文件:

留神并不是把整个文件夹打成一个压缩包

gzip -r a_folder

-v选项能够打印出 压缩百分比信息 。上面是一个它与-k 选项一起应用的例子:

ubuntu@VM-8-8-ubuntu:~$ gzip -vk testdata.txt 
testdata.txt:    -12.5% -- created testdata.txt.gz

最初是应用 gzip 解压一个文件:

gzip -d filename.gz
ubuntu@VM-8-8-ubuntu:~$ gzip -d compress/
gzip: compress/ is a directory -- ignored
ubuntu@VM-8-8-ubuntu:~$ gzip -d compress/aaa.txt.gz 
ubuntu@VM-8-8-ubuntu:~$ cat compress/aaa.txt 
12345678910`12345678910`12345678910`12345678910`12345678910`12345678910`12345678910`12345678910`12345678910`12345678910`12345678910`12345678910`

gunzip

gunzip 命令基本上等同于 gzip 命令,除了 -d 选项总是默认启用。为了缩小记忆负担,倡议应用应用 gzip -d 等效代替。

gunzip filename.gz

gzip -d filename.gz 的成果一样,会解压压缩包,如果发现文件曾经存在则会询问是否须要笼罩。

ubuntu@VM-8-8-ubuntu:~$ gzip -r compress/
gzip: compress/aaa.txt.gz already exists; do you wish to overwrite (y or n)? y

tar

tar 要比 gzip 更为宽泛流传和应用,tar 命令用于创立一个归档文件,将多个文件分组在一个文件中。tar 的全程叫做tape archive,就是说把指定的内容归档磁盘当中进行长久存储。

上面的命令把名字叫做 archive.tar 磁盘档案,它的内容蕴含error.txtlist2.txt 这两个文件。

ubuntu@VM-8-8-ubuntu:~$ tar -cf archive.tar error.txt list2.txt

ubuntu@VM-8-8-ubuntu:~$ ll
total 116
drwxr-x--- 8 ubuntu ubuntu  4096 Mar 25 13:35  ./
drwxr-xr-x 4 root   root    4096 Nov 10 07:16  ../
-rw-rw-r-- 1 ubuntu ubuntu     0 Mar 25 11:15 ''$'\033''[200~recipes.txt~'
-rw-rw-r-- 1 ubuntu ubuntu 10240 Mar 25 13:35  archive.tar

能够利用 tar -tf 的命令查看压缩包的文件内容。

ubuntu@VM-8-8-ubuntu:~$ tar -tf archive.tar 
error.txt
list2.txt

tar 也能够用于配合打包 gzip 压缩之后的文件。

ubuntu@VM-8-8-ubuntu:~$ tar -tf archive.tar 
error.txt
list2.txt
ubuntu@VM-8-8-ubuntu:~$ tar -cf archive.tar list2.txt.gz list.txt.gz 
ubuntu@VM-8-8-ubuntu:~$ tar -tf archive.tar 
list2.txt.gz
list.txt.gz

留神-c 选项代表创立。-f 选项用于将档案写入文件。要从以后文件夹中的存档中提取文件,请应用:

tar -xf archive.tar

-x的选项是用于解压软件的。如果要把以后的文件解压到一个非凡的目录能够应用 -C 的选项,然而须要留神目录要理论存在才行。

ubuntu@VM-8-8-ubuntu:~$ tar -xf archive.tar -C special
tar: special: Cannot open: No such file or directory
tar: Error is not recoverable: exiting now

ubuntu@VM-8-8-ubuntu:~$ tar -xf archive.tar -C compress/

ubuntu@VM-8-8-ubuntu:~$ ls compress/
aaa.txt  list2.txt.gz  list.txt.gz

后面介绍的 gzip -d gunzip的命令都是用来解压压缩软件的,在解压缩档案时,你能够应用 gunzipgzip -d,而后解压缩,但 tar -xf 会辨认它是一个压缩档案并为你做这件事:

ubuntu@VM-8-8-tar -xf compress/list2.txt.gz
ubuntu@VM-8-8-ubuntu:~$ cat compress/list2.txt.gz                                                                                                               ¦Vdlist2.txt

综上所述集体更为倡议用 tar 命令。

alias

别名命令能够把一些简单指令通过别名的形式代替。比方古代大部分的 Linux 零碎都提供了相似 ll 的命令代替ls -l,有的云服务器会带上色彩选项。

要查看一个别名命令的理论命令,能够参考上面的内容。

ubuntu@VM-8-8-ubuntu:~$ alias ll
alias ll='ls -alF'

咱们同样也能够 alias 命令来指定本人的别名指令。

ubuntu@VM-8-8-ubuntu:~$ alias lsa='ls -la'

然而留神这种形式不能让命令长久化,在Session 会话完结之后 alias 自定义就会生效

要使其永久化须要将命令增加到 shell 配置中,如果应用 Bash shell,则可能是 ~/.bashrc~/.profile,或者 ~/.bash_profile,具体取决于应用的操作系统。

如果命令中有变量,请留神引号要用 双引号 ,变量在定义时解析,它在调用时解析应用单引号,这两个是 不同 的:

alias lsthis="ls $PWD"
alias lscurrent='ls $PWD'

第一个命令当中的 $PWD,指的是 shell 以后所处的文件夹。举个栗子,如果当初浏览到新的文件夹,ls current 会列出新文件夹中的文件,lsthis依然会列出你定义别名时所在的文件夹中的文件。

ubuntu@VM-8-8-ubuntu:~$ alias lsthis="ls $PWD"
ubuntu@VM-8-8-ubuntu:~$ alias lscurrent='ls $PWD'

ubuntu@VM-8-8-ubuntu:~$ lsthis
''$'\033''[200~recipes.txt~'   compress    list2.txt      list.txt.gz      otherfile2.txt   testdata2.txt   testdata.txt.gz
 archive.tar                   error.txt   list2.txt.gz   newrecipes.txt   otherfile.txt    testdata.txt
ubuntu@VM-8-8-ubuntu:~$ lscurrent 
''$'\033''[200~recipes.txt~'   compress    list2.txt      list.txt.gz      otherfile2.txt   testdata2.txt   testdata.txt.gz
 archive.tar                   error.txt   list2.txt.gz   newrecipes.txt   otherfile.txt    testdata.txt

下面的后果看出两个命令的后果相似。

ubuntu@VM-8-8-ubuntu:~$ mkdir current
ubuntu@VM-8-8-ubuntu:~$ cd current/

ubuntu@VM-8-8-ubuntu:~/current$ lsthis
''$'\033''[200~recipes.txt~'   compress   error.txt   list2.txt.gz   newrecipes.txt   otherfile.txt   testdata.txt
 archive.tar                   current    list2.txt   list.txt.gz    otherfile2.txt   testdata2.txt   testdata.txt.gz
 
ubuntu@VM-8-8-ubuntu:~/current$ lscurrent 

从下面的案例来看,如果是新建的文件夹 lscurrent 是会显示以后的目录,lsthis仍然会事实构建别名时候的所有文件。

cat

某些中央和 tail 命令比拟相似,能够通过 cat 向文件增加内容。

ubuntu@VM-8-8-ubuntu:~$ cat error.txt 

Command 'pirntf' not found, did you mean:
  command 'printf' from deb coreutils (8.32-4.1ubuntu1)
Try: sudo apt install <deb name>

ubuntu@VM-8-8-ubuntu:~$ cat error.txt

Command 'pirntf' not found, did you mean:
  command 'printf' from deb coreutils (8.32-4.1ubuntu1)
Try: sudo apt install <deb name>

ubuntu@VM-8-8-ubuntu:~$ vim error.txt 

ubuntu@VM-8-8-ubuntu:~$ cat error.txt >> error2.txt

ubuntu@VM-8-8-ubuntu:~$ cat error2.txt 

Command 'pirntf' not found, did you mean:
  command 'printf' from deb coreutils (8.32-4.1ubuntu1)
Try: sudo apt install <deb name>

最简略的用法是用 cat 查看一个文件:

ubuntu@VM-8-8-ubuntu:~/current$ cat ../testdata.txt
abcdefg
hijklmn
aa
aa

cat 前面跟多个文件会合并显示多个文件的内容:

ubuntu@VM-8-8-ubuntu:~/current$ cat ../testdata.txt ../otherfile.txt
abcdefg
hijklmn
aa
aa
abcdefg
hijklmn

利用 > 重定向输入流的形式的个性,咱们能够用 cat 合并两个文件的后果输入到第三个文件:

ubuntu@VM-8-8-ubuntu:~/current$ cat ../testdata.txt ../otherfile.txt
abcdefg
hijklmn
aa
aa
abcdefg
hijklmn
ubuntu@VM-8-8-ubuntu:~/current$ cat ../testdata.txt ../otherfile.txt > ./merge.txt
ubuntu@VM-8-8-ubuntu:~/current$ cat merge.txt 
abcdefg
hijklmn
aa
aa
abcdefg
hijklmn

如果想要合并数据之后往一个已存在的文件追加,能够用 >> 符号:

ubuntu@VM-8-8-ubuntu:~/current$ cat merge.txt 
abcdefg
hijklmn
aa
aa
abcdefg
hijklmn
ubuntu@VM-8-8-ubuntu:~/current$ cat ../testdata.txt ../otherfile.txt >> ./merge.txt
ubuntu@VM-8-8-ubuntu:~/current$ cat merge.txt 
abcdefg
hijklmn
aa
aa
abcdefg
hijklmn
abcdefg
hijklmn
aa
aa
abcdefg
hijklmn

有时候在浏览代码文件,为了更好的定位所在位置,能够用 -n 参数打印行号:

ubuntu@VM-8-8-ubuntu:~/current$ cat -n merge.txt 
     1    abcdefg
     2    hijklmn
     3    aa
     4    aa
     5    abcdefg
     6    hijklmn
     7    abcdefg
     8    hijklmn
     9    aa
    10    aa
    11    abcdefg
    12    hijklmn

能够只用 -b 给非空行增加数字,或者你也能够用 -s 删除所有多空行。

ubuntu@VM-8-8-ubuntu:~/current$ cat -n merge.txt 
     1    abcdefg
     2    hijklmn
     3    aa
     4    aa
     5    
     6    
     7    abcdefg
     8    hijklmn
     9    abcdefg
    10    hijklmn
    11    aa
    12    
    13    
    14    aa
    15    abcdefg
    16    hijklmn
ubuntu@VM-8-8-ubuntu:~/current$ cat -sn merge.txt 
     1    abcdefg
     2    hijklmn
     3    aa
     4    aa
     5    
     6    abcdefg
     7    hijklmn
     8    abcdefg
     9    hijklmn
    10    aa
    11    
    12    aa
    13    abcdefg
    14    hijklmn
ubuntu@VM-8-8-ubuntu:~/current$ cat -bn merge.txt 
     1    abcdefg
     2    hijklmn
     3    aa
     4    aa


     5    abcdefg
     6    hijklmn
     7    abcdefg
     8    hijklmn
     9    aa


    10    aa
    11    abcdefg
    12    hijklmn

cat 命令比拟常见的应用是搭配管道符传递后果给其余命令应用。

less

less 命令是作者常常应用的命令,它以丑陋的交互式 UI 显示存储在文档中的内容。

ubuntu@VM-8-8-ubuntu:~/current$ less merge.txt 

如果想要退出这一的交互式界面能够在键盘上按一下 q 键,在交互模式当中能够应用方向键向上和向下键导航文档内容,也能够应用空格键和 b 逐页导航。您也能够按 G 跳到文档开端,而后按 g 跳回到结尾。

如果想向后搜寻,您能够通过按 / 并键入要搜寻的单词来搜寻文档内的内容。向前搜寻则能够应用 符号并键入一个单词。

留神这些操作都只是可视化操作,如果想要立即进行编辑,能够间接按一下 v,它会应用默认的编辑器,少数状况下会会是 vim。

笔者这里本人试验下来是 Nano

按 F 键进入追随模式或观看模式。当文档被其他人更改时,例如从另一个过程更改,能够实时查看更改(集体未进行试验)。

less 也能够关上多个文档,并通过应用 :n(转到下一个文档)和:p(转到上一个文档)浏览。

ubuntu@VM-8-8-ubuntu:~/current$ less merge.txt ../testdata.txt

比方我关上这两个文件,在上面的截图中通过交互模式理解信息。

tail

tail 的最佳应用是在调用 -f 选项时,在开端关上文件并察看文件的变动,只有文件中有新的内容,它就会被打印在窗口中。

这对于察看日志文件来说是十分棒的:

ubuntu@VM-8-8-ubuntu:~$ tail -f /var/log/syslog
Mar 27 21:07:01 localhost CRON[2319800]: (root) CMD (flock -xn /tmp/stargate.lock -c '/usr/local/qcloud/stargate/admin/start.sh > /dev/null 2>&1 &')
Mar 27 21:08:01 localhost CRON[2320054]: (root) CMD (flock -xn /tmp/stargate.lock -c '/usr/local/qcloud/stargate/admin/start.sh > /dev/null 2>&1 &')
Mar 27 21:09:01 localhost CRON[2320309]: (root) CMD (flock -xn /tmp/stargate.lock -c '/usr/local/qcloud/stargate/admin/start.sh > /dev/null 2>&1 &')
Mar 27 21:10:01 localhost CRON[2320567]: (root) CMD (flock -xn /tmp/stargate.lock -c '/usr/local/qcloud/stargate/admin/start.sh > /dev/null 2>&1 &')
Mar 27 21:10:01 localhost CRON[2320568]: (root) CMD (flock -xn /tmp/stargate.lock -c '/usr/local/qcloud/stargate/admin/start.sh > /dev/null 2>&1 &')
Mar 27 21:10:01 localhost CRON[2320570]: (root) CMD (/usr/local/qcloud/YunJing/clearRules.sh > /dev/null 2>&1)
Mar 27 21:11:01 localhost CRON[2320831]: (root) CMD (flock -xn /tmp/stargate.lock -c '/usr/local/qcloud/stargate/admin/start.sh > /dev/null 2>&1 &')
Mar 27 21:12:01 localhost CRON[2321085]: (root) CMD (flock -xn /tmp/stargate.lock -c '/usr/local/qcloud/stargate/admin/start.sh > /dev/null 2>&1 &')
Mar 27 21:12:53 localhost systemd[1]: Started Session 188653 of User ubuntu.
Mar 27 21:13:01 localhost CRON[2321438]: (root) CMD (flock -xn /tmp/stargate.lock -c '/usr/local/qcloud/stargate/admin/start.sh > /dev/null 2>&1 &')

留神下面的命令会进入交互模式并且阻塞打印命令,如果要退出能够应用 ctrl + C

咱们改变方式能够只打印开端的 10 行内容:

ubuntu@VM-8-8-ubuntu:~$ tail -n 10 /var/log/syslog
Mar 27 21:10:01 localhost CRON[2320568]: (root) CMD (flock -xn /tmp/stargate.lock -c '/usr/local/qcloud/stargate/admin/start.sh > /dev/null 2>&1 &')
Mar 27 21:10:01 localhost CRON[2320570]: (root) CMD (/usr/local/qcloud/YunJing/clearRules.sh > /dev/null 2>&1)
Mar 27 21:11:01 localhost CRON[2320831]: (root) CMD (flock -xn /tmp/stargate.lock -c '/usr/local/qcloud/stargate/admin/start.sh > /dev/null 2>&1 &')
Mar 27 21:12:01 localhost CRON[2321085]: (root) CMD (flock -xn /tmp/stargate.lock -c '/usr/local/qcloud/stargate/admin/start.sh > /dev/null 2>&1 &')
Mar 27 21:12:53 localhost systemd[1]: Started Session 188653 of User ubuntu.
Mar 27 21:13:01 localhost CRON[2321438]: (root) CMD (flock -xn /tmp/stargate.lock -c '/usr/local/qcloud/stargate/admin/start.sh > /dev/null 2>&1 &')
Mar 27 21:14:01 localhost CRON[2321715]: (root) CMD (flock -xn /tmp/stargate.lock -c '/usr/local/qcloud/stargate/admin/start.sh > /dev/null 2>&1 &')
Mar 27 21:15:01 localhost CRON[2321974]: (root) CMD (flock -xn /tmp/stargate.lock -c '/usr/local/qcloud/stargate/admin/start.sh > /dev/null 2>&1 &')
Mar 27 21:15:01 localhost CRON[2321975]: (root) CMD (flock -xn /tmp/stargate.lock -c '/usr/local/qcloud/stargate/admin/start.sh > /dev/null 2>&1 &')
Mar 27 21:15:01 localhost CRON[2321977]: (root) CMD (/usr/local/qcloud/YunJing/clearRules.sh > /dev/null 2>&1)

你能够应用行号前的 “+” 来打印整个文件内容,从 特定的行 开始:

tail -n +10 <filename>

ubuntu@VM-8-8-ubuntu:~$ tail -n 10 /var/log/syslog
Mar 27 21:10:01 localhost CRON[2320568]: (root) CMD (flock -xn /tmp/stargate.lock -c '/usr/local/qcloud/stargate/admin/start.sh > /dev/null 2>&1 &')
Mar 27 21:10:01 localhost CRON[2320570]: (root) CMD (/usr/local/qcloud/YunJing/clearRules.sh > /dev/null 2>&1)
Mar 27 21:11:01 localhost CRON[2320831]: (root) CMD (flock -xn /tmp/stargate.lock -c '/usr/local/qcloud/stargate/admin/start.sh > /dev/null 2>&1 &')
Mar 27 21:12:01 localhost CRON[2321085]: (root) CMD (flock -xn /tmp/stargate.lock -c '/usr/local/qcloud/stargate/admin/start.sh > /dev/null 2>&1 &')
Mar 27 21:12:53 localhost systemd[1]: Started Session 188653 of User ubuntu.
Mar 27 21:13:01 localhost CRON[2321438]: (root) CMD (flock -xn /tmp/stargate.lock -c '/usr/local/qcloud/stargate/admin/start.sh > /dev/null 2>&1 &')
Mar 27 21:14:01 localhost CRON[2321715]: (root) CMD (flock -xn /tmp/stargate.lock -c '/usr/local/qcloud/stargate/admin/start.sh > /dev/null 2>&1 &')
Mar 27 21:15:01 localhost CRON[2321974]: (root) CMD (flock -xn /tmp/stargate.lock -c '/usr/local/qcloud/stargate/admin/start.sh > /dev/null 2>&1 &')
Mar 27 21:15:01 localhost CRON[2321975]: (root) CMD (flock -xn /tmp/stargate.lock -c '/usr/local/qcloud/stargate/admin/start.sh > /dev/null 2>&1 &')
Mar 27 21:15:01 localhost CRON[2321977]: (root) CMD (/usr/local/qcloud/YunJing/clearRules.sh > /dev/null 2>&1)

wc

wc 命令为咱们提供了对于文件或通过管道接管的输出中的有用信息。

ubuntu@VM-8-8-ubuntu:~$ echo test >> test.txt
ubuntu@VM-8-8-ubuntu:~$ wc test.txt 
1 1 5 test.txt

返回的第一列是行数,第二列是字数,第三列是字节数。

ubuntu@VM-8-8-ubuntu:~$ ls -al | wc
     34     301    1935

如果想要只返回行数、字数、字节数,能够用选项 -l-w-c 执行:

ubuntu@VM-8-8-ubuntu:~$ wc -l test.txt 
1 test.txt
ubuntu@VM-8-8-ubuntu:~$ wc -w test.txt 
1 test.txt
ubuntu@VM-8-8-ubuntu:~$ wc -c test.txt 
5 test.txt

留神下面这些例子的后果和书中不太一样,因为不同的字符集算字节数是不一样的,在 ASCII 字符集中,字节相当于字符,但在非 ASCII 字符集中,字符的数量可能不同,因为有些字符可能须要多个字节,再比方 Unicode 中就会呈现这种状况。

在这种状况下,-m标记将有助于取得更为精确的值:

ubuntu@VM-8-8-ubuntu:~$ wc -m test.txt
5 test.txt

pwd

pwd:print name of current/working directory。并不是 password 的缩写,他的作用就是打印以后所在的目录。

ubuntu@VM-8-8-ubuntu:/$ pwd
/
ubuntu@VM-8-8-ubuntu:/$ cd ~
ubuntu@VM-8-8-ubuntu:~$ pwd
/home/ubuntu

env

env 命令能够通过设置环境变量,然而须要留神 env 设置的自定义环境变量默认在 shell 范畴(也就是一次会话)。案例如下:

env USER=flavio node app.js

能够通过这个命令理解那些环境变量被设置,案例如下:

env -i node app.js

留神下面的命令会报错,此时 node 没有设置在全局的 path 环境变量。如果须要解决此问题,能够应用残缺门路进行执行。

通过 i 参数能够特定命令的参数:

env i NAME=favio node app.js

如果去掉 i 参数能够展现原始环境变量的全貌,env 命令不带任何参数能够打印以后 shell 的所有环境变量设置。

LESSCLOSE=/usr/bin/lesspipe %s %s
XDG_SESSION_CLASS=user
TERM=xterm
LESSOPEN=| /usr/bin/lesspipe %s
USER=ubuntu
DISPLAY=localhost:10.0
SHLVL=1
XDG_SESSION_ID=211095
XDG_RUNTIME_DIR=/run/user/1000
SSH_CLIENT=120.231.210.178 1960 22
XDG_DATA_DIRS=/usr/local/share:/usr/share:/var/lib/snapd/desktop
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus
SSH_TTY=/dev/pts/0
_=/usr/bin/env
.....

应用 -u 选项使你运行的程序外部无法访问一个变量,例如上面这段代码将 HOME 变量从命令环境中删除:

ubuntu@VM-8-8-ubuntu:~$ env -u HOME node app.js
node:internal/modules/cjs/loader:936
  throw err;
  ^

Error: Cannot find module '/home/ubuntu/app.js'
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
    at Function.Module._load (node:internal/modules/cjs/loader:778:27)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
    at node:internal/main/run_main_module:17:47 {
  code: 'MODULE_NOT_FOUND',
  requireStack: []}

uname

此命令返回以后操作系统的代号,比方作者机器返回的代号是 darwin(也就是 Mac 零碎)。

-m参数能够打印出以后的零碎硬件名称,比方 Intel 是 x86 64。

ubuntu@VM-8-8-ubuntu:~$ uname -m
x86_64

-p参数能够打印处理器型号。

ubuntu@VM-8-8-ubuntu:~$ uname -p
x86_64

-s参数能够打印操作系统名称,留神和零碎代号不同打印更为具体。

ubuntu@VM-8-8-ubuntu:~$ uname -s
Linux

-r参数打印发行版本。

ubuntu@VM-8-8-ubuntu:~$ uname -r
5.15.0-48-generic

-v参数打印版本。

ubuntu@VM-8-8-ubuntu:~$ uname -v
#54-Ubuntu SMP Fri Aug 26 13:26:29 UTC 2022

-n参数打印以后网络节点名称。

ubuntu@VM-8-8-ubuntu:~$ uname -n
VM-8-8-ubuntu

-a参数打印所有的可见信息。

ubuntu@VM-8-8-ubuntu:~$ uname -a
Linux VM-8-8-ubuntu 5.15.0-48-generic #54-Ubuntu SMP Fri Aug 26 13:26:29 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux

-o 代表参数解决。

ubuntu@VM-8-8-ubuntu:~$ uname -o
GNU/Linux

MAC 操作系统反对命令 sw_vers 命令打印无关 MACOS 的信息。

crontab

定时工作能够让 Linux 设置每个小时,每天,或者其余间隔时间运行脚本。

查看 Linux 定时工作应用 crontab -l 参数。

ubuntu@VM-8-8-ubuntu:~$ crontab -l
no crontab for ubuntu

能够应用命令 crontab -e 参数批改以后的定时工作。

ubuntu@VM-8-8-ubuntu:~$ crontab -e
no crontab for ubuntu - using an empty one

Select an editor.  To change later, run 'select-editor'.
  1. /bin/nano        <---- easiest
  2. /usr/bin/vim.basic
  3. /usr/bin/vim.tiny
  4. /bin/ed

留神 -e 的批改命令有可能应用默认的 Linux 编辑器,大部分人的机器默认可能是 vim,集体应用之后发现是nanao。当然能够应用一些方法批改为 nano。

EDITOR=nano crontab -e

输出 crontab -e 之后,当初你能够增加一行命令在每一个 cron job 工作。

cronjob 命令外部的语法对于初学者比拟 ” 形象 ”。能够用网址:https://crontab-generator.org。定制订时设置。当然能够国内搜寻 cron,同样有很多好用的网站。

如果咱们想要每分钟的第五秒打印一下以后工夫,咱们能够像上面的操作:

  1. 进入到作者介绍的网页,指定每分钟第五秒执行,而后输出指令。

  1. 咱们在 vim 当中增加 5 * * * * echo $(date +%F) >/dev/null 2>&1,而后是用-xq 保留并且退出。
  2. 能够继续执行 crontab -l 能够看到刚刚新增的定时工作:
ubuntu@VM-8-8-ubuntu:~$ crontab -l
# Edit this file to introduce tasks to be run by cron.
# Print hello at 5 second intervals
5 * * * * echo $(date +%F) >/dev/null 2>&1
  1. 如果要删除,继续执行crontab -e,在编辑器删除对应的文本即可。

export

export 命令罕用于导出变量到以后的 Shell,留神在不同的场景 export 作用范畴不一样。

ubuntu@VM-8-8-ubuntu:~$ TEST=“TEST”ubuntu@VM-8-8-ubuntu:~$ echo $TEST“TEST”

默认状况下,如果在 local shell 当中设置了 test 变量,能够看到值为 TEST,在读取的时候 script.sh 脚本增加echo test。理论运行 echo 打印后果可能是nothing

nothing 是因为波及到 Linux 运行 shell 脚本机制,此时会新建子 shell 程序,这个子 shell 是无奈读取到以后 shell 的变量。

有时你须要在变量上附加一些货色,这通常是通过 PATH 变量实现的,你能够应用这种语法:

export PATH=$PATH:/new/path

如果想要以后用户的 shell 永恒失效,能够批改 .bash profile.bashrc 设置文件中增加变量。删除一个环境变量能够应用 export -n 参数。

export -n

当然如果 export 命令不加任何参数,能够查看所有的环境变量:

ubuntu@VM-8-8-ubuntu:~$ export
declare -x DBUS_SESSION_BUS_ADDRESS="unix:path=/run/user/1000/bus"
declare -x DISPLAY="localhost:10.0"
declare -x HISTSIZE="1000"
declare -x HISTTIMEFORMAT="%F %T"
declare -x HOME="/home/ubuntu"
declare -x LANG="en_US.UTF-8"
declare -x LESSCLOSE="/usr/bin/lesspipe %s %s"
declare -x LESSOPEN="| /usr/bin/lesspipe %s"
declare -x LOGNAME="ubuntu"
....

history

默认状况下应用 history 能够查看最近的所有命令。

ubuntu@VM-8-8-ubuntu:~$ history
    1  2023-03-28 21:46:09  
    2  2022-12-27 15:37:46 useradd zxd
    3  2022-12-27 15:37:52 su
    4  2022-12-29 10:37:05 ls
    5  2022-12-29 10:37:06 clear
    6  2022-12-29 10:37:08 su root
    7  2023-01-24 13:29:10 tracepath www.baidu.com
    8  2023-01-24 22:53:26 sudo apt-get install npm
  406  2023-03-28 21:56:32 du ah |sort nr | head
  407  2023-03-28 21:56:41 du -ah | sort -nr | head
  408  2023-03-28 22:01:57 TEST=“TEST”409  2023-03-28 22:02:05 echo $TEST
  410  2023-03-28 22:04:26 history
ubuntu@VM-8-8-ubuntu:~$ 

应用感叹符号!,能够从新执行对应序号的命令。

  408  2023-03-28 22:01:57 TEST=“TEST”409  2023-03-28 22:02:05 echo $TEST
  410  2023-03-28 22:04:26 history
ubuntu@VM-8-8-ubuntu:~$ !echo
echo $TEST“TEST”

下面的字符会过的一大串 从后往前 的历史命令程序进行含糊匹配命令,比方敲入 ps 会找到最近执行过的 ps 命令,!ps就能够代替上一次执行命令。

如果想要执行指定的某一条历史命令,则查看历史命令列表记住序号,而后是用上面的形式执行:

ubuntu@VM-8-8-ubuntu:~$ !409
echo $TEST“TEST”

咱们能够应用感叹号序号的组合从新执行,特别强调 history 命令默认存储近500 个命令,依照 LRU 起码应用的队列准则,或者执行行工夫的程序淘汰。

应用 -c 参数咱们能够清空所有的历史记录。

ubuntu@VM-8-8-ubuntu:~$ history -c
ubuntu@VM-8-8-ubuntu:~$ 
ubuntu@VM-8-8-ubuntu:~$ history 
    1  2023-03-29 07:46:49 history 

如果想要含糊查找历史命令的局部关键词检索,能够应用管道符 grep 搜寻。

traceroute

理解这个命令须要对于网络模型有肯定意识,依据 IP 协定能够得悉网络传输起码 一跳 能力达到指标主机的局域网段。

尝试拜访 Internet 上的主机时,您会通过家庭路由器,而后达到 ISP 网络,ISP 网络又通过其本人的上游网络路由器,依此类推,直到最终达到主机。

这个命令作用简略来说就是收集达到指标主机两头通过的路由信息。在 CenterOs 零碎中这个命令默认是不存在,须要手动装置:

[zxd@localhost ~]$ sudo yum install -y traceroute

traceroute的语法如下:

traceroute <host>

具体的应用案例如下:

[zxd@localhost ~]$ traceroute flaviocopes.com
traceroute to flaviocopes.com (172.66.47.151), 30 hops max, 60 byte packets
 1  gateway (192.168.58.2)  0.208 ms  0.148 ms  0.112 ms
 2  * * *
 3  * * *

上面是再 Ubuntu 零碎上的测试:

# 装置 traceroute
ubuntu@VM-8-8-ubuntu:~$ sudo apt install traceroute
...
ubuntu@VM-8-8-ubuntu:~$ traceroute www.baidu.com
traceroute to www.baidu.com (14.215.177.39), 30 hops max, 60 byte packets
 1  * 11.71.74.193 (11.71.74.193)  0.985 ms *
 2  * * *
 3  * * *
 4  10.162.32.233 (10.162.32.233)  1.184 ms  1.172 ms 10.196.92.129 (10.196.92.129)  6.547 ms
 5  10.162.32.145 (10.162.32.145)  1.444 ms 10.196.2.101 (10.196.2.101)  2.608 ms 10.162.32.149 (10.162.32.149)  0.967 ms
 6  * * *
 7  * * 113.96.5.13 (113.96.5.13)  2.452 ms
 8  90.96.135.219.broad.fs.gd.dynamic.163data.com.cn (219.135.96.90)  4.194 ms 121.14.14.162 (121.14.14.162)  4.007 ms 90.96.135.219.broad.fs.gd.dynamic.163data.com.cn (219.135.96.90)  4.602 ms
 9  14.215.32.102 (14.215.32.102)  6.397 ms 14.29.121.182 (14.29.121.182)  6.673 ms 14.215.32.90 (14.215.32.90)  5.802 ms

并非每个 traceroute 的路由器都会返回咱们信息(各种两头代理)。在这种状况下路由跟踪打印 * * *。否则,咱们能够看到主机名、IP 地址和一些性能指标。

对于每个路由器,咱们都能够看到 3 个样本,这意味着 traceroute 默认尝试 3 次,它很好地批示达到它所需的工夫。

与简略地对该主机执行 ping 执行工夫,就能够跟踪路由为什么须要长时间能力到对方主机。

为了让网络达到预期成果,能够通过 -q 的参数指定尝试的次数:

traceroute to flaviocopes.com (172.66.47.151), 30 hops max, 60 byte packets
 1  gateway (192.168.58.2)  0.083 ms
 2  *
 3  *

最初是集体的腾讯云服务器试验后果:

ubuntu@VM-8-8-ubuntu:~$ traceroute google.com
traceroute to google.com (142.251.42.238), 30 hops max, 60 byte packets
 1  11.71.74.225 (11.71.74.225)  1.145 ms  1.115 ms  1.296 ms
 2  11.71.120.104 (11.71.120.104)  1.107 ms * *
 3  * * *
 4  10.162.32.233 (10.162.32.233)  0.986 ms 10.162.32.241 (10.162.32.241)  1.002 ms 10.162.32.233 (10.162.32.233)  0.938 ms
 5  10.196.93.230 (10.196.93.230)  1.489 ms 10.162.32.149 (10.162.32.149)  1.462 ms 10.196.93.230 (10.196.93.230)  1.934 ms
 6  * * *
 7  113.96.8.193 (113.96.8.193)  4.770 ms * *
 8  113.96.4.165 (113.96.4.165)  2.943 ms 113.96.4.77 (113.96.4.77)  1.973 ms 113.96.4.185 (113.96.4.185)  4.139 ms


 9  * * *
10  * * *
11  * * *
12  * * *
13  * * *
14  * * *
15  * * *
16  * * *
17  * * *
18  * * *
19  * * *
20  * * *
21  * * *
22  * * *
23  * * *
24  * * *
25  * * *
26  * * *
27  * * *
28  * * *
29  * * *
30  * * *

clear

clear 革除在以后终端中运行的所有先前命令。

留神:此命令有一个不便的快捷方式:ctrl-L

实现清理操作后,将无法访问滚动以查看先前输出的命令的输入。因而如果想要查看之前的命令,能够改用 clear -x,它依然能够革除屏幕,但容许应用向上滚动来返回查看上一个工作。

如果是 SSH 配合工具的连贯 clear 十分有用。哪怕 clear 也不须要过分放心。

passwd

Linux 应用 passwd 命令来批改用户的明码。

passwd

敲入命令之后,Linux 首先会要求输出旧明码,而后输出新密码和确认明码。

须要留神普通用户批改本人的明码会被 Linux 明码强度校验规定拦挡,不合乎规定的明码是无奈被设置的,root 用户例外。

ubuntu@VM-8-8-ubuntu:~$ passwd
Changing password for ubuntu.
Current password: 

如果明码和之前一样,则是相似上面的提醒:

ubuntu@VM-8-8-ubuntu:~$ passwd
Changing password for ubuntu.
Current password: 
New password: 
Retype new password: 
The password has not been changed.
New password: 
Retype new password: 
The password has not been changed.

如果是 root 用户能够应用上面的办法批改其余用户的明码:

passwd <username> <new password>

留神 root 提醒中没有呈现要求输出旧明码的操作,root 用户能够忽视以后用户的旧明码,任意批改任何用户的明码。

ping

ping命令置信不必过多介绍,这里间接给一个例子:

xander@xander:~$ ping www.baidu.com
PING www.baidu.com (120.232.145.185) 56(84) bytes of data.

默认状况下 ping 在每秒进行尝试一次,它会始终运行,除非用户手动进行 (ctrl+c)

ping -c 2 google.com

集体的试验后果如下:

xander@xander:~$ ping -c 2 google.com
PING google.com (142.251.42.238) 56(84) bytes of data.


--- google.com ping statistics ---
2 packets transmitted, 0 received, 100% packet loss, time 1024ms

一旦进行它将打印一些对于后果的统计数据,失落包裹的百分比,以及对于网络性能的统计数据。

通过下面的打印信息和相干统计,能够看到获取响应的工夫以及丢包率。

ping 主机的 host 是很有用的,能够晓得该主机是否能够达到(假如它实现了 ping),以及它花多长时间回到你身边的间隔。通常状况下,服务器在天文上越近,返回给你的工夫就越短,因为简略的物理法则导致较长的间隔在电缆中引入更多的提早。

古代的很多服务器为了缩小负荷或者为了网络安全通常会禁 ping。ping 数据包也能够被防火墙过滤掉。

协定

留神 ping 应用 ICMP 协定(互联网管制信息协定)工作,这是一个与 TCP 或 UDP 一样的网络层协定。能够了解为申请带有一个非凡的数据包,而服务端收到数据包之后解析包给出回应。

作用

  • 查看服务器是否可达
  • 查看域名或者 IP 的正确性
  • 检测网络通信情况和品质

sudo

sudo 通常用于以 root 身份运行命令。sudo 命令能够让一个非 root 身份的角色执行和 root 身份等同的大部分操作(留神不是全副),然而留神这须要通过非凡配置容许。

权限是高度可配置的,这在多用户服务器环境中尤其杰出,并且能够通过 sudo 授予某些用户运行特定命令的拜访权限。

ubuntu@VM-8-8-ubuntu:~$ sudo nano /etc/hosts

如果须要让用户具备执行 sudo 的能力,能够批改 /etc/hosts 文件,否则编辑器会批改失败。上面的命令相当于切换到 root 用户:

sudo -i

只有晓得 root 用户的明码,基本上任何用户都能够应用 sudo 身份运行命令。你能够应用 sudo 以任何用户身份运行命令,root 是缺省默认值,应用 -u 选项能够指定另一个用户:

sudo -u flavio ls /Users/flavio

su

su 命令用于登录的时候切换到另一个用户,对于此命令的应用经常呈现在一些须要其余用户权限的场景。

su <username>

如果 su 命令不带任何其余参数或者用户名称,则默认切换到 root 并且此时 Linux 会间接提醒输出明码:

[zxd@localhost ~]$ su
Password: 

su will start a new shell as another user.
su 将以另一个用户的身份启动一个新的 shell。

这句话比拟要害,依据之前学习的 env 命令,切换到一个新的 shell 意味着有可能呈现环境变量生效的状况。应用 exit 命令,能够回退到切换之前的角色。

[zxd@localhost ~]$ su
Password: 
hello boy
[root@localhost zxd]# exit
exit
[zxd@localhost ~]$ 

who

who 能够查看以后的 Linux 中有谁在登陆。

除非你应用的是一个多人都能拜访的服务器,否则你有可能是惟一一个登录的用户,然而须要留神同一个用户屡次登录会有上面的成果。

xander@xander:~$ who
xander   pts/0        2023-03-11 00:31 (192.168.110.1)
xander   pts/1        2023-03-11 02:42 (192.168.110.1)

比拟有意思的是,同一个用户会有多个登陆登录事件是因为每个 Shell 都会算入无效拜访。能够通过 who 命令查看以后零碎谁最初登陆过以及登陆工夫。

-aH 标记将展现显示更多信息,包含闲暇工夫和终端的过程 ID:

ubuntu@VM-8-8-ubuntu:~$ who -aH
NAME       LINE         TIME             IDLE          PID COMMENT  EXIT
           system boot  2022-12-27 15:39
LOGIN      tty1         2022-12-27 15:39              1100 id=tty1
LOGIN      ttyS0        2022-12-27 15:39              1098 id=tyS0
           run-level 5  2022-12-27 15:39
ubuntu   + pts/0        2023-04-02 12:09   .        161627 (120.229.67.2)
           pts/1        2023-03-16 13:21           2430467 id=ts/1  term=0 exit=0
           pts/2        2023-03-16 13:19           2430547 id=ts/2  term=0 exit=0
           pts/3        2023-03-29 08:10           2712328 id=ts/3  term=0 exit=0

命令 who am i 能够查看本人:

ubuntu@VM-8-8-ubuntu:~$ who am i
ubuntu   pts/0        2023-04-02 12:09 (120.229.67.2)

配合参数 -aH 能够展现更多的信息:

ubuntu@VM-8-8-ubuntu:~$ who -aH am i
NAME       LINE         TIME             IDLE          PID COMMENT  EXIT
ubuntu   + pts/3        2023-04-02 13:16   .        178153 (120.229.67.2)
正文完
 0