Ubuntu常用命令学习 (2)

1.echo 命令

   echo 命令用于在终端输出字符串或变量提取后的值,格式为"echo [字符串|$变量]"

image.png

2.date命令

   date命令用于显示及设置系统的时间或日期,格式为 "date[选项][+指定的格式]"
   只需要在强大的date命令中标输入以"+"号开头的参数,即可按照指定格式来输出系统的时间或日期,这样在日常工作时便可以把备份数据的命令与指定格式输出的时间信息结合到一起.例如,把打包后的文件自动按照"年-月-日"的格式打包成"backup-2019-02-12.tar.gz",用户只需要看一眼文件名称就能大概了解到每个文件的备份时间了.

              表2-4      date命令中的参数以及作用

参数 作用
%t 跳格[Tab键]
%H 小时(00~23)
%I 小时(00~12)
%M 分钟(00~59)
%S 秒(00~59)
%j 今年中的第几天

按照默认格式查看当前系统时间的 date 命令如下所示:

[root@linuxprobe ~]# date
Mon Aug 24 16:11:23 CST 2017

按照“年-月-日 小时:分钟:秒”的格式查看当前系统时间的 date 命令如下所示:

[root@linuxprobe ~]# date "+%Y-%m-%d %H:%M:%S"
2017-08-24 16:29:12

将系统的当前时间设置为 2017 年 9 月 1 日 8 点 30 分的 date 命令如下所示:

[root@linuxprobe ~]# date -s "20170901 8:30:00"
Fri Sep 1 08:30:00 CST 2017

再次使用 date 命令并按照默认的格式查看当前的系统时间,如下所示:

[root@linuxprobe ~]# date
Fri Sep 1 08:30:01 CST 2017

date 命令中的参数%j 可用来查看今天是当年中的第几天。这个参数能够很好地区分备份时
间的新旧,即数字越大,越靠近当前时间。该参数的使用方式以及显示结果如下所示。

[root@linuxprobe ~]# date "+%j"
244

3.shutdown命令

  由于重启计算机这种操作会涉及硬件资源的管理权限,因此默认只能使用root管理员来重启,其命令如下:

  • shutdown --help
    可以查看shutdown命令如何使用,当然也可以使用man shutdown命令。

  • shutdown -h now 现在立即关机

  • shutdown -r now 现在立即重启

  • shutdown -r +3 三分钟后重启

  • shutdown -h +3 “The System will shutdown after 3 minutes” 提示使用者将在三分钟后关机

zheng@zheng-Vostro-3667:~$ su root
密码: 
root@zheng-Vostro-3667:/home/zheng# shutdown -h +30 "30分钟后关机"
Shutdown scheduled for Tue 2019-02-12 16:43:07 CST, use 'shutdown -c' to cancel.
  • shutdown -r 20:23 在20:23时将重启计算机

  • shutdown -r 20:23 & 可以将在20:23时重启的任务放到后台去,用户可以继续操作终端

4.wget命令

  wget命令用于在终端中下载网络文件,格式为 "wget [参数] 下载文件"

              表2-5    wget命令的参数以及作用

参数 作用
-b 后台下载模式
-P 下载到指定目录
-t 最大尝试次数
-c 断点续传
-p 下载页面内所有资源、包括图片、视频等
-r 递归下载

  使用 wget 命令从本书的配套站点中下载本书的最新 pdf 格式电子文档,这个文件的完整路径为 http://www.linuxprobe.com/docs/LinuxProbe.pdf,执行该命令后的下载效果如下:

[root@linuxprobe ~]# wget http://www.linuxprobe.com/docs/LinuxProbe.pdf
--2017-08-24 19:30:12 -- http://www.linuxprobe.com/docs/LinuxProbe.pdf
Resolving www.linuxprobe.com (www.linuxprobe.com)... 220.181.105.185
Connecting to www.linuxprobe.com (www.linuxprobe.com)|220.181.105.185|:80...
connected.
HTTP request sent, awaiting response... 200 OK
Length: 45948568 (44M) [application/pdf]
Saving to: ‘LinuxProbe.pdf’
100%[===========================================>] 45,948,568 32.9MB/s in 1.3s
2017-08-24 19:30:14 (32.9 MB/s) - ‘LinuxProbe.pdf’ saved [45948568/45948568]

  使用 wget 命令递归下载 www.linuxprobe.com 网站内的所有页面数据以及文件,下载完后会自动保存到当前路径下一个名为 www.linuxprobe.com 的目录中。执行该操作的命令为 wget -r -p http://www.linuxprobe.com,该命令的执行结果如下:

[root@linuxprobe ~]# wget -r -p http://www.linuxprobe.com
--2017-08-24 19:31:41-- http://www.linuxprobe.com/
Resolving www.linuxprobe.com... 106.185.25.197
Connecting to www.linuxprobe.com|106.185.25.197|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [text/html]
Saving to: 'www.linuxprobe.com/index.html'
..................省略下载过程..................

5.ps命令

  ps命令用于查看系统中的进程状态,格式为 "ps [参数]"

              表2-6       ps命令的参数以及作用

参数 作用
-a 显示所有进程(包括其他用户的进程)
-u 用户以及其他详细信息
-x 显示没有控制终端的进程

  Liunx系统中时刻运行着许多进程,如果能够合理地管理它们,则可以优化系统的性能.在Liunx系统中,有5种常见的进程状态,分别为运行、中断、不可中断、僵化与停止,其各自含义如下所示:

   ➢ R(运行): 进程正在运行或在运行队列中等待。
   ➢ S(中断): 进程处于休眠中,当某个条件形成后或者接收到信号时,则脱离该状态。
   ➢ D(不可中断): 进程不响应系统异步信号,即便用 kill 命令也不能将其中断。
   ➢ Z(僵死): 进程已经终止,但进程描述符依然存在, 直到父进程调用 wait4()系统函数后将进程释放。
   ➢ T(停止): 进程收到停止信号后停止运行。

  当执行 ps aux 命令后通常会看到如表 2-7 所示的进程状态,表 2-7 中只是列举了部分输出值,而且正常的输出值中不包括中文注释。
              表2-7       进程状态


image.png

6.top命令

   top命令用于动态地监视进程活动与系统负载等信息,其格式为top.


图2-6,top命令的运行界面.png

  在图2-6中,top命令执行结果的前5行为系统整体的统计信息,其所代表的含义如下.

  ➢ 第 1 行:系统时间、运行时间、登录终端数、系统负载(三个数值分别为 1 分钟、5分钟、15 分钟内的平均值,数值越小意味着负载越低)。
  ➢ 第 2 行:进程总数、运行中的进程数、睡眠中的进程数、停止的进程数、僵死的进程数。
  ➢ 第 3 行:用户占用资源百分比、系统内核占用资源百分比、改变过优先级的进程资源百分比、空闲的资源百分比等。

7.pidof命令

  pidof命令用于查询某个指定服务进程的PID值,格式为 "pidof[参数][服务名称]"

[root@linuxprobe ~]# pidof sshd
2156

8.kill,killall命令

  kill 命令用于终止某个指定 PID 的服务进程,格式为“kill [参数] [进程 PID]”
  killall 命令用于终止某个指定名称的服务所对应的全部进程,格式为:“killall [参数] [服务名称]”。

[root@linuxprobe ~]# pidof httpd
13581 13580 13579 13578 13577 13576
[root@linuxprobe ~]# killall httpd
[root@linuxprobe ~]# pidof httpd
[root@linuxprobe ~]#

9.uname命令

  uname命令用于查看系统内核与系统版本等信息,格式为 "uname -[a]"

zheng@zheng-Vostro-3667:/etc$ uname -a
Linux zheng-Vostro-3667 4.15.0-45-generic #48-Ubuntu SMP Tue Jan 29 16:28:13 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux

查看当前版本的详细信息则用cat /proc/version

zheng@zheng-Vostro-3667:/etc$ cat /proc/version
Linux version 4.15.0-45-generic (buildd@lgw01-amd64-031) (gcc version 7.3.0 (Ubuntu 7.3.0-16ubuntu3)) #48-Ubuntu SMP Tue Jan 29 16:28:13 UTC 2019

10.free命令

  free 用于显示当前系统中内存的使用量信息,格式为“free [-h]”。

zheng@zheng-Vostro-3667:/etc$ free -h
              总计         已用        空闲      共享    缓冲/缓存    可用
内存:         15G        5.2G        5.5G        983M        4.9G        9.1G
交换:        2.0G          0B        2.0G

11.history命令

  history 命令用于显示历史执行过的命令,格式为“history [-c]”。

  在使用 history 命令时,如果使用-c 参数则会清空所有的命令历史记录。还可以使用“!编码数字”的方式来重复执行某一次的命令。

zheng@zheng-Vostro-3667:/etc$ history 
 1023  history 
 1024  ls
 1025  history 

  历史命令会被保存到用户家目录中的.bash_history 文件中。Linux 系统中以点(.)开
头的文件均代表隐藏文件,这些文件大多数为系统服务文件,可以用 cat 命令查看其文件
内容。

[root@linuxprobe ~]# cat ~/.bash_history

要清空当前用户在本机上执行的 Linux 命令历史记录信息,可执行如下命令:

[root@linuxprobe ~]# history -c

12.pwd命令

  pwd 命令用于显示用户当前所处的工作目录,格式为“pwd [选项]”。

zheng@zheng-Vostro-3667:/etc$ pwd
/etc

文本文件编辑命令

1.cat命令

  cat命令用于查看纯文本文件(内容投入的),格式为"cat [选项] [文件]"
  如果在查看文本内容时还想顺便显示行号的话,不妨在 cat 命令后面追加一个-n 参数:

[root@linuxprobe ~]# cat -n initial-setup-ks.cfg
1  #version=RHEL7
2  # X Window System configuration information
3  xconfig --startxonboot
4
5 # License agreement
6 eula --agreed
7 # System authorization information
8 auth --enableshadow --passalgo=sha512
9 # Use CDROM installation media
10 cdrom
11 # Run the Setup Agent on first boot
12 firstboot --enable
13 # Keyboard layouts
14 keyboard --vckeymap=us --xlayouts='us'
15 # System language
16 lang en_US.UTF-8
..................省略部分输出信息..................

2.more命令

  more 命令用于查看纯文本文件(内容较多的),格式为“more [选项]文件”。

[root@linuxprobe ~]# more initial-setup-ks.cfg
#version=RHEL7
# X Window System configuration information
xconfig
--startxonboot
# License agreement
eula --agreed
# System authorization information
auth --enableshadow --passalgo=sha512
# Use CDROM installation media
cdrom
# Run the Setup Agent on first boot
--More--(43%)

3.head命令

  head 命令用于查看纯文本文档的前 N 行,格式为“head [选项] [文件]”。

[root@linuxprobe ~]# head -n 20 initial-setup-ks.cfg
#version=RHEL7
# X Window System configuration information
xconfig --startxonboot
# License agreement
eula --agreed
# System authorization information
auth --enableshadow --passalgo=sha512
# Use CDROM installation media
cdrom
# Run the Setup Agent on first boot
firstboot --enable
# Keyboard layouts
keyboard --vckeymap=us --xlayouts='us'
# System language
lang en_US.UTF-8
ignoredisk --only-use=sda
# Network information
network --bootproto=dhcp --device=eno16777728 --onboot=off --ipv6=auto
[root@linuxprobe ~]#

4.tail命令

  tail 命令用于查看纯文本文档的后 N 行或持续刷新内容,格式为“tail [选项] [文件]”。我们可能还会遇到另外一种情况,比如需要查看文本内容的最后 20 行,这时就需要用到tail 命令了。tail 命令的操作方法与 head 命令非常相似,只需要执行“tail -n 20 文件名”命令就可以达到这样的效果。tail 命令最强悍的功能是可以持续刷新一个文件的内容,当想要实时查看最新日志文件时,这特别有用,此时的命令格式为“tail -f 文件名”:

[root@linuxprobe ~]# tail -f /var/log/messages
May 4 07:56:38 localhost gnome-session: Window manager warning: Log level 16:
STACK_OP_ADD: window 0x1e00001 already in stack
May 4 07:56:38 localhost gnome-session: Window manager warning: Log level 16:
STACK_OP_ADD: window 0x1e00001 already in stack
May 4 07:56:38 localhost vmusr[12982]: [ warning] [Gtk] gtk_disable_setlocale()
must be called before gtk_init()
May 4 07:56:50 localhost systemd-logind: Removed session c1.
Aug 1 01:05:31 localhost systemd: Time has been changed
Aug 1 01:05:31 localhost systemd: Started LSB: Bring up/down networking.
Aug 1 01:08:56 localhost dbus-daemon: dbus[1124]: [system] Activating service

5.diff命令

  diff命令用于比较多个文本文件的差异,格式为 "diff [参数] 文件"
  在使用diff命令时,不仅可以使用--brief参数来确认两个文件是否不同,还可以使用-c参数来详细比较出多个文件的差异之处.

[root@linuxprobe ~]# cat diff_A.txt
Welcome to linuxprobe.com
Red Hat certified
Free Linux Lessons
Professional guidance
Linux Course
[root@linuxprobe ~]# cat diff_B.txt
Welcome tooo linuxprobe.com
Red Hat certified
Free Linux LeSSonS
////////.....////////
Professional guidance
Linux Course

[root@linuxprobe ~]# diff --brief diff_A.txt diff_B.txt
Files diff_A.txt and diff_B.txt differ

[root@linuxprobe ~]# diff -c diff_A.txt diff_B.txt
*** diff_A.txt 2017-08-30 18:07:45.230864626 +0800
--- diff_B.txt 2017-08-30 18:08:52.203860389 +0800
***************
*** 1,5 ****
! Welcome to linuxprobe.com
Red Hat certified
! Free Linux Lessons
Professional guidance
Linux Course
--- 1,7 ----
! Welcome tooo linuxprobe.com
!
Red Hat certified
! Free Linux LeSSonS
! ////////.....////////
Professional guidance
Linux Course

文件目录管理命令

1.touch命令

  touch命令用于创建空白文件或设置文件的时间,格式为 "touch [格式][文件]"

              表2-11    touch命令的参数以及作用

参数 作用
-a 仅修改"读取时间"(atime)
-m 仅修改"修改时间"(mtime)
-d 同时修改atime与mtime
zheng@zheng-Vostro-3667:~/下载$ touch a.txt
zheng@zheng-Vostro-3667:~/下载$ cat a.txt
zheng@zheng-Vostro-3667:~/下载$ echo "这是一个文件" >>a.txt
zheng@zheng-Vostro-3667:~/下载$ cat a.txt
这是一个文件
zheng@zheng-Vostro-3667:~/下载$ ls -l a.txt
-rw-r--r-- 1 zheng root 19 2月  12 18:53 a.txt
zheng@zheng-Vostro-3667:~/下载$ touch -d "2019-03-01 11:01" a.txt
zheng@zheng-Vostro-3667:~/下载$ ls -l a.txt
-rw-r--r-- 1 zheng root 19 3月   1  2019 a.txt
zheng@zheng-Vostro-3667:~/下载$ 

2.mkdir命令

  mkdir 命令用于创建空白的目录,格式为“mkdir [选项] 目录”
  在 Linux 系统中,文件夹是最常见的文件类型之一。除了能创建单个空白目录外,mkdir
命令还可以结合-p 参数来递归创建出具有嵌套叠层关系的文件目录。

zheng@zheng-Vostro-3667:~/下载$ mkdir a
zheng@zheng-Vostro-3667:~/下载$ cd a
zheng@zheng-Vostro-3667:~/下载/a$ mkdir -p b/c/d/e
zheng@zheng-Vostro-3667:~/下载/a$ cd b
zheng@zheng-Vostro-3667:~/下载/a/b$ cd c
zheng@zheng-Vostro-3667:~/下载/a/b/c$ cd d
zheng@zheng-Vostro-3667:~/下载/a/b/c/d$ cd e
zheng@zheng-Vostro-3667:~/下载/a/b/c/d/e$ ls
zheng@zheng-Vostro-3667:~/下载/a/b/c/d/e$ 

3.cp命令

  cp命令用于复制文件或目录,格式为 "cp[选项] 源文件 目标文件"

大家对文件复制操作应该不陌生,在 Linux 系统中,复制操作具体分为 3 种情况:
➢ 如果目标文件是目录,则会把源文件复制到该目录中;
➢ 如果目标文件也是普通文件,则会询问是否要覆盖它;
➢ 如果目标文件不存在,则执行正常的复制操作。
cp 命令的参数及其作用如表 2-12 所示。

              表2-12   cp 命令的参数及其作用

参数 作用
-p 保留原始文件的属性
-d 若对象为“链接文件”,则保留该“链接文件”的属性
-r 递归持续复制(用于目录)
-i 若目标文件存在则询问是否覆盖
-a 相当于-pdr(p、d、r 为上述参数)
[root@linuxprobe ~]# touch install.log
[root@linuxprobe ~]# cp install.log x.log
[root@linuxprobe ~]# ls
install.log x.log

4.mv命令

  mv 命令用于剪切文件或将文件重命名,格式为“mv [选项] 源文件 [目标路径|目标文件名]”。剪切操作不同于复制操作,因为它会默认把源文件删除掉,只保留剪切后的文件。如果在同一个目录中对一个文件进行剪切操作,其实也就是对其进行重命名:

[root@linuxprobe ~]# mv x.log linux.log
[root@linuxprobe ~]# ls
install.log linux.log

5.rm命令

  rm 命令用于删除文件或目录,格式为“rm [选项] 文件”。
  在 Linux 系统中删除文件时,系统会默认向您询问是否要执行删除操作,如果不想总是看到这种反复的确认信息,可在 rm 命令后跟上-f 参数来强制删除。另外,想要删除一个目录,需要在 rm 命令后面一个-r 参数才可以,否则删除不掉。我们来尝试删除前面创建的 install.log和 linux.log 文件:

[root@linuxprobe ~]# rm install.log
rm: remove regular empty file ‘install.log’? y
[root@linuxprobe ~]# rm -f linux.log
[root@linuxprobe ~]# ls
[root@linuxprobe ~]#

6.file命令

  file 命令用于查看文件的类型,格式为“file 文件名”

[root@linuxprobe ~]# file anaconda-ks.cfg
anaconda-ks.cfg: ASCII text
[root@linuxprobe ~]# file /dev/sda
/dev/sda: block special

打包压缩与搜索命令

1.tar命令

  tar命令用于对文件进行打包压缩或解压,格式为 "tar[选项] [文件]"
  在 Linux 系统中,常见的文件格式比较多,其中主要使用的是.tar 或.tar.gz 或.tar.bz2 格式

              表2-14   tar 命令的参数及其作用

参数 作用
-c 创建压缩文件
-x 解开压缩文件
-t 查看压缩包内有哪些文件
-z 用Gzip压缩或解压
-j 用bzip2压缩或解压
-v 显示压缩或解压的过程
-f 目标文件名
-p 保留原始的权限与属性
-P 使用绝对路径来压缩
-C 指定解压到的目录

  使用 tar 命令把/etc 目录通过 gzip 格式进行打包压缩,并把文件命名为 etc.tar.gz:

[root@linuxprobe ~]# tar -czvf etc.tar.gz /etc
tar: Removing leading '/' from member names
/etc/
/etc/fstab
/etc/crypttab
/etc/mtab
/etc/fonts/
/etc/fonts/conf.d/
/etc/fonts/conf.d/65-0-madan.conf
/etc/fonts/conf.d/59-liberation-sans.conf
/etc/fonts/conf.d/90-ttf-arphic-uming-embolden.conf
/etc/fonts/conf.d/59-liberation-mono.conf
/etc/fonts/conf.d/66-sil-nuosu.conf
..................省略部分压缩过程信息..................

  将打包后的压缩包文件指定解压到/root/etc 目录中(先使用 mkdir 命令来创建/root/etc 目录):

[root@linuxprobe ~]# mkdir /root/etc
[root@linuxprobe ~]# tar xzvf etc.tar.gz -C /root/etc
etc/
etc/fstab
etc/crypttab
etc/mtab
etc/fonts/
etc/fonts/conf.d/
etc/fonts/conf.d/65-0-madan.conf
etc/fonts/conf.d/59-liberation-sans.conf
etc/fonts/conf.d/90-ttf-arphic-uming-embolden.conf
etc/fonts/conf.d/59-liberation-mono.conf
etc/fonts/conf.d/66-sil-nuosu.conf
etc/fonts/conf.d/65-1-vlgothic-gothic.conf
etc/fonts/conf.d/65-0-lohit-bengali.conf
etc/fonts/conf.d/20-unhint-small-dejavu-sans.conf
..................省略部分解压过程信息..................

2.grep命令

  grep命令用于在文本中执行关键词搜索,并显示匹配的结果,格式为"grep [选项] [文件]"

              表2-15   grep 命令的参数及其作用

参数 作用
-b 将可执行文件(binary)当作文本文件(text)来搜索
-c 仅显示找到的行数
-i 忽略大小写
-n 显示行号
-v 反向选择--仅列出没有"关键词"的行

  两个最常用的参数:-n参数用于显示搜索到信息的行号;-v参数用于反选信息(即没有包含关键词的所有信息行); 详细参数可以使用man grep命令查询

  在Liunx系统中,/etc/passwd文件保存着所有的用户信息,而一旦用户的登录终端被设置成/sbin/nologin,则不再允许登录系统,因此可以使用grep命令来查找当前系统中不允许登录系统的所有用户信息:

[root@linuxprobe ~]# grep /sbin/nologin /etc/passwd
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
..................省略部分输出过程信息..................

3.find命令

  find命令按照指定文件来查找文件,格式为 "find [查找路径] 寻找文件 操作".
  在Liunx系统中,搜索工作一般都是通过find 命令来完成的,它可以使用不同的文件特性作为寻找条件(如文件名、大小、修改时间、权限等信息),一旦匹配成功则默认将信息显示到屏幕上.

              表2-16   find 命令的参数及其作用

参数 作用
-name 匹配名称
-perm 匹配权限
-user 匹配所有者
-group 匹配所有组
-mtime -n +n 匹配修改内容的时间(-n 指n天以内, +n指n天以前)
-atime -n +n 匹配访问文件的时间(-n 指 n 天以内,+n 指 n 天以前)
-ctime -n +n 匹配修改文件权限的时间(-n 指 n 天以内,+n 指 n 天以前)
-nouser 匹配无所有者的文件
-nogroup 匹配无所有组的文件
-newer f1 !f2 匹配比文件 f1 新但比 f2 旧的文件
--type b/d/c/p/l/f 匹配文件类型(后面的字母参数依次表示块设备、目录、字符设备、管b/d/c/p/l/f 道、链接文件、文本文件)
-size 匹配文件的大小(+50KB 为查找超过 50KB 的文件,而-50KB 为查找小-size于 50KB 的文件)
-prune 忽略某个目录
-exec ...... {}; 后面可跟用于进一步处理搜索结果的命令(下文会有演示)

  如果要想获取到该目录中所有以 host 开头的文件列表,
可以执行如下命令:

[root@linuxprobe ~]# find /etc -name "host*" -print
/etc/avahi/hosts
/etc/host.conf
/etc/hosts
/etc/hosts.allow
/etc/hosts.deny
/etc/selinux/targeted/modules/active/modules/hostname.pp
/etc/hostname

你可能感兴趣的:(Ubuntu常用命令学习 (2))