Linux命令行基础知识

Linux命令行

Linux使用命令行与操作系统进行交互,通过在命令行输入命令进行程序运行、文件创建与编辑、查询系统信息等,命令行是Linux的精髓呢。默认我们通过命令行登录运行的程序是Shell(壳程序),我们通过在shell上输入命令进行操作。

之前我们已经大概了解了Linux系统一些常识,以及通过虚拟机安装了Linux系统进行学习,现在开始学习下在Linux中执行命令实现交互。

基本命令操作

首先看下登录到系统是什么样子,分为几个部分。

[root@localhost ~]#
root:当前登录用户
localhost:服务器的计算机名称,和windows的计算机名一致
~ :当前目录,这个~代表默认用户家目录
# :这个代表当前是root用户,如果是普通用户显示为$符合

命令格式:

[root@localhost ~]# command [-options] parameter1 parameter2
command:命令或可执行文件,如:ls pwd cd top等等,系统有很多预配置的功能命令
[-options]:可选项,有两种格式比如-h,-a,有时候也会有--表示,比如--help
parameter:表示命令的参数或者选项的参数
命令和可选项和参数之间用空格隔开,多个空格也表示为一个

# 示例1:显示当前目录内容,按详细信息和时间排序
[root@localhost ~]# ls -latr
总用量 3622724
-rw-r--r--.  1 root root       129 5月  11 2019 .tcshrc
-rw-r--r--.  1 root root       100 5月  11 2019 .cshrc
-rw-r--r--.  1 root root       176 5月  11 2019 .bashrc
-rw-r--r--.  1 root root       176 5月  11 2019 .bash_profile
-rw-r--r--.  1 root root        18 5月  11 2019 .bash_logout
drwxr-xr-x.  6 root root        65 6月  21 2021 vmtools
-rw-r--r--   1 root root 733017638 2月  22 2022 tb_sku1.sql
-rw-r--r--   1 root root 737475921 2月  22 2022 tb_sku2.sql
-rw-r--r--   1 root root 737525348 2月  22 2022 tb_sku4.sql
-rw-r--r--   1 root root 737504035 2月  22 2022 tb_sku3.sql
-rw-r--r--   1 root root 737508325 2月  22 2022 tb_sku5.sql
可选项可以写多个,多个-a -l -s 可以简写为-las,上面是没有参数的。

# 示例2:在当前目录下创建多层目录
[root@localhost ~]# mkdir -p ./test/a
[root@localhost ~]# tree ./test
./test
└── a

1 directory, 0 files
-p :创建多层嵌套目录

# 示例3:没有添加可选项和参数的命令
ost ~]# pwd
/root
pwd:返回当前目录

# 示例4:命令+选项+参数一行代表一条命令被执行,如果命令过长可以使用\分行
./configure --user=nginx \
--group=nginx \
--prefix=/www/nginx \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-stream \
--with-http_gzip_static_module \
--with-http_sub_module

上面使用的编译nginx的一个命令,使用configure这个程序文件,添加选项进行编译

下面是一些常用命令:

命令 功能
cd 用于更改当前工作目录
ls 用于列出当前目录中的文件和子目录
pwd 用于显示当前工作目录的路径
mkdir 用于创建新的目录
rm 用于删除文件和目录(这个命令需要慎用)
cp 用于复制文件和目录
shutdown 用于关闭或重新启动计算机
history 显示历史命令,可以用来重复执行之前执行过的命令。
mv 用于移动或重命名文件和目录
cat 用于显示文件内容,一般用于文本内容显示,可执行文件显示乱码
more & less 用于分页显示文件内容
head & tail 用于显示文件的开头和结尾部分
chmod 用于改变文件或目录的权限
chown 用于改变文件或目录的所有者和所属组
ps 用于查看当前进程状态
df 用于显示磁盘空间使用情况
kill 用于终止进程,类似任务管理器终止任务
grep 用于在文件中搜索文本
find 用于在目录中搜索文件
wget 用于从网络下载文件
curl 用于从网络下载文件
ping 用于测试网络连接

上面的命令很多都有对应的选项和参数进行选择,而且很多命令可以集成使用,示例:

[root@localhost ~]# ps -aux | grep nginx
root      740893  0.0  0.0  85896   824 ?        Ss   10月29   0:00 nginx: master process ./nginx
nginx     740894  0.0  0.2 114076 22524 ?        S    10月29   0:00 nginx: worker process
root     4135185  0.0  0.0  12352  1088 pts/0    S+   13:46   0:00 grep --color=auto nginx
上面先通过ps命令查询当前进程,然后通过|管道传输到下一个命令,通过grep过滤包含nginx的进程。

在Linux上命令的使用是非常灵活的,可以实现非常强大的功能。

命令与信息查询

--help

上面给了一些常用的Linux命令,但是呢我没有写上参数,因为每个命令都有对应的参数,太多了根本写不完,但是我们也不需要死记硬背,可以通过命令的帮助功能查询,非常方便,大多数情况下可以通过--help查询该命令有什么选项。

[root@localhost ~]# ls --help
用法:ls [选项]... [文件]...
List information about the FILEs (the current directory by default).
Sort entries alphabetically if none of -cftuvSUX nor --sort is specified.

必选参数对长短选项同时适用。
  -a, --all                     不隐藏任何以. 开始的项目
  -A, --almost-all              列出除. 及.. 以外的任何项目
      --author                  与-l 同时使用时列出每个文件的作者
  -b, --escape                  以八进制溢出序列表示不可打印的字符
      --block-size=SIZE      with -l, scale sizes by SIZE when printing them;
                               e.g., '--block-size=M'; see SIZE format below
  -B, --ignore-backups       do not list implied entries ending with ~
  -c                         with -lt: sort by, and show, ctime (time of last
                               modification of file status information);
                               with -l: show ctime and sort by name;
                               otherwise: sort by ctime, newest first
  -C                         list entries by columns
      --color[=WHEN]         colorize the output; WHEN can be 'always' (default
                               if omitted), 'auto', or 'never'; more info below
  -d, --directory            list directories themselves, not their contents
  -D, --dired                generate output designed for Emacs' dired mode
  -f                         do not sort, enable -aU, disable -ls --color
  -F, --classify             append indicator (one of */=>@|) to entries
      --file-type            likewise, except do not append '*'
      --format=WORD          across -x, commas -m, horizontal -x, long -l,
                               single-column -1, verbose -l, vertical -C
      --full-time            like -l --time-style=full-iso
  -g                            类似-l,但不列出所有者
      --group-directories-first
                             group directories before files;
                               can be augmented with a --sort option, but any
                               use of --sort=none (-U) disables grouping
  -G, --no-group             in a long listing, don't print group names
  -h, --human-readable       with -l and -s, print sizes like 1K 234M 2G etc.
      --si                   likewise, but use powers of 1000 not 1024
  -H, --dereference-command-line
                             follow symbolic links listed on the command line
      --dereference-command-line-symlink-to-dir
                             follow each command line symbolic link
                               that points to a directory
      --hide=PATTERN         do not list implied entries matching shell PATTERN
                               (overridden by -a or -A)
      --hyperlink[=WHEN]     hyperlink file names; WHEN can be 'always'
                               (default if omitted), 'auto', or 'never'
      --indicator-style=WORD  append indicator with style WORD to entry names:
                               none (default), slash (-p),
                               file-type (--file-type), classify (-F)
  -i, --inode                print the index number of each file
  -I, --ignore=PATTERN       do not list implied entries matching shell PATTERN
  -k, --kibibytes            default to 1024-byte blocks for disk usage;
                               used only with -s and per directory total

# 而且里面解析非常详细,有两种写法,短格式-和长格式--,而且其实我们平时用不到那么多,只需要一些常用的记住一些就行了,熟能生巧而已。

man

也可以通过man查询命令的详细信息。man后面接要查询的命令,然后进入一个类似说明文件,使用上下左右方向键进行移动,按键q退出。 快捷键说明:

按键 功能
空格键 向下翻一页
[Page Down] 向下翻一页
[Page Up] 向上翻一页
[Home] 去到第一页
[End] 最后一页
/string 向下查询,string代表需要查询的字符串
?string 向上查询,string代表需要查询的字符串
n,N 通过/或者?查询结果之后使用这两个按键进行查询结果上一个和下一个跳转
q 退出man查询
[root@localhost ~]# man ls
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

Linux中一些常用按键

tab : 如果使用CRT等工具连接可以使用tab键进行补全
Ctrl+C : 退出当前命令操作

好了,本次学习了命令的格式与执行,一些常用的命令和查询用法,下次继续。

本文由 mdnice 多平台发布

相关推荐

  1. Linux命令基础知识

    2023-12-18 23:38:05       68 阅读
  2. Linux系统基础命令指令——Ubuntu

    2023-12-18 23:38:05       34 阅读
  3. Linux/Ubuntu/Debian基本命令命令历史记录

    2023-12-18 23:38:05       40 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-18 23:38:05       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-18 23:38:05       106 阅读
  3. 在Django里面运行非项目文件

    2023-12-18 23:38:05       87 阅读
  4. Python语言-面向对象

    2023-12-18 23:38:05       96 阅读

热门阅读

  1. 数据结构基础小结

    2023-12-18 23:38:05       55 阅读
  2. autosar是rtos的一种

    2023-12-18 23:38:05       54 阅读
  3. Python文件命名规则:批量重命名与规则匹配的文件

    2023-12-18 23:38:05       55 阅读
  4. 记错vue3+ts require 报错

    2023-12-18 23:38:05       59 阅读
  5. 3.4【窗口】窗口的几何形状(三,窗口旋转)

    2023-12-18 23:38:05       56 阅读
  6. 抖音ip地址切换会看不到视频吗

    2023-12-18 23:38:05       100 阅读
  7. AI全栈大模型工程师(十二)AI的未来

    2023-12-18 23:38:05       59 阅读