【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 contentsSYNOPSIS 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 lsCache 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 ~]$ tldrUsage: tldr command [options]Simplified and community-driven man pagesOptions: -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/bashHISTSIZE=1000HISTTIMEFORMAT=%F %T PWD=/home/ubuntuLOGNAME=ubuntuXDG_SESSION_TYPE=ttyMOTD_SHOWN=pamHOME=/home/ubuntuLANG=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 -altotal 76drwxr-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 .bashrcdrwx------ 2 xander xander 4096 Jan 8 03:12 .cachedrwx------ 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 .profiledrwx------ 2 xander xander 4096 Jan 8 03:14 .ssh-rw-r--r-- 1 xander xander 0 Jan 8 03:26 .sudo_as_admin_successfuldrwxrwxr-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 testrmdir: 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 difffubuntu@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.gzubuntu@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.txtubuntu@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 -ltotal 24-rw-rw-r-- 1 ubuntu ubuntu 226 Mar 14 13:22 list.txtubuntu@VM-8-8-ubuntu:~$ touch list.txt ubuntu@VM-8-8-ubuntu:~$ ls -ltotal 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.txtubuntu@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 srcfind: ‘/tmp/systemd-private-b1f69592ff0a423584359e16f90b4c29-ModemManager.service-yvVX9P’: Permission deniedfind: ‘/tmp/systemd-private-b1f69592ff0a423584359e16f90b4c29-ntp.service-VRK7Gs’: Permission deniedfind: ‘/tmp/tat_agent’: Permission deniedfind: ‘/tmp/systemd-private-b1f69592ff0a423584359e16f90b4c29-systemd-resolved.service-3IzTT4’: Permission deniedfind: ‘/tmp/8a6be2f9e39171ef24ae0f7757c5d095’: Permission deniedfind: ‘/tmp/systemd-private-b1f69592ff0a423584359e16f90b4c29-systemd-logind.service-92E7Xj’: Permission deniedfind: ‘/tmp/snap-private-tmp’: Permission deniedfind: ‘/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 fruitscd 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]$ lltotal 0-rw-rw-r-- 1 zxd zxd 0 Mar 24 19:20 aaadrwxrwxr-x 2 zxd zxd 19 Mar 24 21:10 bemoveddrwxrwxr-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]$ lsaaa 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]$ lsaaa bemoved bemoved_copy fruits test_copy.txt test.txt
文件夹的外部文件也会一起拷贝:
[zxd@localhost test]$ ls bemoved_copy/a.txt[zxd@localhost test]$ ls bemoveda.txt
拷贝如果没 -r
参数会报错:
[zxd@localhost test]$ cp bemoved bemoved_copy2cp: omitting directory ‘bemoved’
gzip
你能够应用gzip命令,用名为LZ77
的gzip
压缩协定来压缩文件。最简略的用法如下:
gzip filename
ubuntu@VM-8-8-ubuntu:~$ gzip list.txtubuntu@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.txtubuntu@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$ lltotal 16drwxrwxr-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 -- ignoredubuntu@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.txt
、list2.txt
这两个文件。
ubuntu@VM-8-8-ubuntu:~$ tar -cf archive.tar error.txt list2.txtubuntu@VM-8-8-ubuntu:~$ lltotal 116drwxr-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.txtlist2.txt
tar 也能够用于配合打包 gzip 压缩之后的文件。
ubuntu@VM-8-8-ubuntu:~$ tar -tf archive.tar error.txtlist2.txtubuntu@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.gzlist.txt.gz
留神-c
选项代表创立。-f
选项用于将档案写入文件。要从以后文件夹中的存档中提取文件,请应用:
tar -xf archive.tar
-x
的选项是用于解压软件的。如果要把以后的文件解压到一个非凡的目录能够应用-C
的选项,然而须要留神目录要理论存在才行。
ubuntu@VM-8-8-ubuntu:~$ tar -xf archive.tar -C specialtar: special: Cannot open: No such file or directorytar: Error is not recoverable: exiting nowubuntu@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
的命令都是用来解压压缩软件的,在解压缩档案时,你能够应用 gunzip
或gzip -d
,而后解压缩,但 tar -xf
会辨认它是一个压缩档案并为你做这件事:
ubuntu@VM-8-8-tar -xf compress/list2.txt.gzubuntu@VM-8-8-ubuntu:~$ cat compress/list2.txt.gz ¦Vdlist2.txt
综上所述集体更为倡议用tar
命令。
alias
别名命令能够把一些简单指令通过别名的形式代替。比方古代大部分的Linux零碎都提供了相似ll
的命令代替ls -l
,有的云服务器会带上色彩选项。
要查看一个别名命令的理论命令,能够参考上面的内容。
ubuntu@VM-8-8-ubuntu:~$ alias llalias 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.txtubuntu@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 currentubuntu@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.txtCommand '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.txtubuntu@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.txtabcdefghijklmnaaaa
cat
前面跟多个文件会合并显示多个文件的内容:
ubuntu@VM-8-8-ubuntu:~/current$ cat ../testdata.txt ../otherfile.txtabcdefghijklmnaaaaabcdefghijklmn
利用>
重定向输入流的形式的个性,咱们能够用cat
合并两个文件的后果输入到第三个文件:
ubuntu@VM-8-8-ubuntu:~/current$ cat ../testdata.txt ../otherfile.txtabcdefghijklmnaaaaabcdefghijklmnubuntu@VM-8-8-ubuntu:~/current$ cat ../testdata.txt ../otherfile.txt > ./merge.txtubuntu@VM-8-8-ubuntu:~/current$ cat merge.txt abcdefghijklmnaaaaabcdefghijklmn
如果想要合并数据之后往一个已存在的文件追加,能够用 >>
符号:
ubuntu@VM-8-8-ubuntu:~/current$ cat merge.txt abcdefghijklmnaaaaabcdefghijklmnubuntu@VM-8-8-ubuntu:~/current$ cat ../testdata.txt ../otherfile.txt >> ./merge.txtubuntu@VM-8-8-ubuntu:~/current$ cat merge.txt abcdefghijklmnaaaaabcdefghijklmnabcdefghijklmnaaaaabcdefghijklmn
有时候在浏览代码文件,为了更好的定位所在位置,能够用-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 hijklmnubuntu@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 hijklmnubuntu@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/syslogMar 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/syslogMar 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/syslogMar 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.txtubuntu@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.txtubuntu@VM-8-8-ubuntu:~$ wc -w test.txt 1 test.txtubuntu@VM-8-8-ubuntu:~$ wc -c test.txt 5 test.txt
留神下面这些例子的后果和书中不太一样,因为不同的字符集算字节数是不一样的,在ASCII字符集中,字节相当于字符,但在非ASCII字符集中,字符的数量可能不同,因为有些字符可能须要多个字节,再比方Unicode
中就会呈现这种状况。
在这种状况下,-m
标记将有助于取得更为精确的值:
ubuntu@VM-8-8-ubuntu:~$ wc -m test.txt5 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 %sXDG_SESSION_CLASS=userTERM=xtermLESSOPEN=| /usr/bin/lesspipe %sUSER=ubuntuDISPLAY=localhost:10.0SHLVL=1XDG_SESSION_ID=211095XDG_RUNTIME_DIR=/run/user/1000SSH_CLIENT=120.231.210.178 1960 22XDG_DATA_DIRS=/usr/local/share:/usr/share:/var/lib/snapd/desktopPATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/binDBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/busSSH_TTY=/dev/pts/0_=/usr/bin/env.....
应用-u
选项使你运行的程序外部无法访问一个变量,例如上面这段代码将HOME变量从命令环境中删除:
ubuntu@VM-8-8-ubuntu:~$ env -u HOME node app.jsnode: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 -mx86_64
-p
参数能够打印处理器型号。
ubuntu@VM-8-8-ubuntu:~$ uname -px86_64
-s
参数能够打印操作系统名称,留神和零碎代号不同打印更为具体。
ubuntu@VM-8-8-ubuntu:~$ uname -sLinux
-r
参数打印发行版本。
ubuntu@VM-8-8-ubuntu:~$ uname -r5.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 -nVM-8-8-ubuntu
-a
参数打印所有的可见信息。
ubuntu@VM-8-8-ubuntu:~$ uname -aLinux 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 -oGNU/Linux
MAC操作系统反对命令sw_vers
命令打印无关MACOS的信息。
crontab
定时工作能够让 Linux 设置每个小时,每天,或者其余间隔时间运行脚本。
查看Linux定时工作应用crontab -l
参数。
ubuntu@VM-8-8-ubuntu:~$ crontab -lno crontab for ubuntu
能够应用命令crontab -e
参数批改以后的定时工作。
ubuntu@VM-8-8-ubuntu:~$ crontab -eno crontab for ubuntu - using an empty oneSelect 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,同样有很多好用的网站。
如果咱们想要每分钟的第五秒打印一下以后工夫,咱们能够像上面的操作:
- 进入到作者介绍的网页,指定每分钟第五秒执行,而后输出指令。
- 咱们在
vim
当中增加5 * * * * echo $(date +%F) >/dev/null 2>&1
,而后是用-xq
保留并且退出。 - 能够继续执行
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 intervals5 * * * * echo $(date +%F) >/dev/null 2>&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:~$ exportdeclare -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 historyubuntu@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 historyubuntu@VM-8-8-ubuntu:~$ !echoecho $TEST“TEST”
下面的字符会过的一大串从后往前的历史命令程序进行含糊匹配命令,比方敲入ps
会找到最近执行过的ps
命令,!ps
就能够代替上一次执行命令。
如果想要执行指定的某一条历史命令,则查看历史命令列表记住序号,而后是用上面的形式执行:
ubuntu@VM-8-8-ubuntu:~$ !409echo $TEST“TEST”
咱们能够应用感叹号序号的组合从新执行,特别强调history
命令默认存储近500个命令,依照LRU起码应用的队列准则,或者执行行工夫的程序淘汰。
应用-c
参数咱们能够清空所有的历史记录。
ubuntu@VM-8-8-ubuntu:~$ history -cubuntu@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.comtraceroute 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零碎上的测试:
# 装置tracerouteubuntu@VM-8-8-ubuntu:~$ sudo apt install traceroute...ubuntu@VM-8-8-ubuntu:~$ traceroute www.baidu.comtraceroute 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.comtraceroute 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:~$ passwdChanging password for ubuntu.Current password:
如果明码和之前一样,则是相似上面的提醒:
ubuntu@VM-8-8-ubuntu:~$ passwdChanging 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.comPING 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.comPING 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 ~]$ suPassword:
su will start a new shell as another user.
su 将以另一个用户的身份启动一个新的shell。
这句话比拟要害,依据之前学习的env
命令,切换到一个新的shell意味着有可能呈现环境变量生效的状况。应用exit
命令,能够回退到切换之前的角色。
[zxd@localhost ~]$ suPassword: hello boy[root@localhost zxd]# exitexit[zxd@localhost ~]$
who
who 能够查看以后的Linux中有谁在登陆。
除非你应用的是一个多人都能拜访的服务器,否则你有可能是惟一一个登录的用户,然而须要留神同一个用户屡次登录会有上面的成果。
xander@xander:~$ whoxander 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 -aHNAME LINE TIME IDLE PID COMMENT EXIT system boot 2022-12-27 15:39LOGIN tty1 2022-12-27 15:39 1100 id=tty1LOGIN ttyS0 2022-12-27 15:39 1098 id=tyS0 run-level 5 2022-12-27 15:39ubuntu + 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 iubuntu pts/0 2023-04-02 12:09 (120.229.67.2)
配合参数-aH
能够展现更多的信息:
ubuntu@VM-8-8-ubuntu:~$ who -aH am iNAME LINE TIME IDLE PID COMMENT EXITubuntu + pts/3 2023-04-02 13:16 . 178153 (120.229.67.2)