|
1. history 查看命令历史
!! 命令历史中上一条命令
[root@hpf-linux ~]# ls
1 222 anaconda-ks.cfg install.log.syslog
1.txt 23232 downloads.php mysql-5.1.40-linux-i686-icc-glibc23.tar.gz
1.zip 3.txt install.log
[root@hpf-linux ~]# !!
ls
1 222 anaconda-ks.cfg install.log.syslog
1.txt 23232 downloads.php mysql-5.1.40-linux-i686-icc-glibc23.tar.gz
!$ 上一条命令的最后一个参数
[root@hpf-linux ~]# ls 3.txt
3.txt
[root@hpf-linux ~]# ls !$
ls 3.txt
!n 命令历史的第n条命令
1101 history
1102 pwd
1103 ls
1104 rm -rf php*
[root@hpf-linux ~]# !1102
pwd
!c 命令历史中最近一条已c开头的命令(c为多个或一个字符)
1108 ls 3.txt
1109 ls 3.txt
1110 history
1111 pwd
1112 history
[root@hpf-linux ~]# !p
pwd
2.alias别名 一般用于简化命令,取消用unalias
[root@hpf-linux ~]# alias aaa='ls -lh /etc/passwd'
[root@hpf-linux ~]# aaa
-rw-r--r-- 1 root root 1.1K 4月 10 00:39 /etc/passwd
[root@hpf-linux ~]# unalias aaa
[root@hpf-linux ~]# aaa
-bash: aaa: command not found
3.通配符:*匹配零个和多个字符 ?匹配一个字符 [] 匹配中括号内任意字符
[root@hpf-linux ~]# ls *.txt
1.txt 2.txt 3.txt
[root@hpf-linux ~]# ls [12].txt
1.txt 2.txt
[root@hpf-linux ~]# ls ?.txt
1.txt 2.txt 3.txt
[root@hpf-linux ~]# touch txt
[root@hpf-linux ~]# ls ?txt
ls: 无法访问?txt: 没有那个文件或目录
[root@hpf-linux ~]# ls *txt
1.txt 2.txt 3.txt txt
4.输入输出重定向:>输出重定向 >>追加输出 错误输出重定向 2>> 错误输出追加 2>&1 错误正确输出重定向
[root@hpf-linux ~]# echo "123" >4.txt
[root@hpf-linux ~]# cat 4.txt
123
[root@hpf-linux ~]# echo "4345" >4.txt
[root@hpf-linux ~]# cat 4.txt
4345
[root@hpf-linux ~]# echo "4345" >> 4.txt
[root@hpf-linux ~]# cat 4.txt
4345
4345
[root@hpf-linux ~]# mail -s "zhuti" afsafewfsa@qq.com < 3.txt
[root@hpf-linux ~]# cd /etc/passwd
-bash: cd: /etc/passwd: 不是目录
[root@hpf-linux ~]# cd /etc/passwd 2>cuowu.log
[root@hpf-linux ~]# cat cuowu.log
-bash: cd: /etc/passwd: 不是目录
[root@hpf-linux ~]# cd /etc/passwd 2>cuowu.log
[root@hpf-linux ~]# cat cuowu.log
-bash: cd: /etc/passwd: 不是目录
[root@hpf-linux ~]# cd /etc/passwd 2>>cuowu.log
[root@hpf-linux ~]# cat cuowu.log
-bash: cd: /etc/passwd: 不是目录
-bash: cd: /etc/passwd: 不是目录
实验问题:错误输出追加不能用2>>&1 只能用 2>>cuowu.log
[root@hpf-linux ~]# cd /etc/passwd 1>shuchu.log 2>&1
[root@hpf-linux ~]# cat shuchu.log
-bash: cd: /etc/passwd: 不是目录
[root@hpf-linux ~]# cd /etc/passwd 1>shuchu.log 2>&1
[root@hpf-linux ~]# cat shuchu.log
-bash: cd: /etc/passwd: 不是目录
[root@hpf-linux ~]# cd /etc/passwd 1>shuchu.log 2>>&1
-bash: syntax error near unexpected token `&'
[root@hpf-linux ~]# cd /etc/passwd 1>>shuchu.log 2>>shuchu.log
[root@hpf-linux ~]# cat shuchu.log
-bash: cd: /etc/passwd: 不是目录
-bash: cd: /etc/passwd: 不是目录
[root@hpf-linux ~]# cd /etc/passwd 1>>shuchu.log 2>>&1
-bash: syntax error near unexpected token `&'
5.管道:|把前面一个命令的输出做为后面一个命令的参数
[root@hpf-linux ~]# ls |xargs
1 1.txt 1.zip 222 23232 2.txt 3.txt 4.txt anaconda-ks.cfg cuowu.log downloads.php install.log install.log.syslog mysql-5.1.40-linux-i686-icc-glibc23.tar.gz shuchu.log txt
6.作业控制:
当运行一个进程时,你可以使它暂停(按Ctrl+z),然后使用fg命令恢复它,利用bg命令使他到后台运行,你也可以使它终止(按Ctrl+c)
jobs 可以查看被暂停或在后台运行的任务
[root@hpf-linux ~]# sleep 300
^Z
[1]+ Stopped sleep 300
[root@hpf-linux ~]# sleep 200
^Z
[2]+ Stopped sleep 200
[root@hpf-linux ~]# jobs
[1]- Stopped sleep 300
[2]+ Stopped sleep 200
[root@hpf-linux ~]# fg
sleep 200
^Z
[2]+ Stopped sleep 200
[root@hpf-linux ~]# bg 1
[1]- sleep 300 &
[root@hpf-linux ~]# jobs
[1]- Running sleep 300 &
[2]+ Stopped sleep 200
fg 不加参数命令默认调用+(优先级高) bg 命令可以把后台命令开启
小知识:
另一种情况,关闭当前shell,重新打开另一个shell,使用jobs并不会显示在后台运行的进程。想要停止进程的话,需要知道pid,然后使用kill命令杀死进程。如遇到杀不死的进程,使用 kill -9 pid
[root@hpf-linux ~]# ps aux | grep sleep
root 1650 0.0 0.0 5680 508 pts/0 S 04:32 0:00 sleep 300
root 1651 0.0 0.0 5680 504 pts/0 T 04:32 0:00 sleep 200
root 1657 0.0 0.0 5980 740 pts/1 S+ 04:36 0:00 grep sleep
[root@hpf-linux ~]# kill 1651
7.变量
系统变量名都是大写,常用的有常用变量有:PATH、HOME、LANG、PWD、LOGNAME、HOSTNAME
echo 可以查看变量名
[root@hpf-linux ~]# echo $PATH
/usr/lib/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/tmp:/root/bin:/root/bin:/usr/local/apache2/bin
[root@hpf-linux ~]# echo $HOME
/root
[root@hpf-linux ~]# echo $LANG
zh_CN.UTF-8
[root@hpf-linux ~]# echo $LOGNAME
root
[root@hpf-linux ~]# echo $HOSTNAME
hpf-linux
env 可以列出当前用户的所有环境变量以及用户自定义全局变量
set命令可以把所有变量列出来包括系统的和自定义的全局变量以及当前shell自定义变量
自定义变量只在当前shell环境生效,一旦进入子shell则不能使用
[root@hpf-linux ~]# abc="aminglinux"
[root@hpf-linux ~]# echo abc
abc
[root@hpf-linux ~]# echo $abc
aminglinux
[root@hpf-linux ~]# bash
[root@hpf-linux ~]# echo $abc
[root@hpf-linux ~]# exit
exit
[root@hpf-linux ~]# echo $abc
aminglinux
变量永久生效:
在所有用户下均生效,编辑文件 vim /etc/profile 在末尾加上:export abc=aminglinux
[root@hpf-linux ~]# vim /etc/profile
[root@hpf-linux ~]# source /etc/profile
[root@hpf-linux ~]# echo $abc
aminglinux
[root@hpf-linux ~]# su - test3
[test3@hpf-linux ~]$ echo $abc
aminglinux
只在当前用户下生效
[test3@hpf-linux ~]$ echo "export bbb=dota" >> .bashrc
[test3@hpf-linux ~]$ source .bashrc
[test3@hpf-linux ~]$ echo $bbb
dota
linux下设置自定义变量规则:
(1)格式为 “a=b”, 其中a为变量名,b为变量的内容,等号两边不能有空格;
(2)变量名只能由英、数字以及下划线组成,而且不能以数字开头;
(3)当变量内容带有特殊字符(如空格)时,需要加上单引号;
(4)如果变量内容中需要用到其他命令运行结果则可以使用反引号;
(5)变量内容可以累加其他变量的内容,需要加双引号;
[root@hpf-linux ~]# bbb=dota
[root@hpf-linux ~]# echo #bbb
[root@hpf-linux ~]# echo $bbb
dota
[root@hpf-linux ~]# bbb="love dota"
[root@hpf-linux ~]# echo $bbb
love dota
[root@hpf-linux ~]# bbb="love dota'"
[root@hpf-linux ~]# echo $bbb
love dota'
[root@hpf-linux ~]# bbb=`pwd`
[root@hpf-linux ~]# echo $bbb
/root
[root@hpf-linux ~]# bbb=love
[root@hpf-linux ~]# echo $bbb
love
[root@hpf-linux ~]# bc="$bbb"dota
[root@hpf-linux ~]# echo $bc
lovedota
linux单引号与双引号区别:用双引号时不会取消掉里面出现的特殊字符的本身作用(这里的$),而使用单引号则里面的特殊字符全部失去它本身的作用。
[root@hpf-linux ~]# bb="love dota"
[root@hpf-linux ~]# c=`echo $bb`
[root@hpf-linux ~]# echo $c
love dota
[root@hpf-linux ~]# c='echo $bb'
[root@hpf-linux ~]# echo $c
echo $bb
[root@hpf-linux ~]# c="echo $bb"
[root@hpf-linux ~]# echo $c
echo love dota
在父shell设置变量只会在当前shell生效,并不会在其子shell当中生效,若需要在期子shell当中生效则需要用export指令,export 声明全局变量,如果export后面不加任何变量名,则它会声明所有的变量。
[root@hpf-linux ~]# bbb=dota
[root@hpf-linux ~]# echo $bbb
dota
[root@hpf-linux ~]# bash
[root@hpf-linux ~]# echo $bbb
[root@hpf-linux ~]# exit
exit
[root@hpf-linux ~]# export bbb
[root@hpf-linux ~]# bash
[root@hpf-linux ~]# echo $bbb
dota
取消变量 unset 命令
[root@hpf-linux ~]# ccc=dota
[root@hpf-linux ~]# echo $ccc
dota
[root@hpf-linux ~]# unset ccc
[root@hpf-linux ~]# echo $ccc
系统环境变量与个人环境变量的配置文件
/etc/profile 系统预设的几个重要的变量,例如PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL
/etc/bashrc 预设umask以及PS1等变量。PS1就是我们登录linux 命令行最前面显示的字符;
[root@hpf-linux ~]# echo $PS1
[\u@\h \W]\$
[root@hpf-linux ~]# PS1='[\h@\u \w] \$'
[hpf-linux@root ~] #cd /etc/init.d/
[hpf-linux@root /etc/init.d] #PS1='[\u@\h \W]\$'[\u@\h \W]\$
\u:用户名
\h:主机名
\W:当前路径 \w:全局路径
\$:root用户显示# 普通用户显示$
每个用户的主目录下还有几个这样的隐藏文件:
.bash_profile :定义了用户的个人化路径与环境变量的文件名称。每个用户都可使用该文件输入专用于自己使用的shell信息,当用户登录时,该文件仅仅执行一次。
.bashrc :该文件包含专用于你的shell的bash信息,当登录时以及每次打开新的shell时,该该文件被读取。例如你可以将用户自定义的alias或者自定义变量写到这个文件中。
.bash_history :记录命令历史用的。
.bash_logout :当退出shell时,会执行该文件。可以把一些清理的工作放到这个文件中。
Linux shell中的特殊字符
* 代表零个或多个任意字符
? 代表一个任意字符
# 代表注释说明的意思,#号后面的内容忽略掉;
\ 脱意字符,将后面的特殊字符(* $)还原为普通字符;
| 管道符,讲前面命令执行的结果作为后面命令的输入;
$ 引用变量
; 分隔2个命令,2个命令都执行,不管前面命令是否错误 ls a.txt ; cat a.txt
& 把命令放到后台运行
&& 命令的连接符,第一个执行成功才会执行第二个;
|| 分隔命令,只有前面命令运行不成功,才会执行后面的命令;
~ 用户家目录
cut 截取某一个字段、字符 // -c 一般不与-d和-f合用
-d 指定分隔符,分割字符用单引号 ' ' 括起来
-f 指定第哪个区间
-c 指定第几个字符;指定多个字符 -c 1,5 ;可以指定一个区间 -c 1-4 ;
[root@hpf-linux ~]# cut -d ':' -f 1 /etc/passwd |head -n 5
root
bin
daemon
adm
lp
[root@hpf-linux ~]#cut -c 1-5 /etc/passwd |head -5
root:
bin:x
daemo
adm:x
lp:x:
sort 排序输出 默认按首字母升序的顺序排列;
-t 指定分隔符
-k 指定以哪个区间进行排序
-r 逆序排列
-u 删除重复的
-n 按照数字大小排序
-f 忽略大小写
[root@hpf-linux ~]#sort -t: -k3 -n /etc/passwd |head -5
root:x:0:0:root:/root:/bin/bash
test1:x:0:0::/home/test1:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
[root@hpf-linux ~]# sort -t: -k3,5 -r /etc/passwd |head -5
nobody:x:99:99:Nobody:/:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
wc 统计行数、单词数、字节数
wc -l 统计行数
wc -w 统计单词数 //已空格分割的为界限
wc -c 统计字符数
[root@hpf-linux ~]#cat 2.txt
fwawafwa
wfawaefwa 23 242
2324234
wfcwaefwa\./\'.\\.\'.]'\.\'
wfwaefwea
[root@hpf-linux ~]#wc -l 2.txt
8 2.txt
[root@hpf-linux ~]#wc 2.txt
8 7 87 2.txt
[root@hpf-linux ~]#cat 4.txt
4
2
[root@hpf-linux ~]#wc 4.txt
2 2 4 4.txt
[root@hpf-linux ~]#cat -A 4.txt
4$
2$
uniq 去除重复的行
针对数字的话,需要先进行sort排序,再去除重复的行。
-c 统计重复的行数,并写在最前面;
tee 重定向文件,并且同时还在屏幕上显示;
类似与重定向 “>”, 但是比重定向多了一个功能,在把文件写入后面所跟的文件中的同时,还显示在屏幕上。
[root@hpf-linux ~]#cat 4.txt |tee 1.log
111111
212121
343434
232323
[root@hpf-linux ~]#cat 1.log
111111
212121
343434
232323
split 切割文档
-b 依据大小来分割文档,单位为byte
-l 依据行数来分割文档。
|
|
|