linux常用命令-命令行编辑,history,命令行快捷键,pstree,alias,命令替换,通配符
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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条命令;
[[email protected] ~]$ !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
[[email protected] ~]$ history
1 whatis man
!-n:执行命令历史中的倒数第n条命令;
85 whatis man
86 history
[[email protected] ~]$ !-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
!!: 执行上一条命令;
[[email protected] ~]$ !!
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:执行命令历史中最近一个以指定字符串开头的命令
[[email protected] ~]$ !man
man history
[[email protected] ~]$
[[email protected] ~]$
!$:引用前一个命令的最后一个参数;
[[email protected] ~]$ 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
[[email protected] ~]$ 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
[[email protected] ~]$ 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:命令历史使用
[[email protected] ~]$ 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进程;
[[email protected] ~]$ 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‘
[[email protected] ~]$
[[email protected] ~]$ type alias
alias is a shell builtin
[[email protected] ~]$ 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.
[[email protected] ~]$
取消命令别名
ualias CMDALIAS
使用原有命令
\CMD
[[email protected] ~]$ \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
[[email protected] ~]$
命令替换: $(COMMAND), 反引号:`COMMAND`
把命令中某个子命令替换为其执行结果的过程
file-2013-02-28-14-53-31.txt
bash支持的引号:
``: 命令替换
[[email protected] ~]$ echo "dir is `pwd`"
dir is /home/rhel
[[email protected] ~]$ cd /tmp/
[[email protected] tmp]$ echo "dir is `pwd`"
dir is /tmp
[[email protected] tmp]$
"": 弱引用,可以实现变量替换
[[email protected] 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
[[email protected] tmp]$
‘‘: 强引用,不完成变量替换
[[email protected] tmp]$ echo ‘the path is $PATH‘
the path is $PATH
[[email protected] 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,命令替换,通配符的主要内容,如果未能解决你的问题,请参考以下文章
Linux 命令(227)—— history 命令(builtin)