硬件 内核 与 shell
我们必须通过shell将我们输入的命令与内核通信,好让内核控制硬件正确无误的工作。
shell 与 shell scripts
bash的主要优点: bash (linux系统默认的shell)
1.命令记忆能力(hitory): ~/.bash_history 记录前一次登录 执行过得命令,而此次登录的命令存储在临时内存中,当你成功注销系统后,此次的命令才会记录到 bash_history中.
[root@localhost home]# history 5 #格式 history n(n为数字) 查看最近的5条命令 ***********
675 cd /home
676 history 10
677 history 100
678 history 100 |more
679 history 5
2.命令与文件补全功能 (tab键的好处)
tab 接在一串命令的第一个字符后面,为命令补全。
tab 接在一串命令的第二个字后面为,则为文件补全。
3.命令别名设置功能
你在命令行输入 alias 就可以知道目前命令别名有哪些了,也可以直接执行命令来设置别名 alias lm =“ls -a”此时lm就拥有了与ls -a 同样的功能。取消命令的别名 就可以用 unalias 例如 unalias lm 。
[root@localhost ~]# alias
alias cp='cp -i'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
4. 作业控制 前台 后台控制(job control foreground background)
5. 程序脚本(shell scripts)
6. 通配符
例如 :“*”,“?”,“[]”,“^”(尖),“{}”。
“*”:代表 任意多个字符等 查询“.txt”结尾的文件。
[root@localhost ~]# ll *.txt
-rw-rw-r--. 1 root root 0 5月 7 02:37 ccc.txt
-rw-r--r--. 1 root root 25 5月 4 21:47 hello1.txt
-rw-r--r--. 1 root root 17 5月 4 19:54 hello.txt
-rw-r--r--. 1 root root 6 5月 7 02:23 hi.txt
-rw-r--r--. 1 root root 204 5月 10 23:48 test.txt
-rw-r--r--. 1 root root 0 5月 7 04:16 welcome.txt
“?”:代表任意一个单个字符 只查询 a ,b, c
[root@localhost ~]# ll ? #查询只有一个字符的文件或者目录
-rw-r--r--. 1 root root 0 5月 11 19:23 a
“[]” :代表“[”和“]”之间的某个字符,比如[0-9] 可以代表0-9 之间任意的一个数字,[a-zA-Z]可以代表 a-z和A-Z之间的任意一个字母,字母区分大小写。
[root@localhost ~]# ll [a-zA-Z]
-rw-r--r--. 1 root root 0 5月 11 19:23 a
例题 : 查询以‘.txt’结尾的文件 并且文件名中只含有两个字符 并且第二个为数字。
[root@localhost ~]# ll ?[0-9].txt
ls: 无法访问?[0-9].txt: 没有那个文件或目录
#由于没有文件 所以只列出格式
“^”(尖): 表示匹配取反的意思,注意这个通配符必须要在[]中使用。
查询不是一‘.txt’结尾的文件
[root@localhost ~]# ll *[^.txt]* |head #由于文件过多 使用head 进行筛选
-rw-r--r--. 1 root root 0 5月 11 19:23 a
-rw-r--r--. 1 root root 178 5月 10 05:23 abc.tar.gz
-rw-------. 1 root root 1862 5月 4 05:42 anaconda-ks.cfg
-rw-rw-r--. 1 root root 0 5月 7 02:37 ccc.txt
lrwxrwxrwx. 1 root root 12 5月 9 02:30 contab2 -> /etc/contab2
-rw-r--r--. 2 root root 451 6月 10 2014 crontab
lrwxrwxrwx. 1 root root 13 5月 9 02:31 crontab2 -> /etc/crontab2
-rw-r--r--. 1 root root 25 5月 4 21:47 hello1.txt
-rw-r--r--. 1 root root 17 5月 4 19:54 hello.txt
-rw-r--r--. 1 root root 6 5月 7 02:23 hi.txt
‘{}’ 表示符合括号内包含的多个文件
例如查询 “.log”和“.txt”的文件
[root@localhost ~]# ll {*.log,*txt}
ls: 无法访问*.log: 没有那个文件或目录 #由于文件中没有。log结尾的文件所以只显示了.txt结尾的文件
-rw-rw-r--. 1 root root 0 5月 7 02:37 ccc.txt
-rw-r--r--. 1 root root 25 5月 4 21:47 hello1.txt
-rw-r--r--. 1 root root 17 5月 4 19:54 hello.txt
-rw-r--r--. 1 root root 6 5月 7 02:23 hi.txt
-rw-r--r--. 1 root root 204 5月 10 23:48 test.txt
-rw-r--r--. 1 root root 0 5月 7 04:16 welcome.txt
注意‘.’这个符号比较特殊,如果匹配的条件加上这个符号,那么说明查询的结果就包含了带‘.’的文件。
删除操作
例如要删除a b c和以.txt文件结尾的文件
rm -f {[abc],*.txt}
[root@localhost ~]# rm {[abc],*txt} #询问删除
rm:是否删除普通空文件 "a"?y
rm:是否删除普通空文件 "ccc.txt"?
bash shell 的内置命令:type
格式 $type [-tpa] name # 命令 参数 指令名
不加任何参数 会显示出 name 是内部还是外部命令
[root@localhost ~]# type ll
ll 是 `ls -l --color=auto' 的别名
加上 -t 参数 显示它的意义
[root@localhost ~]# type -t ll
alias
file:表示为外部命令
alias: 表示该命令为命令别名所设置的名称
bulitin: 表示该命令为bash 内置的命令功能
-p : 如果后面的命令为外部指令时 ,才会显示完整的文件名。
-a :会由PATH变量定义的路径中,将所有含有name的命令都列出来包括alias
什么是变量: 简单的说就是让某个特定的字符串代表不固定的内容就是了
查看环境变量。
[root@localhost ~]# echo $PATH #查看PATH。
/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:/home/zhaoxin/.local/bin:/home/zhaoxin/bin