Linux基础入门之Linux终端和shell

linux终端和shell

linux终端

虚拟终端由mingetty程序产生

切换终端 ctrl+alt+fn  n=1-6

虚拟终端(输入tty显示当前是那个终端)

[root@learn ~]# tty

/dev/tty1

模拟终端 (使用ssh通过网络连接到linux

[root@learn ~]# tty

/dev/pts/0       

linuxshell

查看系统使用的shell

[root@learn ~]# echo $SHELL

/bin/bash

查看系统可用的shell

[root@learn ~]# cat /etc/shells

/bin/sh

/bin/bash

/sbin/nologin

/bin/dash

/bin/tcsh

/bin/csh

bash功能

命令行编辑

1) 光标快速移动

ctrl+a 跳到行首

ctrl+e 跳到行尾

2) 删除命令中的内容

ctrl+w 删除光标前一个单词

ctrl+u删除光标行首的字符

ctrl+k删除光标行尾的字符

3) 清屏

ctrl+l  windows命令提示符 cls

4) 取消不执行的命令

ctrl+c

内部命令和外部命令

内部命令shell自带的命令

外部命令 在系统的某个路径下的可执行程序,依赖于PATH环境变量进行查找命令

查看是内部命令还是外部命令

[root@learn ~]# type ls

ls is aliased to `ls --color=auto'

[root@learn ~]# type alias

alias is a shell builtin

也可用以下命令查看哪些属于内部命令

[root@learn ~]# man builtin

查看外部命令搜索路径

[root@learn ~]# echo $PATH

/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin

命令补全 路径补全

使用tab键进行命令和路径补全操作

命令历史

echo $HISTSIZE   缓存多少条命令

[root@learn ~]# echo $HISTSIZE

1000

echo $HISTFILE   查看历史文件

[root@learn ~]# echo $HISTFILE

/root/.bash_history

echo $HISTFILESIZE   命令历史文件能保存的条数

[root@learn ~]# echo $HISTFILESIZE

1000

cat  /root/.bash_history  查看保存命令历史

[root@learn ~]# cat /root/.bash_history |head -1    #通过管道过滤显示一条命令

ifconfig

1) 显示历史记录规则

[root@learn ~]# echo $HISTCONTROL

ignoredups

含义:

igonredups 不记录后面重复的命令,只记录一个

ignorespace 不记录以空格开头的命令

ignoreboth 不记录后面重复的命令和不记录以空格开头的命令

永久生效

echo 'export HISTCONTROL=ignoreboth' >> /etc/profile

通过export 修改历史记录规则

[root@learn ~]# export HISTCONTROL=ignoreboth

[root@learn ~]#  history |tail -2  

  480  history |tail -1

  481  history |tail

前面有空格则不记录历史命令

 

2) 执行以前的命令(快捷键)

!!执行上一条命令

history查看历史命令

!6 执行命令历史中第6行命令

!-1执行命令历史中倒数第一条命令

上下键可以翻命令

ctrl+p向上翻

ctrl+n向下翻

 

3) 当前命令调用上一条命令的最后一个参数

方法一:

ESC松开按.

方法二:

新命令后加!$

[root@learn ~]# head !$

head /etc/passwd

history命令参数

-c 清空历史命令

-w覆盖命令到历史文件

-a 追加命令到历史文件

-d 4 删除命令历史的第4条命令

479  export HISTCONTROL=ignoreboth

  480  history |tail -1

  481  history |tail

  482  history |tail -3

[root@learn ~]# history -d 480

[root@learn ~]# history |tail -5

  479  export HISTCONTROL=ignoreboth

  480  history |tail

  481  history |tail -3

  482  history -d 480

  483  history |tail -5

文件名通配符

通配符是特殊的字符,不表示字符的表面意义而是能够匹配符号指定特征的字符

* 代表任意长度的字符

[root@learn lisi]# touch a acb addb cdb ateb

[root@learn lisi]# ls a*

a  acb  addb  ateb

[root@learn lisi]# ls a*b

acb  addb  ateb

 

? 代表任意单个字符

[root@learn lisi]# touch a acb addb cdb ateb

[root@learn lisi]# ls a?

ls: cannot access a?: No such file or directory

[root@learn lisi]# ls a?b

acb

[] 代表指定范围的单个字符

[root@learn lisi]# ls a[0-9]*

a23b  a3b  a4b  a9b

[root@learn lisi]# ls a[0-9]b

a3b  a4b  a9b

^ 排除

[root@learn lisi]# touch a23b a3b a9b a4b

[root@learn lisi]# ls a[^0-5]b

a9b  acb

查找文件中间有空格的文件

[root@learn lisi]# touch 'a b'

[root@learn lisi]# ls *[' ']b

a b

[root@learn lisi]# ls *[' ']*

a b

使用专门的方法识别特定字符

[root@learn lisi]# ls a[[:space:]]b      #空格

a b

[root@learn lisi]# ls a[[:digit:]]b       #[0-9]

a3b  a4b  a9b

特定字符的含义:

[[:space:]] 空格

[[:digit:]] [0-9]

[[:lower:]] [a-z]

[[::upper:]] [A-Z]

[[:alpha:]] [a-Z]

命令别名

格式: alias cmdalias='command [option] [argument]'

查看别名类型

[root@learn ~]# type alias

查看定义的别名

[root@learn ~]# alias

定义别名

[root@learn ~]# alias if='ifconfig eth0'

[root@learn ~]# alias ifconfig='ifconfig eth0'

调用命令本身(去掉别名含义)

[root@learn ~]# alias ifconfig='ifconfig eth0'

[root@learn ~]# ifconfig

eth0      Link encap:Ethernet  HWaddr 00:0C:29:41:FA:11  

          ......

[root@learn ~]# \ifconfig    #去掉别名,使用命令本身

eth0      Link encap:Ethernet  HWaddr 00:0C:29:41:FA:11  

  ......

eth1      Link encap:Ethernet  HWaddr 00:0C:29:41:FA:1B  

......

lo        Link encap:Local Loopback  

......

将别名添加到用户配置文件

[root@learn ~]# vim .bashrc

编辑/etc/bashrc添加别名,对所有用户生效(全局)

[root@learn ~]# vim /etc/bashrc

删除别名

[root@learn ~]# unalias if1

[root@learn ~]# if1

-bash: if1: command not found

命令替换

使用命令执行的结果替换该命令

格式: 命令 $(命令)

[root@learn home]# echo "The current directory is $(pwd)"

The current directory is /home

[root@learn home]# touch file-$(date +%Y-%m-%d-%H:%M:%S)

[root@learn home]# ls

file-2017-01-05-11:47:38

格式:使用反引号实现命令替换

[root@learn home]# touch file-`(date +%Y-%m-%d-%H:%M:%S)`

[root@learn home]# ls

file-2017-01-05-11:47:38

bash" "是弱引用能够实现命令替换

[root@learn home]# echo "The current directory is $(pwd)"

The current directory is /home

bash' ' 是强引用不能实现命令替换

[root@learn home]# echo 'The current directory is $(pwd)'

The current directory is $(pwd)

命令行展开{}多项式

工作中会遇到如新建很多文件,使用一条命令来解决此问题,这时就需要使用命令行展开来实现,例:

[root@learn lisi]# touch a.txt b.txt.c.txt.d.txt

[root@learn lisi]# ls

a.txt  b.txt.c.txt.d.txt

这样写很繁琐,他们有共通点,这时我们用命令行展开来写,需要用到{}来实现。

[root@learn lisi]# touch {a..d}.txt

[root@learn lisi]# ls

a.txt  b.txt  c.txt  d.txt

怎么样?这样写是不是很方便。还可以创建更复杂的。

比如我们想创建

/home/lisi/a.txt

/home/lisi/b.txt

/home/lisi/a/a.txt

/home/lisi/a/b.txt

/home/lisi/c

/home/lisi/d

但是不能创建目录,需要先创建目录,如果是创建目录则不会存在次问题。

[root@learn lisi]# mkdir a

[root@learn lisi]# touch {a,b}.txt a/{a,b}.txt {c,d}

[root@learn lisi]# touch {{a,b}.txt,a/{a,b}.txt,{c,d}}  #同上一条命令

[root@learn lisi]# ls

a  a.txt  b.txt  c  d

[root@learn lisi]# tree

.

├── a

   ├── a.txt

   └── b.txt

├── a.txt

├── b.txt

├── c

└── d

重定向

重定向的用途

可以将命令输出保存到文件,可以向配置文件增加内容,可以合并文件内容。

查看标准输入输出设备

[lisi@learn ~]$ ls -l /dev/std*

lrwxrwxrwx 1 root root 15 Jan  8 18:29 /dev/stderr -> /proc/self/fd/2

lrwxrwxrwx 1 root root 15 Jan  8 18:29 /dev/stdin -> /proc/self/fd/0

lrwxrwxrwx 1 root root 15 Jan  8 18:29 /dev/stdout -> /proc/self/fd/1

 

 

查看文件内容

[lisi@learn ~]$ tail -2f /etc/passwd

li:x:500:500::/home/li:/bin/bash

lisi:x:501:501::/home/lisi:/bin/bash

ctrl+z将进程转到后台

ps 查看运行的进程

[lisi@learn ~]$ ps

  PID TTY          TIME CMD

 5954 pts/0    00:00:00 bash

 6004 pts/0    00:00:00 tail

 6009 pts/0    00:00:00 ps

[lisi@learn ~]$ ls -l /proc/6004/fd  #查看6004进程下的文件描述符

total 0

lrwx------ 1 lisi lisi 64 Jan 10 22:01 0 -> /dev/pts/0   #标准输入

lrwx------ 1 lisi lisi 64 Jan 10 22:01 1 -> /dev/pts/0   #标准输出

lrwx------ 1 lisi lisi 64 Jan 10 22:00 2 -> /dev/pts/0   #标准错误输出

lr-x------ 1 lisi lisi 64 Jan 10 22:01 3 -> /etc/passwd   #进程指向的文件

lr-x------ 1 lisi lisi 64 Jan 10 22:01 4 -> inotify

重定向 标准输出

[root@learn ~]$ ifconfig eth0 1>abc

重定向 标准错误输出

[root@learn ~]$ ifconfig eth1 2>123

将标准输出和标准错误输出重定向到不同文件

[root@learn ~]$ find /etc -name "*.conf" 1>a 2>b

将标准输出和标准错误输出重定向到同一个文件

[root@learn ~]$ find /etc -name "*.conf" 1>a 2>b

重定向 追加

[root@learn ~]$ find /etc -name "*.conf" 1>>a 2>>b

 

管道

1.在管道后面的命令都不应该再跟文件名。如果跟的话则是后面命令的结果。

[lisi@learn ~]$ tail /etc/passwd |head -2 /etc/passwd  #管道后面这条命令加了文件名,列出后面命令的值

root:x:0:0:root:/root:/bin/bash

bin:x:1:1:bin:/bin:/sbin/nologin 

[lisi@learn ~]$ tail /etc/passwd |head -2  #不加则是需要的值

vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin

abrt:x:173:173::/etc/abrt:/sbin/nologin

2.在管道中只有标准输出才传递给下一个命令;标准错误输出直接输出到终端,可以把标准错误输出给重定向。

[lisi@learn ~]$ find /etc/ -name "*.conf" 2>/dev/null |grep rc   #把标准错误输出到空设备去,过滤包含rc内容的文件

/etc/latrace.d/resource.conf

/etc/libreport/events.d/vimrc_event.conf

/etc/init/rcS.conf

/etc/init/rc.conf

/etc/init/rcS-emergency.conf

/etc/init/rcS-sulogin.conf

3.有些命令不支持管道,让xargs支持这些命令。

[root@learn home]# which cat |ls -l   #列出的是当前目录下的内容

total 8

drwx------ 2  500  500 4096 Jan 10 21:39 lisi

drwxr-xr-x 9 root root 4096 Feb 26  2016 vmware-tools-distrib

[root@learn home]# which cat |xargs ls -l  #加上xargs后得到需要的值

-rwxr-xr-x. 1 root root 48568 Oct 15  2014 /bin/cat

posted @ 2017-07-01 17:37  pathping  阅读(431)  评论(0编辑  收藏  举报