基本指令操作
一.shell
1.Linux系统中运行的一种特殊程序
2.在用户和内核之间之间充当“翻译官”
3.用户登陆Linux系统时,自动加载一个Shell程序
4.Bash是Linux系统中默认使用的Shell程序
文件位于/bin/bash
二.linux命令的分类
内部命令 | 外部命令 |
---|---|
集成于shell解释器程序内部的一些特殊指令,也称为内建(Built-in)指令 | linux系统中能够完成指定功能的脚本文件或二进制程序 |
属于Shell的一部分 | 属于Shell解释器程序以外的命令 |
没有单独对应的系统文件 | 每个外部命令对应了系统中的一个文件 |
自动载入内存,可直接使用 | 必须知道对应的文件位置,由Shell加载后才能执行/bin,/usr/bin等 |
三 .Linux命令行格式
命令字 [选项] [参数] 英文字符区分大小写
●命令字:操作
是整条命令中最关键的一部分
唯一确定一条命令
●选项:功能
- 短格式选项:使用“-”符号引导 (多个单字符选项可以组合在一起使用)
- 长格式选项:使用“–”符号引导 (后面是完整单词)
●参数:要执行的对象
命令字的处理对象
可以是文件名、目录(路径)名或用户名等内容
个数可以是零到多个
四.编辑Linux命令行辅助操作
按键 | 作用 |
---|---|
Tab键 | 自动补全 |
反斜杠“\” | 强制换行 |
Ctrl+U组合键 | 清空至行首 |
Ctrl+K | 清空至行尾 |
Ctrl+L | 清屏,或者clear |
Ctrl+C | 取消本次命令编辑 |
Ctrl+a | 切换到行首 |
Ctrl+e | 切换到行尾 |
五.获得命令帮助的方法
1.内部命令help:
查看Shell内部命令的帮助信息
[root@localhost data]# help
GNU bash, 版本 4.2.46(2)-release (x86_64-redhat-linux-gnu)
这些 shell 命令是内部定义的。请输入 `help' 以获取一个列表.
输入 `help 名称' 以得到有关函数`名称'的更多信息.
使用 `info bash' 来获得关于 shell 的更多一般性信息
使用 `man -k' 或 `info' 来获取不在列表中的命令的更多信息.
名称旁边的星号 (*) 意味着该命令被禁用.
job_spec [&] history [-c] [-d 偏移量] [n] 或 h>
(( 表达式 )) if 命令; then 命令; [ elif 命令>
. 文件名 [参数] jobs [-lnprs] [任务声明 ...] 或 >
: kill [-s 信号声明 | -n 信号编
[ 参数... ] let 参数 [参数 ...]
[[ 表达式 ]] local [option] 名称[=值] ...
alias [-p] [名称[=值] ... ] logout [n]
bg [任务声明 ...] mapfile [-n 计数] [-O 起始序号]>
bind [-lpvsPVS] [-m 键映射] [-f 文> popd [-n] [+N | -N]
break [n] printf [-v var] 格式 [参数]
builtin [shell 内嵌 [参数 ...]] pushd [-n] [+N | -N | 目录]
caller [表达式] pwd [-LP]
case 词 in [模式 [| 模式]...) 命> read [-ers] [-a 数组] [-d 分隔符>
cd [-L|[-P [-e]]] [dir] readarray [-n 计数] [-O 起始序卾
command [-pVv] 命令 [参数 ...] readonly [-aAf] [name[=value] ...] or>
compgen [-abcdefgjksuv] [-o 选项] [> return [n]
complete [-abcdefgjksuv] [-pr] [-DE] [> select NAME [in 词语 ... ;] do 命
compopt [-o|+o 选项] [-DE] [名称 .> set [-abefhkmnptuvxBCHP] [-o option-n>
continue [n] shift [n]
coproc [名称] 命令 [重定向] shopt [-pqsu] [-o] [选项名 ...]
declare [-aAfFgilrtux] [-p] [name[=val> source 文件名 [参数]
dirs [-clpv] [+N] [-N] suspend [-f]
disown [-h] [-ar] [任务声明 ...] test [表达式]
echo [-neE] [参数 ...] time [-p] 管道
enable [-a] [-dnps] [-f 文件名] [厾 times
eval [参数 ...] trap [-lp] [[参数] 信号声明 ...>
exec [-cl] [-a 名称] [命令 [参数> 真
exit [n] type [-afptP] 名称 [名称 ...]
export [-fn] [名称[=值] ...] 或 ex> typeset [-aAfFgilrtux] [-p] name[=val>
伪 ulimit [-SHacdefilmnpqrstuvx] [限制>
fc [-e 编辑器名] [-lnr] [起始] [> umask [-p] [-S] [模式]
fg [任务声明] unalias [-a] 名称 [名称 ...]
for 名称 [in 词语 ... ] ; do 命乾 unset [-f] [-v] [名称 ...]
for (( 表达式1; 表达式2; 表达㼠 until 命令; do 命令; done
function 名称 {
命令 ; } 或 name > variables - 一些 shell 变量的名>
getopts 选项字符串 名称 [参数> wait [编号]
hash [-lr] [-p 路径名] [-dt] [名祾 while 命令; do 命令; done
help [-dms] [模式 ...] {
命令 ; }
[root@localhost data]#
2.命令的“- -help”选项
适用于大多数Linux外部命令
[root@localhost data]# ls --help
用法:ls [选项]... [文件]...
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 不隐藏任何以. 开始的项目
-A, --almost-all 列出除. 及.. 以外的任何项目
--author 与-l 同时使用时列出每个文件的作者
-b, --escape 以八进制溢出序列表示不可打印的字符
--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 ~
-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 'never', 'auto',
or 'always' (the default); 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 以一个长列表的形式,不输出组名
-h, --human-readable 与-l 一起,以易于阅读的格式输出文件大小
(例如 1K 234M 2G)
--si 同上面类似,但是使用1000 为基底而非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)
--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
-l 使用较长格式列出信息
-L, --dereference 当显示符号链接的文件信息时,显示符号链接所指示
的对象而并非符号链接本身的信息
-m 所有项目以逗号分隔,并填满整行行宽
-n, --numeric-uid-gid 类似 -l,但列出UID 及GID 号
-N, --literal 输出未经处理的项目名称 (如不特别处理控制字符)
-o 类似 -l,但不列出有关组的信息
-p, --indicator-style=slash 对目录加上表示符号"/"
-q, --hide-control-chars print ? instead of nongraphic characters
--show-control-chars show nongraphic characters as-is (the default,
unless program is 'ls' and output is a terminal)
-Q, --quote-name enclose entry names in double quotes
--quoting-style=WORD use quoting style WORD for entry names:
literal, locale, shell, shell-always, c, escape
-r, --reverse 逆序排列
-R, --recursive 递归显示子目录
-s, --size 以块数形式显示每个文件分配的尺寸
-S sort by file size
--sort=WORD sort by WORD instead of name: none (-U), size (-S),
time (-t), version (-v), extension (-X)
--time=WORD with -l, show time as WORD instead of default
modification time: atime or access or use (-u)
ctime or status (-c); also use specified time
as sort key if --sort=time
--time-style=STYLE with -l, show times using style STYLE:
full-iso, long-iso, iso, locale, or +FORMAT;
FORMAT is interpreted like in 'date'; if FORMAT
is FORMAT1<newline>FORMAT2, then FORMAT1 applies
to non-recent files and FORMAT2 to recent files;
if STYLE is prefixed with 'posix-', STYLE
takes effect only outside the POSIX locale
-t sort by modification time, newest first
-T, --tabsize=COLS assume tab stops at each COLS instead of 8
-u with -lt: sort by, and show, access time;
with -l: show access time and sort by name;
otherwise: sort by access time
-U do not sort; list entries in directory order
-v natural sort of (version) numbers within text
-w, --width=COLS assume screen width instead of current value
-x list entries by lines instead of by columns
-X sort alphabetically by entry extension
-1 list one file per line
SELinux options:
--lcontext Display security context. Enable -l. Lines
will probably be too wide for most displays.
-Z, --context Display security context so it fits on most
displays. Displays only mode, user, group,
security context and file name.
--scontext Display only security context and file name.
--help 显示此帮助信息并退出
--version 显示版本信息并退出
SIZE is an integer and optional unit (example: 10M is 10*1024*1024). Units
are K, M, G, T, P, E, Z, Y (powers of 1024) or KB, MB, ... (powers of 1000).
使用色彩来区分文件类型的功能已被禁用,默认设置和 --color=never 同时禁用了它。
使用 --color=auto 选项,ls 只在标准输出被连至终端时才生成颜色代码。
LS_COLORS 环境变量可改变此设置,可使用 dircolors 命令来设置。
退出状态:
0 正常
1 一般问题 (例如:无法访问子文件夹)
2 严重问题 (例如:无法使用命令行参数)
GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
请向<http://translationproject.org/team/zh_CN.html> 报告ls 的翻译错误
要获取完整文档,请运行:info coreutils 'ls invocation'
[root@localhost data]#
3.使用man(manual 手册)命令阅读手册页
●使用“⬆”、“⬇” (ENTER)方向键滚动一行文本
●使用Page Up和Page Down (空格) 键翻页
●按Q或q键退出阅读环境
●按“/"键查找内容,n键向下查找,N键向上查找
4.百度
六.查看当前的工作目录 pwd
pwd ---显示用户当前所在的工作目录位置
七.切换工作目录 cd
cd ---将用户的工作目录更改到其他位置
cd 目标位置 切换到目标位置
cd 或cd 若不指定目标位置,切换到当前用户的宿主目录
cd - 切换到上一次执行cd命令之前所在的目录
八.相对路径、绝对路径
●绝对路径:以/开头,根目录为起点
●相对路径:以当前目录为起点
相对路径表现形式:
九.列表显示目录内容 -ls
ls ----显示目录中内容,包括子目录和文件相关属性信息
ls【文件】【文件或目录…】
选项l | 作用 |
---|---|
-l | 以长格式(Long) 显示文件和目录的列表,包括权限、大小、最后更新时间等详细信息。ll命令与ls -l功能相同 |
-a | 显示所有(All)子目录和文件的信息,包括名称以点号“."开头的隐藏目录和隐藏文件 |
-A | 与-a选项的作用基本类似,但有两个特殊隐藏目录不会显示,即表示当前目录的“.”和表示父目录的“…" |
-d | 显示目录(Directory) 本身的属性,而不是显示目录中的内容 |
-h | 以更人性化(Human) 的方式显示目录或文件的大小,默认的大小单位为字节(B),使用-h选项后将显示为KB、MB等单位。此选项需要结合-l选项一起使用。 |
-R | 以递归(Recursive) 的方式显示指定目录及其子目录中的所有内容。 |
-color | 在字符模式中以颜色区分不同的文件。默认已开启。(通常情况下,深蓝色表示目录,白色表示一般文件, 绿色表示可执行的文件,黄色表示设备文件,红色表示压缩文件,淡蓝色是文件快捷方式。) |
-i | 显示文件和目录的inode号 |
●结合通配符
“?”:代表任意一个字符,且至少一个
“*”:代表任意数个字符,可以是0个或者1个或者多个
“[]”:表示可以匹配字符组中的任意一个字符
10.设置别名 -alias
alias
●通过别名机制简化常用的、比较长的命令
alias命令别名=‘命令’
例:alias myls=‘ls-alh’
●取消别名:unalias 命令别名
11.统计目录及文件空间占用情况 -du
du
-a | 统计所有文件和目录(包括本目录)占用的磁盘空间 |
---|---|
-h | 以更人性化的方式(默认以KB计数,但不显示单位)显示出统计结果 |
-s | 只统计占用空间的大小,而不是统计每个子目录、文件的大小 |
- -max-depth=n | 统计所有小于等于第n层的目录的总大小 |
注:选项-a和-s不可同时使用
12.创建新的空目录 -mkdir
●创建新的空目录
mkdir【选项】目录位置及名称
例:
在当前目录下创建名为public_html的子目录
mkdir public_html
创建目录/aa,在/aa目录下创建子目录bb,在/aa/bb目录下创建子目录cc
mkdir -p aa/bb/cc
13.创建空文件 -touch
●更新文件的时间标记
●经常用于创建多个新的空文件
touch 文件…
例:
touch {aa,bb}.txt = touch aa.txt touch bb.txt
14.创建链接文件 -ln
●为文件或目录建立链接文件,类似于Windows系统的快捷方式
链接文件类型:
链接文件分为软链接(符号链接)和硬链接
15.复制文件或目录 -cp
●将需要复制的文件或目录(源)重建一份,并保存为新的文件或目录
●cp 【选项】…源文件或目录…目标文件或目录…
-f | 覆盖目标同名文件或目录时不进行提醒,直接强行复制 |
---|---|
-i | 覆盖目标同名文件或目录时提醒用户确定 |
-p | 复制时保持源文件的权限、属主及时间标记等属性不变 |
-r | 复制目录时必须使用,表示递归复制所有文件及子目录 |
注:复制多个文件或目录时,目标位置必须为目录,且目标目录必须已存在