首页 > 代码库 > linux常用命令-命令行编辑,history,命令行快捷键,pstree,alias,命令替换,通配符
linux常用命令-命令行编辑,history,命令行快捷键,pstree,alias,命令替换,通配符
命令行编辑:
光标跳转:
Ctrl+a:跳到命令行首
Ctrl+e:跳到命令行尾
Ctrl+u: 删除光标至命令行首的内容
Ctrl+k: 删除光标至命令行尾的内容
Ctrl+l: 清屏
Ctrl+d: 删除光标后面内容
命令历史:
查看命令历史:history
-c:清空命令历史
-d OFFSET [n]: 删除指定位置的命令
-w:保存命令历史至历史文件中
环境变量
PATH:命令搜索路径
HISTSIZE: 命令历史缓冲区大小
命令历史的使用技巧:
!n:执行命令历史中的第n条命令;
[rhel@localhost ~]$ !1
whatis man
man (1) - format and display the on-line manual pages
man (1p) - display system documentation
man (7) - macros to format man pages
man.config [man] (5) - configuration data for man
man [manpath] (1) - format and display the on-line manual pages
man-pages (7) - conventions for writing Linux man pages
[rhel@localhost ~]$ history
1 whatis man
!-n:执行命令历史中的倒数第n条命令;
85 whatis man
86 history
[rhel@localhost ~]$ !-2
whatis man
man (1) - format and display the on-line manual pages
man (1p) - display system documentation
man (7) - macros to format man pages
man.config [man] (5) - configuration data for man
man [manpath] (1) - format and display the on-line manual pages
man-pages (7) - conventions for writing Linux man pages
!!: 执行上一条命令;
[rhel@localhost ~]$ !!
whatis man
man (1) - format and display the on-line manual pages
man (1p) - display system documentation
man (7) - macros to format man pages
man.config [man] (5) - configuration data for man
man [manpath] (1) - format and display the on-line manual pages
man-pages (7) - conventions for writing Linux man pages
!string:执行命令历史中最近一个以指定字符串开头的命令
[rhel@localhost ~]$ !man
man history
[rhel@localhost ~]$
[rhel@localhost ~]$
!$:引用前一个命令的最后一个参数;
[rhel@localhost ~]$ ls /tmp/
abc inittabl.2 keyring-C6bpQR orbit-gdm pulse-Y030iPht7UnR test
hi keyring-11UHBi keyring-HXk4gf orbit-rhel rc virtual-rhel.T2NxT0
inittab keyring-4aFGvD keyring-tTiDCT passwd rc.2
inittab.1 keyring-6hak4Z keyring-V1nCMd pulse-kYyFfO5RNhPL rc.3
[rhel@localhost ~]$ ll !$
ll /tmp/
total 84
drwxr-xr-x. 2 root root 4096 Nov 19 20:26 abc
drwxr-xr-x. 2 root root 4096 Nov 19 20:28 hi
-rw-r--r--. 1 root root 884 Nov 20 02:59 inittab
drwxr-xr-x. 2 root root 4096 Nov 19 21:01 inittab.1
drwxr-xr-x. 2 root root 4096 Nov 19 21:01 inittabl.2
drwx------. 2 rhel rhel 4096 Nov 19 17:54 keyring-11UHBi
drwx------. 2 rhel rhel 4096 Jun 23 2015 keyring-4aFGvD
Esc, . 与 !$相同
Alt+. (远程终端不支持)
命令补全:
PATH:
路径补全:
pstree:显示进程树
NAME
pstree - display a tree of processes
[rhel@localhost ~]$ pstree
init─┬─NetworkManager─┬─dhclient
│ └─{NetworkManager}
├─abrtd
├─acpid
├─atd
├─auditd───{auditd}
├─bonobo-activati───{bonobo-activat}
├─clock-applet
├─console-kit-dae───63*[{console-kit-da}]
├─crond
├─cupsd
├─2*[dbus-daemon───{dbus-daemon}]
├─2*[dbus-launch]
├─devkit-power-da
├─gconfd-2
history:命令历史使用
[rhel@localhost ~]$ history
1 whatis man
2 whatis /etc/passwd
3 man whatis
4 whatis cd
5 whatis ls
6 whatis type
7 whatis du
8 whatis echo
9 whatis mv
10 whatis man
11 man cut
12 cut -d : /etc/passwd
13 cut -d : -f1 /etc/passwd
-c:清空历史
-n:删除指定位置命令
-w:保存命令至历史文件
命令别名
alias CMDALIAS=‘COMMAND [options] [arguments]‘
在shell中定义的别名仅在当前shell生命周期中有效;别名的有效范围仅为当前shell进程;
[rhel@localhost ~]$ alias
alias l.=‘ls -d .* --color=auto‘
alias ll=‘ls -l --color=auto‘
alias ls=‘ls --color=auto‘
alias vi=‘vim‘
alias which=‘alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde‘
[rhel@localhost ~]$
[rhel@localhost ~]$ type alias
alias is a shell builtin
[rhel@localhost ~]$ help alias
alias: alias [-p] [name[=value] ... ]
Define or display aliases.
Without arguments, `alias‘ prints the list of aliases in the reusable
form `alias NAME=VALUE‘ on standard output.
Otherwise, an alias is defined for each NAME whose VALUE is given.
A trailing space in VALUE causes the next word to be checked for
alias substitution when the alias is expanded.
Options:
-p Print all defined aliases in a reusable format
Exit Status:
alias returns true unless a NAME is supplied for which no alias has been
defined.
[rhel@localhost ~]$
取消命令别名
ualias CMDALIAS
使用原有命令
\CMD
[rhel@localhost ~]$ \ls /tmp/
abc inittabl.2 keyring-C6bpQR orbit-gdm pulse-Y030iPht7UnR test
hi keyring-11UHBi keyring-HXk4gf orbit-rhel rc virtual-rhel.T2NxT0
inittab keyring-4aFGvD keyring-tTiDCT passwd rc.2
inittab.1 keyring-6hak4Z keyring-V1nCMd pulse-kYyFfO5RNhPL rc.3
[rhel@localhost ~]$
命令替换: $(COMMAND), 反引号:`COMMAND`
把命令中某个子命令替换为其执行结果的过程
file-2013-02-28-14-53-31.txt
bash支持的引号:
``: 命令替换
[rhel@localhost ~]$ echo "dir is `pwd`"
dir is /home/rhel
[rhel@localhost ~]$ cd /tmp/
[rhel@localhost tmp]$ echo "dir is `pwd`"
dir is /tmp
[rhel@localhost tmp]$
"": 弱引用,可以实现变量替换
[rhel@localhost tmp]$ echo "the path is $PATH"
the path is /usr/lib64/qt-3.3/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/rhel/bin
[rhel@localhost tmp]$
‘‘: 强引用,不完成变量替换
[rhel@localhost tmp]$ echo ‘the path is $PATH‘
the path is $PATH
[rhel@localhost tmp]$
文件名通配, globbing
*: 任意长度的任意字符
?:任意单个字符
[]:匹配指定范围内的任意单个字符
[abc], [a-m], [a-z], [A-Z], [0-9], [a-zA-Z], [0-9a-zA-Z]
[:space:]:空白字符
[:punct:]:标点符号
[:lower:]:小写字母
[:upper:]: 大写字母
[:alpha:]: 大小写字母
[:digit:]: 数字
[:alnum:]: 数字和大小写字母
# man 7 glob
[^]: 匹配指定范围之外的任意单个字符
[[:alpha:]]*[[:space:]]*[^[:alpha:]]
练习:
1、创建a123, cd6, c78m, c1 my, m.z, k 67, 8yu, 789等文件;注意,以上文件是以逗号隔开的,其它符号都是文件名的组成部分;
2、显示所有以a或m开头的文件;
ls [am]*
3、显示所有文件名中包含了数字的文件;
ls *[0-9]*
ls *[[:digit:]]*
4、显示所有以数字结尾且文件名中不包含空白的文件;
ls *[^[:space:]]*[0-9] ?????????
5、显示文件名中包含了非字母或数字的特殊符号的文件;
ls *[^[:alnum:]]*
linux常用命令-命令行编辑,history,命令行快捷键,pstree,alias,命令替换,通配符