Linux中的常见命令

1 查看系统相关信息命令

(1)查看内核版本

uname -r

man命令总结linux常用基本命令用法以及查看帮助文档的方法_重启

man命令总结linux常用基本命令用法以及查看帮助文档的方法_重启_02

(2) 显示操作系统发行版本

cat /etc/os-release

man命令总结linux常用基本命令用法以及查看帮助文档的方法_git_03

man命令总结linux常用基本命令用法以及查看帮助文档的方法_重启_04

(3)查看当前主机名

hostname

man命令总结linux常用基本命令用法以及查看帮助文档的方法_重启_05

man命令总结linux常用基本命令用法以及查看帮助文档的方法_ubuntu_06

2 查看硬件信息

(1)查看CPU

lscpu

man命令总结linux常用基本命令用法以及查看帮助文档的方法_ubuntu_07

man命令总结linux常用基本命令用法以及查看帮助文档的方法_git_08

cat /proc/cpuinfo

man命令总结linux常用基本命令用法以及查看帮助文档的方法_ubuntu_09

man命令总结linux常用基本命令用法以及查看帮助文档的方法_ubuntu_10


(2) 查看内存大小

free -h

man命令总结linux常用基本命令用法以及查看帮助文档的方法_git_11

cat /proc/meminfo

man命令总结linux常用基本命令用法以及查看帮助文档的方法_ubuntu_12

(3) 查看硬盘分区情况

lsblk

man命令总结linux常用基本命令用法以及查看帮助文档的方法_重启_13

cat /proc/partitions

man命令总结linux常用基本命令用法以及查看帮助文档的方法_git_14

3 退出和注销

exit注销用户,退出系统

man命令总结linux常用基本命令用法以及查看帮助文档的方法_ubuntu_15

man命令总结linux常用基本命令用法以及查看帮助文档的方法_git_16

quit退出当前程序

man命令总结linux常用基本命令用法以及查看帮助文档的方法_git_17

4 关机重启相关命令

(1) 关机:

init 0

halt

poweroff

shutdown -h now

(2) 重启

unit 6

reboot

shutdown -r now


关机或重启:shutdown

shutdown - Halt, power off or reboot the machine 停止、关闭或重新启动机器

shutdown [OPTIONS...] [TIME] [WALL...]

 OPTIONS

--help
       Print a short help text and exit.

   -H, --halt 
       Halt the machine.

   -P, --poweroff
       Power the machine off (the default).

   -r, --reboot
       Reboot the machine.

   -h
       The same as --poweroff, but does not override the action to take if it is "halt". E.g.  shutdown --reboot -h means "poweroff", but shutdown --halt -h means "halt".

   -k
       Do not halt, power off, or reboot, but just write the wall message.

   --no-wall
       Do not send wall message before halt, power off, or reboot.

   -c
       Cancel a pending shutdown. This may be used to cancel the effect of an invocation of shutdown with a time argument that is not "+0" or "now".

   --show
       Show a pending shutdown action and time if there is any.


shutdown -h没有时间参数时,会在1分钟左右之后关机。使用shutdown -c可以取消1分钟后的关机动作。接参数now表示立即执行关机动作。

man命令总结linux常用基本命令用法以及查看帮助文档的方法_git_18


5 日期和时间

Linux系统上有两种时钟:

硬件时钟 由计算机内部的物理芯片计时,由主板的电池提供电源供电。

系统时钟 从硬件时钟读取并设定时间,或者从网络NTP服务器同步时间。

(1) date显示或设置系统时间日期

        date [OPTION]... [+FORMAT]

       date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]

DESCRIPTION

             Display the current time in the given FORMAT, or set the system date.

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

   -d, --date=STRING
          display time described by STRING, not 'now'

   --debug
          annotate the parsed date, and warn about questionable usage to stderr

   -f, --file=DATEFILE
          like --date; once for each line of DATEFILE

   -I[FMT], --iso-8601[=FMT]
          output  date/time  in  ISO  8601 format.  FMT='date' for date only (the default), 'hours', 'minutes', 'seconds', or 'ns' for date and time to the indicated precision.
          Example: 2006-08-14T02:34:56-06:00

   -R, --rfc-email
          output date and time in RFC 5322 format.  Example: Mon, 14 Aug 2006 02:34:56 -0600

   --rfc-3339=FMT
          output date/time in RFC 3339 format.  FMT='date', 'seconds', or 'ns' for date and time to the indicated precision.  Example: 2006-08-14 02:34:56-06:00

   -r, --reference=FILE
          display the last modification time of FILE

   -s, --set=STRING
          set time described by STRING

   -u, --utc, --universal
          print or set Coordinated Universal Time (UTC)

   --help display this help and exit

   --version
          output version information and exit

   FORMAT controls the output.  Interpreted sequences are:

   %%     a literal %

   %a     locale's abbreviated weekday name (e.g., Sun)

   %A     locale's full weekday name (e.g., Sunday)

   %b     locale's abbreviated month name (e.g., Jan)

   %B     locale's full month name (e.g., January)

   %c     locale's date and time (e.g., Thu Mar  3 23:05:25 2005)

   %C     century; like %Y, except omit last two digits (e.g., 20)

   %d     day of month (e.g., 01)

   %D     date; same as %m/%d/%y

   %e     day of month, space padded; same as %_d

   %F     full date; like %+4Y-%m-%d

   %g     last two digits of year of ISO week number (see %G)

   %G     year of ISO week number (see %V); normally useful only with %V

   %h     same as %b

   %H     hour (00..23)

   %I     hour (01..12)

   %j     day of year (001..366)

   %k     hour, space padded ( 0..23); same as %_H

   %l     hour, space padded ( 1..12); same as %_I

   %m     month (01..12)

   %M     minute (00..59)

   %n     a newline

   %N     nanoseconds (000000000..999999999)

   %p     locale's equivalent of either AM or PM; blank if not known

   %P     like %p, but lower case

   %q     quarter of year (1..4)

   %r     locale's 12-hour clock time (e.g., 11:11:04 PM)

   %R     24-hour hour and minute; same as %H:%M

   %s     seconds since 1970-01-01 00:00:00 UTC

   %S     second (00..60)

   %t     a tab

   %T     time; same as %H:%M:%S

   %u     day of week (1..7); 1 is Monday

   %U     week number of year, with Sunday as first day of week (00..53)

   %V     ISO week number, with Monday as first day of week (01..53)

   %w     day of week (0..6); 0 is Sunday

   %W     week number of year, with Monday as first day of week (00..53)

   %x     locale's date representation (e.g., 12/31/99)

   %X     locale's time representation (e.g., 23:13:48)

   %y     last two digits of year (00..99)

   %Y     year

   %z     +hhmm numeric time zone (e.g., -0400)

   %:z    +hh:mm numeric time zone (e.g., -04:00)

   %::z   +hh:mm:ss numeric time zone (e.g., -04:00:00)

   %:::z  numeric time zone with : to necessary precision (e.g., -04, +05:30)

   %Z     alphabetic time zone abbreviation (e.g., EDT)

   By default, date pads numeric fields with zeroes.  The following optional flags may follow '%':
   By default, date pads numeric fields with zeroes.  The following optional flags may follow '%':

   -      (hyphen) do not pad the field

   _      (underscore) pad with spaces

   0      (zero) pad with zeros

   +      pad with zeros, and put '+' before future years with >4 digits

   ^      use upper case if possible

   #      use opposite case if possible

   After any flags comes an optional field width, as a decimal number; then an optional modifier, which is either E to use the locale's alternate representations if  available,
   or O to use the locale's alternate numeric symbols if available.

显示系统时间日期

man命令总结linux常用基本命令用法以及查看帮助文档的方法_重启_19

带有格式的示例

[root@ubuntu2204 10:38:45 ~]# date +%F 显示日期年月日

2023-11-10

[root@ubuntu2204 10:49:04 ~]# date "+%F %T" 显示日期时间

2023-11-10 10:49:53

[root@ubuntu2204 10:50:55 ~]# date -d '-1 day' +%F 显示昨天的日期

2023-11-09

root@ubuntu2204 10:51:07 ~]# date 101110542023.50 设置系统时间为2023年11月10号10点54分50秒

[root@ubuntu2204 10:57:06 ~]# date -s '1 year' +%F 设置当前日期为明年

2024-11-10

(2) 查看当前月历,查看过2020年9月的日历

man命令总结linux常用基本命令用法以及查看帮助文档的方法_git_20

(3)操作硬件时钟

hwclock/clock,显示操作时钟同步

hwclock [function] [option...]

-s|--hctosys #以硬件时钟为准,校正系统时钟

-w|--systohc #以系统时钟为准,校正硬件时钟

[root@rocky86 ~]# hwclock -s 以硬件时钟为准调整时钟

(4)时区

ll /etc/localtime 查看本地时区

man命令总结linux常用基本命令用法以及查看帮助文档的方法_重启_21

[root@ubuntu2204 ~]# timedatectl list-timezones 查看时区列表

man命令总结linux常用基本命令用法以及查看帮助文档的方法_git_22

[root@ubuntu2204 ~]# timedatectl set-timezone Asia/Shanghai 设置时区为上海时区

[root@ubuntu2204 ~]# timedatectl status 查看设置结果

man命令总结linux常用基本命令用法以及查看帮助文档的方法_ubuntu_23

6 用户登录信息相关命令

whoami命令:显示当前登录有效用户

who命令:显示当前所有的登录会话

w命令:显示系统当前所有登录的会话及所做的操作

man命令总结linux常用基本命令用法以及查看帮助文档的方法_重启_24

7 echo命令用法

echo命令将字符串显示在屏幕上(标准输出)

echo [SHORT-OPTION]... [STRING]...

echo LONG-OPTION

DESCRIPTION Echo the STRING(s) to standard output.

-n     do not output the trailing newline

   -e     enable interpretation of backslash escapes

   -E     disable interpretation of backslash escapes (default)

   --help display this help and exit

   --version
          output version information and exit

-n:不自动附加换行符;即在一行显示

-e:启用转义符

-E:不启用转义符(echo默认行为)

man命令总结linux常用基本命令用法以及查看帮助文档的方法_git_25

echo命令可以查看变量的值

man命令总结linux常用基本命令用法以及查看帮助文档的方法_ubuntu_26

8 查看命令帮助文档

(1)whatis & whereis

      命令whatis快速的查看命令或相关内容的简短功能。whatis等同于man -f。whatis基于数据库来查找的。初始安装的系统需要创建数据库

     在CentOS 6以前的版本,使用makewhatis创建数据库

     在较新的版本中使用mandb

man命令总结linux常用基本命令用法以及查看帮助文档的方法_git_27

man命令总结linux常用基本命令用法以及查看帮助文档的方法_ubuntu_28

     命令whereis 可以列出命令或系统文件路径,以及其对应的man 手册中的文档路径

man命令总结linux常用基本命令用法以及查看帮助文档的方法_重启_29

(2)内部和外部命令使用帮助

区分内部命令和外部命令

type COMMAND

man命令总结linux常用基本命令用法以及查看帮助文档的方法_重启_30

显示shell builtin是内部命令.

man命令总结linux常用基本命令用法以及查看帮助文档的方法_git_31

ls是一个别名,man命令是外部命令,cd是内部命令

①内部命令获得帮助

help COMMAND

#内部命令都在bash中,包括一些全局变量,可以在man手册中查看全部bash信息

help不接参数 显示所有的内部命令

man命令总结linux常用基本命令用法以及查看帮助文档的方法_ubuntu_32

help echo显示echo命令的具体用法

man命令总结linux常用基本命令用法以及查看帮助文档的方法_重启_33

②外部命令获得帮助

COMMAND --help/-h

man命令总结linux常用基本命令用法以及查看帮助文档的方法_ubuntu_34

   外部命令不可以使用help command的形式,需要使用--help或-h来获得帮助

man命令总结linux常用基本命令用法以及查看帮助文档的方法_git_35

man命令总结linux常用基本命令用法以及查看帮助文档的方法_git_36

③使用man获得帮助

man 是一个外部命令,基本质就是读取特定文件,进行输出,其读取的文件一般位于/usr/share/man/目录下

新安装一个软件或程序后,对应的man手册也会放到/usr/share/man/目录下,几乎每个命令都有man的 “页面”

标准man 手册一般有以下几个章节:

  1. 可执行程序或 shell 命令
  2. 系统调用(内核提供的函数)
  3. 库调用(程序库中的函数)
  4. 特殊文件(通常位于 /dev)
  5. 文件格式和规范,如 /etc/passwd
  6. 游戏
  7. 杂项
  8. 系统管理命令(通常只针对 root 用户)
  9. 内核API

NAME man - an interface to the system reference manuals

SYNOPSIS man [man options] [[section] page ...] ... man -k [apropos options] regexp ... man -K [man options] [section] term ... man -f [whatis options] page ... man -l [man options] file ... man -w|-W [man options] page ...

DESCRIPTION man is the system's manual pager. Each page argument given to man is normally the name of a program, utility or function. The manual page associated with each of these ar? guments is then found and displayed. A section, if provided, will direct man to look only in that section of the manual. The default action is to search in all of the available sections following a pre-defined order (see DEFAULTS), and to show only the first page found, even if page exists in several sections.

The table below shows the section numbers of the manual followed by the types of pages they contain.

   The table below shows the section numbers of the manual followed by the types of pages they contain.

   1   Executable programs or shell commands
   2   System calls (functions provided by the kernel)
   3   Library calls (functions within program libraries)
   4   Special files (usually found in /dev)
   5   File formats and conventions, e.g. /etc/passwd
   6   Games
   7   Miscellaneous (including macro packages and conventions), e.g. man(7), groff(7), man-pages(7)
   8   System administration commands (usually only for root)
   9   Kernel routines [Non standard]

   A manual page consists of several sections.

   Conventional  section  names  include  NAME,  SYNOPSIS,  CONFIGURATION, DESCRIPTION, OPTIONS, EXIT STATUS, RETURN VALUE, ERRORS, ENVIRONMENT, FILES, VERSIONS, CONFORMING TO,
   NOTES, BUGS, EXAMPLE, AUTHORS, and SEE ALSO.

   The following conventions apply to the SYNOPSIS section and can be used as a guide in other sections.

   bold text          type exactly as shown.
   italic text        replace with appropriate argument.
   [-abc]             any or all arguments within [ ] are optional.
   -a|-b              options delimited by | cannot be used together.
   argument ...       argument is repeatable.
   [expression] ...   entire expression within [ ] is repeatable.

   Exact rendering may vary depending on the output device.  For instance, man will usually not be able to render italics when running in a terminal, and will typically use un?
   derlined or coloured text instead.

   The  command  or function illustration is a pattern that should match all possible invocations.  In some cases it is advisable to illustrate several exclusive invocations as
   is shown in the SYNOPSIS section of this manual page.

EXAMPLES man ls Display the manual page for the item (program) ls.

[root@ubuntu2204 12:37:53 ~]# man echo

man命令总结linux常用基本命令用法以及查看帮助文档的方法_重启_37

使用q退出man手册,空格键翻屏

info不常用,不过多介绍了

9 本地帮助文档/usr/share/doc

多数安装软件在/usr/share/doc目录下生成子目录,其常见的帮助文档:README、INSTALL、CHANGES等。