ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

Linux 教程(手册):Linux 初学者必了解的概念(常用命令以及操作解析)

2022-02-21 23:31:17  阅读:212  来源: 互联网

标签:-- Linux 命令 初学者 conf 常用命令 paxos ls


最近一段时间我打算出一些 Linux 下终端的基础命令使用文章。

如果你会使用 Linux,这些文章可以作为手册去查询。如果你从来没有接触过 Linux 系统的读者,那正好,看看不会有错的 (从入门到放弃系列)

我打算在所有文章的开头,都写一个 TLDR:太长了我才不看用于给读者或我自己快速参考(逃

所以这里面的内容可能并不是文章的摘要和文章内想要强调的。

所以正式重新开始 Linux 之旅吧

Linux 命令格式:

Usage: ls [OPTION]... [FILE]...
Usage: useradd [options] LOGIN
Usage: passwd [options] <accountName>
Usage: date [OPTION]... [+FORMAT]
    or:  date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]
  • [] 表示可选项
  • 大写文字 表示必选项
  • <> 表示变量,是必选项,但不写一般也有默认值
  • ... 表示一个列表,可以接多个选项或参数
  • 比如 date 命令:date +%Y%m date '+%Y-%m-%d %H:%M:%S'
  • [a|b|c] 多选一

查询帮助:

  • whatis (mandb)
  • man
  • info
  • /usr/share/doc

使用 cd 命令

  • cd 回家目录
  • cd - 回上一个工作目录
  • cd ~usernameusername 的家目录

使用 Linux

学习建议

在 Linux 中不要尝试去记住一切命令,而是要学会从系统、网络中获取帮助。

Linux 系统的命令你永远不可能全记住,所以学习方向应该是善于查找帮助,多敲命令,再配上shell补全来实现 “记住命令”

命令

命令的组成

一条完整的 Linux 命令一般构成:命令 选项 参数

命令 选项 参数 之间都是由空格分隔的。

多个命令如果无依赖关系用 ; 分隔(如果你要写在一行执行)

命令(command):用来实现某个功能
选项(options):用来修饰命令的行为
参数(arguments):要操作的对象 (比如文件、用户、目录)

选项

比如 passwd 这个命令,以 命令 + 参数 使用:

passwd 直接加参数就是修改密码

passwd paxos

现在加上选项, 命令 + 选项 + 参数 方式使用:

-l 选项是锁定用户(用户将不能用密码登录),passwd 命令被这个选项行为修改成了锁定密码

# 锁定密码
passwd -l paxos
# 解锁用户
passwd -u paxos

选项的长格式和短格式

Linux 中,长的选项一般都有长短格式。

选项的短格式一般情况下是为了更方便使用长格式。但是也有个别情况,因为不是所有的长格式都有对应的短格式,这全取决于开发者。

如下就是 passwd 命令的选项,可以看到它的长短格式:

paxos@Paxos-C26-2021:/etc/systemd$ passwd -h
Usage: passwd [options] [LOGIN]

Options:
  -a, --all                     report password status on all accounts
  -d, --delete                  delete the password for the named account
  -e, --expire                  force expire the password for the named account
  -h, --help                    display this help message and exit
  -k, --keep-tokens             change password only if expired
  -i, --inactive INACTIVE       set password inactive after expiration

在使用方面一般情况只有一个区别:

  • 短格式可以缩写在一起
  • 长格式不能缩写在一起

比如 ls 这个命令:

可以知道它有 -a / --all 选项用来显示所有文件,有 -s / --size 选项用来显示文件的大小

paxos@Paxos-C26-2021:/etc/systemd$ ls --help
Usage: ls [OPTION]... [FILE]...
Mandatory arguments to long options are mandatory for short options too.
  -a, --all                  do not ignore entries starting with .
  -s, --size                 print the allocated size of each file, in blocks

当你仅使用短格式时,短格式可以缩写在一块:

paxos@Paxos-C26-2021:/etc/systemd$ ls -a -s
total 36
0 .   4 journald.conf  0 network        4 pstore.conf    4 sleep.conf  4 system.conf     0 user
0 ..  4 logind.conf    4 networkd.conf  4 resolved.conf  0 system      4 timesyncd.conf  4 user.conf

# 短格式可以缩写在一块
paxos@Paxos-C26-2021:/etc/systemd$ ls -as
total 36
0 .   4 journald.conf  0 network        4 pstore.conf    4 sleep.conf  4 system.conf     0 user
0 ..  4 logind.conf    4 networkd.conf  4 resolved.conf  0 system      4 timesyncd.conf  4 user.conf

# 长格式不能缩写,只能 ls --all --size
paxos@Paxos-C26-2021:/etc/systemd$ ls --allsize
ls: unrecognized option '--allsize'
Try 'ls --help' for more information.

学会使用帮助

看到陌生命令,不要直接无脑复制执行。

  1. 先要搞清楚命令的含义

比如你在某度知道,某某贴吧网站看到了网友给你了几条陌生命令用来优化系统:

警告:下列命令请不要执行,非常危险。

rm -rf /      # 清理系统垃圾
chmod 000 -R /   # 优化系统,将系统缓存时间设置为0

看到这几条命令后,如果不认识,请不要直接无脑执行,而是去利用 Linux 一些自带帮助或在互联网上查询命令含义在执行:

比如先使用 whatisman 等命令查看该命令功能:

如果 whatis 提示 noting 可以试试使用 mandb 生成一下帮助数据库

  • rm : 用于删除文件或目录
  • chmod : 用于修改文件模式(属性)
paxos@Paxos-C26-2021:/etc/systemd$ whatis rm
rm (1)               - remove files or directories
paxos@Paxos-C26-2021:/etc/systemd$ whatis chmod
chmod (1)            - change file mode bits
  1. 查询命令的选项作用
  • 使用 命令 + --help 查询简短帮助
  • 使用 man + 命令 查询帮助手册
  • 使用 info + 命令 查询 info 帮助文档
  • 查询开发者写的文档(产品手册)比如在:/usr/share/doc 里的
  • 网络上搜索案例
paxos@Paxos-C26-2021:/etc/systemd$ ls --help
Usage: ls [OPTION]... [FILE]...
 -r, -R, --recursive   remove directories and their contents recursively
 -f, --force           ignore nonexistent files and arguments, never prompt

paxos@Paxos-C26-2021:/mnt/c/Users/Paxos$ chmod --help
Usage: chmod [OPTION]... MODE[,MODE]... FILE...
-R, --recursive        change files and directories recursively
Each MODE is of the form '[ugoa]*([-+=]([rwxXst]*|[ugo]))+|[-+=][0-7]+'.

通过查询我们知道了:

  • rm -rf / 递归式删除根目录所有文件
  • chmod 是修改文件属性(mode),000 不清楚。大概是递归式修改根目录下所有文件属性为 000

然后使用 man 来查询 chmod 的帮助文档

 A  numeric mode is from one to four octal digits (0-7), derived by adding up the bits with values 4, 2, and 1.
       Omitted digits are assumed to be leading zeros.  The first digit selects the set user ID (4) and set group  ID
       (2)  and  restricted deletion or sticky (1) attributes.  The second digit selects permissions for the user who
       owns the file: read (4), write (2), and execute (1); the third selects permissions  for  other  users  in  the
       file's group, with the same values; and the fourth for other users not in the file's group, with the same val‐
       ues.

就可以知道设置成 0 读写执行三个权限一个都会没。

所以最终上面的命令用途是这样的,而不是热心网友所描述的那种效果:

rm -rf /         # 删除根目录所有文件(系统删到一半就会挂)
chmod 000 -R /   # 根目录所有文件读写执行权限全部设定为没有(系统会挂)

所以千万不要无脑执行别人给你的陌生命令。

man 帮助

命令简单使用:

man [章节] 命令或配置文件
man -k 搜索关键词

键盘操作:

上下键等常规功能按键操作与系统行为保持一致

命令效果
空格下翻页
回车向下翻页
g/G跳转到开头/结尾
/string搜索string关键词
n/N在找到的关键词之间前后跳转
q退出

存储目录:

paxos@Paxos-C26-2021 ~/1> ls /usr/share/man/
cs/  de/  fi/  hu/  it/  ko/    man3/  man7/  nl/  pt/     ro/  sl/  sv/  uk/     zh_TW/
da/  es/  fr/  id/  ja/  man1/  man5/  man8/  pl/  pt_BR/  ru/  sr/  tr/  zh_CN/

帮助手册章节:

常用:

  • 1 用户命令
  • 5 文件格式
  • 8 系统管理命令
   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)
   8   System administration commands (usually only for root)
   9   Kernel routines [Non standard]

帮助手册页面格式:

  • NAME
  • 命令和短介绍(就是你用 whatis 查询的)
  • EXAMPLES
  • 例子

info 帮助文件

这个比 man 详细一点,但是使用和帮助文档也会更复杂一些,比较耽误时间看。

info 查询的命令

Linux 文件系统

  • 文件和目录单根的树状结构
  • 文件系统从根目录开始,表示为 /
  • 大小写敏感(Windows一般不敏感)
  • 文件名称不能多于 255 字符

常见重要目录

目录作用都是约定俗成的,并不是所有东西都遵守

  • /root:超级用户root的家目录
  • /home/username:普通用户的家目录
  • /usr:安装的软件,共享库等目录
  • 重要的子目录有
    • /usr/bin:用户命令
    • /usr/sbin:系统管理员命令
    • /usr/local:本地自定义软件安装目录
  • /etc:系统的配置文件
  • /var:系统服务的数据,例如数据库文件,日志文件和网站内容等
  • /tmp:系统临时文件目录

以下两目录都是伪文件系统,并不是真实存在的,开机时候生成:

不要把文件存在这两地方

  • /proc
  • /sys

常见有趣的点

WIP 持续更新中

cd 命令

  1. cd ..

使用 ls 能看到 .. 是个目录,代表上一个目录。 . 代表当前目录

paxos@Paxos-C26-2021 ~/1> ls  -al
total 0
drwxr-xr-x 1 paxos paxos 4096 Sep 17 18:46 ./
drwxr-xr-x 1 paxos paxos 4096 Sep 17 18:21 ../
  1. 快速一键回到家目录

你是不是常用 cd ~,其实直接输入 cd 回车就行了。

  1. 快速回到上一个工作目录

只需要输入 cd -

  1. 快速去其他用户家目录
cd ~username

标签:--,Linux,命令,初学者,conf,常用命令,paxos,ls
来源: https://blog.csdn.net/qq_52023825/article/details/123058483

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有