linux 输入、输出和重定向

1. 标准输入输出

传统的Linux标准输入、输出设备主要是键盘和显示器。vim

二、输出重定向

输出的重定向:bash

[root@localhost ~]# ls -il /etc/ > etc.txt

[root@localhost /]# cat /proc/cpuinfo > cpu

[root@localhost /]# df -Th > df
[root@localhost /]# cat df
Filesystem     Type      Size  Used Avail Use% Mounted on
/dev/sda2      xfs        20G  3.3G   17G  17% /
devtmpfs       devtmpfs  986M     0  986M   0% /dev
tmpfs          tmpfs     994M   84K  994M   1% /dev/shm
tmpfs          tmpfs     994M  8.9M  986M   1% /run
tmpfs          tmpfs     994M     0  994M   0% /sys/fs/cgroup
/dev/sda1      xfs       197M  101M   97M  52% /boot
/dev/sr0       iso9660   3.5G  3.5G     0 100% /run/media/root/RHEL-7.0 Server.x86_64
[root@localhost /]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda2        20G  3.3G   17G  17% /
devtmpfs        986M     0  986M   0% /dev
tmpfs           994M   84K  994M   1% /dev/shm
tmpfs           994M  8.9M  986M   1% /run
tmpfs           994M     0  994M   0% /sys/fs/cgroup
/dev/sda1       197M  101M   97M  52% /boot
/dev/sr0        3.5G  3.5G     0 100% /run/media/root/RHEL-7.0 Server.x86_64

重定向追加写入:dom

[root@localhost /]# df -h > df.txt
[root@localhost /]# cat df.txt 
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda2        20G  3.3G   17G  17% /
devtmpfs        986M     0  986M   0% /dev
tmpfs           994M   84K  994M   1% /dev/shm
tmpfs           994M  8.9M  986M   1% /run
tmpfs           994M     0  994M   0% /sys/fs/cgroup
/dev/sda1       197M  101M   97M  52% /boot
/dev/sr0        3.5G  3.5G     0 100% /run/media/root/RHEL-7.0 Server.x86_64
[root@localhost /]# echo  aidu3ndddndd >> df.txt
[root@localhost /]# cat df.txt 
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda2        20G  3.3G   17G  17% /
devtmpfs        986M     0  986M   0% /dev
tmpfs           994M   84K  994M   1% /dev/shm
tmpfs           994M  8.9M  986M   1% /run
tmpfs           994M     0  994M   0% /sys/fs/cgroup
/dev/sda1       197M  101M   97M  52% /boot
/dev/sr0        3.5G  3.5G     0 100% /run/media/root/RHEL-7.0 Server.x86_64
aidu3ndddndd
[root@localhost /]#

3. 输入重定向

这里给mycms5发一封邮件,邮件是个文件,df.txtui

[root@localhost /]# mail mycms5 < df.txt
[root@localhost /]# su - mycms5
Last login: Tue Oct 17 13:03:49 CST 2017 on pts/1
[mycms5@localhost ~]$ mail
Heirloom Mail version 12.5 7/5/10.  Type ? for help.
"/var/spool/mail/mycms5": 2 messages 1 new
    1 root                  Tue Oct 17 13:03  21/642   "bozhou in Ah"
>N  2 root                  Tue Oct 17 13:19  25/999   
& 2
Message  2:
From root@localhost.localdomain  Tue Oct 17 13:19:07 2017
Return-Path: <root@localhost.localdomain>
X-Original-To: mycms5
Delivered-To: mycms5@localhost.localdomain
Date: Tue, 17 Oct 2017 13:19:06 +0800
To: mycms5@localhost.localdomain
User-Agent: Heirloom mailx 12.5 7/5/10
Content-Type: text/plain; charset=us-ascii
From: root@localhost.localdomain (root)
Status: R

Filesystem      Size  Used Avail Use% Mounted on
/dev/sda2        20G  3.3G   17G  17% /
devtmpfs        986M     0  986M   0% /dev
tmpfs           994M   84K  994M   1% /dev/shm
tmpfs           994M  8.9M  986M   1% /run
tmpfs           994M     0  994M   0% /sys/fs/cgroup
/dev/sda1       197M  101M   97M  52% /boot
/dev/sr0        3.5G  3.5G     0 100% /run/media/root/RHEL-7.0 Server.x86_64
aidu3ndddndd

& 
ctrl+d 退出
& Held 2 messages in /var/spool/mail/mycms5
You have mail in /var/spool/mail/mycms5
[mycms5@localhost ~]$ logout
[root@localhost /]# cat > file2017.txt << EOF #以EOF结束
> ABC
> 123456
> aidd
> adduduudd
> 293933
> da73633
> eof
> EOF
[root@localhost /]#

4. 标准错误

[root@localhost /]# audheheh
bash: audheheh: command not found... #标准错误
[root@localhost /]# ahcbddbdd > my.txt
bash: ahcbddbdd: command not found...
[root@localhost /]# cat my.txt  
[root@localhost /]# 
[root@localhost /]# ahcbddbdd 2> my.txt #错误信息写入my.txt
[root@localhost /]# cat my.txt 
bash: ahcbddbdd: command not found...
[root@localhost /]# bozhou-test 2>> my.txt  # 2错误的信息,>> 追加
[root@localhost /]# cat my.txt 
bash: ahcbddbdd: command not found...
bash: bozhou-test: command not found...

5. 查找

find / -name file 查找根目录/下的名为file的文件unix

find / -user mycms5 查找属于用户mycms5的文件code

[root@localhost /]# find / -user mycms5 1> yes 2> no
[root@localhost /]# cat yes
/tmp/.ICE-unix/1933
/tmp/.ICE-unix/12195
/tmp/.esd-1000
/var/spool/mail/mycms5
/var/tmp/abrt/ccpp-2017-10-16-14:03:49-12606
/var/tmp/abrt/ccpp-2017-10-16-14:03:49-12606/time
/var/tmp/abrt/ccpp-2017-10-16-14:03:49-12606/last_occurrence
/var/tmp/abrt/ccpp-2017-10-16-14:03:49-12606/uid
/var/tmp/abrt/ccpp-2017-10-16-14:03:49-12606/kernel
/var/tmp/abrt/ccpp-2017-10-16-14:03:49-12606/architecture
/home/mycms5/.bash_history
/home/mycms5/core.12606
/home/mycms5/.viminfo
此处省略多行.....
[root@localhost /]# cat no
find: ?.proc/3601/task/3601/fd/6?. No such file or directory
find: ?.proc/3601/task/3601/fdinfo/6?. No such file or directory
find: ?.proc/3601/fd/6?. No such file or directory
find: ?.proc/3601/fdinfo/6?. No such file or directory

[root@localhost /]# find / -user mycms5 &> all
[root@localhost /]# cat all
find: ?.proc/3621/task/3621/fd/6?. No such file or directory
find: ?.proc/3621/task/3621/fdinfo/6?. No such file or directory
find: ?.proc/3621/fd/6?. No such file or directory
find: ?.proc/3621/fdinfo/6?. No such file or directory
/tmp/.ICE-unix/1933
/tmp/.ICE-unix/12195
/tmp/.esd-1000
/var/spool/mail/mycms5
/var/tmp/abrt/ccpp-2017-10-16-14:03:49-12606
/var/tmp/abrt/ccpp-2017-10-16-14:03:49-12606/time
/var/tmp/abrt/ccpp-2017-10-16-14:03:49-12606/last_occurrence
/var/tmp/abrt/ccpp-2017-10-16-14:03:49-12606/uid
/var/tmp/abrt/ccpp-2017-10-16-14:03:49-12606/kernel
/var/tmp/abrt/ccpp-2017-10-16-14:03:49-12606/architecture
/var/tmp/abrt/ccpp-2017-10-16-14:03:49-12606/hostname
此处省略多行.....

[root@localhost /]# find / -user mycms5 > all
find: ?.proc/3646?. No such file or directory
find: ?.proc/3648/task/3648/fd/6?. No such file or directory
find: ?.proc/3648/task/3648/fdinfo/6?. No such file or directory
find: ?.proc/3648/fd/6?. No such file or directory
find: ?.proc/3648/fdinfo/6?. No such file or directory
[root@localhost /]# find / -user mycms5 > all 2>&1
[root@localhost /]# cat all
find: ?.proc/3657/task/3657/fd/6?. No such file or directory
find: ?.proc/3657/task/3657/fdinfo/6?. No such file or directory
find: ?.proc/3657/fd/6?. No such file or directory
find: ?.proc/3657/fdinfo/6?. No such file or directory
/tmp/.ICE-unix/1933
/tmp/.ICE-unix/12195
/tmp/.esd-1000
/var/spool/mail/mycms5
/var/tmp/abrt/ccpp-2017-10-16-14:03:49-12606
/var/tmp/abrt/ccpp-2017-10-16-14:03:49-12606/time
/var/tmp/abrt/ccpp-2017-10-16-14:03:49-12606/last_occurrence
/var/tmp/abrt/ccpp-2017-10-16-14:03:49-12606/uid
/var/tmp/abrt/ccpp-2017-10-16-14:03:49-12606/kernel
/var/tmp/abrt/ccpp-2017-10-16-14:03:49-12606/architectu

6. 管道

把一个程序的输出结果直接链接到另外一个程序的输入,常说的管道通常是指无名管道(好比“|”)。ci

[root@localhost /]# ls -l | head -10
total 108
-rw-r--r--.   1 root   root    8628 Oct 17 13:39 all
lrwxrwxrwx.   1 root   root       7 Oct 14 07:23 bin -> usr/bin
dr-xr-xr-x.   3 root   root    4096 Oct 16 12:59 boot
-rw-r--r--.   1 root   root    3260 Oct 17 12:52 cpu
drwxr-xr-x.  19 root   root    3220 Oct 16 23:25 dev
-rw-r--r--.   1 root   root     468 Oct 17 12:53 df
-rw-r--r--.   1 root   root     409 Oct 17 12:58 df.txt
drwxr-xr-x. 134 root   root    8192 Oct 16 23:25 etc
-rw-r--r--.   1 root   root   18236 Oct 17 12:57 etc.txt

6.1 多管道

[root@localhost /]# ls -l | head -100 | more
total 108
-rw-r--r--.   1 root   root    8628 Oct 17 13:39 all
lrwxrwxrwx.   1 root   root       7 Oct 14 07:23 bin -> usr/bin
dr-xr-xr-x.   3 root   root    4096 Oct 16 12:59 boot
-rw-r--r--.   1 root   root    3260 Oct 17 12:52 cpu
drwxr-xr-x.  19 root   root    3220 Oct 16 23:25 dev
-rw-r--r--.   1 root   root     468 Oct 17 12:53 df
-rw-r--r--.   1 root   root     409 Oct 17 12:58 df.txt
drwxr-xr-x. 134 root   root    8192 Oct 16 23:25 etc
-rw-r--r--.   1 root   root   18236 Oct 17 12:57 etc.txt
-rw-r--r--.   1 root   root      45 Oct 17 13:24 file2017.txt
drwxr-xr-x.  12 root   root    4096 Oct 16 21:00 home
lrwxrwxrwx.   1 root   root       7 Oct 14 07:23 lib -> usr/lib
lrwxrwxrwx.   1 root   root       9 Oct 14 07:23 lib64 -> usr/lib64
drwxr-xr-x.   2 root   root       6 Mar 13  2014 media
drwxr-xr-x.   3 root   root      17 Oct 14 14:34 mnt
drwx------.   3 mytest mytest    74 Oct 16 12:59 mytesthome
-rw-r--r--.   1 root   root      78 Oct 17 13:29 my.txt
-rw-r--r--.   1 root   root     248 Oct 17 13:33 no
drwxr-xr-x.   3 root   root      15 Oct 14 07:27 opt
-rwSr--r--.   1 root   root    2410 Oct 16 21:55 passwd1
dr-xr-xr-x. 511 root   root       0 Oct 17 07:25 proc
dr-xr-x---.  14 root   root    4096 Oct 17 12:52 root
drwxr-xr-x.  38 root   root    1200 Oct 17 13:08 run
lrwxrwxrwx.   1 root   root       8 Oct 14 07:23 sbin -> usr/sbin
--More--
[root@localhost /]

6.2 wc 统计

wc(Word Count)命令的功能为统计指定文件中的字节数、字数、行数,并将统计结果显示输出。
命令格式:
wc [选项]文件...
命令参数:
-c 统计字节数。
-l 统计行数。
-m 统计字符数。这个标志不能与 -c 标志一块儿使用。
-w 统计字数。一个字被定义为由空白、跳格或换行字符分隔的字符串。
-L 打印最长行的长度。
-help 显示帮助信息
--version 显示版本信息字符串

[root@localhost /]# cat /etc/passwd | wc -l
49
[root@localhost /]# wc -l  /etc/passwd 
49 /etc/passwd
[root@localhost /]# wc /etc/passwd 
 49   77 2410 /etc/passwd  #行数/单词数/字节数/文件名
[root@localhost /]#

6.3 grep 过滤

在一个文件里过滤东西,经常使用它。it

[root@localhost /]# grep root /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@localhost /]# cat /etc/passwd | grep root
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@localhost /]# cat /etc/passwd | grep ^root  #^表示以什么字符开头
root:x:0:0:root:/root:/bin/bash
[root@localhost /]# cat /etc/passwd | grep  /bin/bash$  
#$表示以什么字符结尾,这里程序可登录系统的用户
root:x:0:0:root:/root:/bin/bash
mycms5:x:1000:1000:mycms5:/home/mycms5:/bin/bash
ok:x:1001:1002::/home/ok:/bin/bash
kuaiji001:x:1002:1001::/home/kuaiji001:/bin/bash
kuaiji002:x:1003:1003::/home/kuaiji002:/bin/bash
mytest:x:1004:1004::/mytesthome:/bin/bash
ppp:x:1006:1007::/home/ppp:/bin/bash
liying:x:1007:1008::/home/liying:/bin/bash
kuaiji:x:1008:1009::/home/kuaiji:/bin/bash
boss:x:1009:1010::/home/boss:/bin/bash
qiaocheng:x:1010:1011::/home/qiaocheng:/bin/bash
[root@localhost /]# cat /etc/passwd | grep  /bin/bash$ | wc -l 
# 统计多少登录用户
11
[root@localhost /]#

6.5 通道的一些经常使用参数

查看grub.cfg内容,去掉注释行、空行
参数含义: -v 取反,^ 开始,$ 结尾io

[root@localhost ~]# cat /boot/grub2/grub.cfg 
#
# DO NOT EDIT THIS FILE
#
# It is automatically generated by grub2-mkconfig using templates
# from /etc/grub.d and settings from /etc/default/grub
#

### BEGIN /etc/grub.d/00_header ###
set pager=1

if [ -s $prefix/grubenv ]; then
  load_env
fi
if [ "${next_entry}" ] ; then
   set default="${next_entry}"
   set next_entry=
   save_env next_entry
   set boot_once=true
else
   set default="${saved_entry}"
# 省略N多行......

### END /etc/grub.d/41_custom ###
[root@localhost ~]# cat /boot/grub2/grub.cfg | grep -v ^# | grep -v ^$  
# 过滤空行、注释行
set pager=1
if [ -s $prefix/grubenv ]; then
  load_env
fi
if [ "${next_entry}" ] ; then
   set default="${next_entry}"
   set next_entry=
   save_env next_entry
   set boot_once=true
else
   set default="${saved_entry}"
fi
# 省略N多行......

[root@localhost ~]# cat /boot/grub2/grub.cfg | grep -v ^# | grep -v ^$ | wc -l
 # 统计行
85
[root@localhost ~]# cat grub.txt | wc -l # 效果等同于上面
85
[root@localhost ~]# cat /boot/grub2/grub.cfg | grep -v ^# | grep -v ^$  > grub.txt
# 输出结果到文件grub.txt
[root@localhost ~]# cat /boot/grub2/grub.cfg | tee 1 | grep -v ^# | tee 2 |  grep -v ^$ | tee 3  > newgrub.txt

上文执行后会生成四个文件:一、二、三、newgrub.txt